Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions flake-parts/homeModules/git.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,41 @@
default = null;
description = "Deployed path of the user's GitHub PAT. Internal: set by nixosModules/git.nix from the clan vars generator — never hardcode it in a profile.";
};
# The same PAT as Nix access-tokens, for fetching private github:
# flake inputs. Lives only in the user's ~/.config/nix/nix.conf:
# the system nix.conf (daemon, activation, boot) never sees it, so
# a bad token can only break this user's nix commands.
nix = {
enable = lib.mkEnableOption "Nix access-tokens for github.com from the githubAuth PAT, in the user's own nix.conf";
accessTokensFile = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "Deployed path of the generated `access-tokens = github.com=...` line. Internal: set by nixosModules/git.nix from the nix-access-tokens-<user> clan var — never hardcode it in a profile.";
};
};
};
};

config = lib.mkIf cfg.enable {
assertions = [
{
assertion = cfg.githubAuth.nix.enable -> cfg.githubAuth.enabled;
message = "homeSpec.programs.git.githubAuth.nix.enable needs homeSpec.programs.git.githubAuth.enabled: the Nix token is derived from the githubAuth PAT.";
}
];

home.packages = with pkgs; [
gh
];

# `!include` (never plain `include`): a missing or unreadable file is
# skipped silently, so nix keeps working before the var is generated
# or deployed. The clan var generator validates the token, so the
# included file is always one well-formed line.
nix.extraOptions = lib.mkIf (cfg.githubAuth.nix.enable && cfg.githubAuth.nix.accessTokensFile != null) ''
!include ${cfg.githubAuth.nix.accessTokensFile}
'';

programs = {
lazygit.enable = true;

Expand Down
59 changes: 55 additions & 4 deletions flake-parts/nixosModules/git.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ _: {
# owned by that user. sops-nix deploys it to
# /run/secrets/vars/shared/github-<user>/pat
# (owner = user, mode 0400, neededFor = services).
# With homeSpec.programs.git.githubAuth.nix.enable, a derived generator
# "nix-access-tokens-<user>" renders the same PAT as a nix.conf line that
# the user's ~/.config/nix/nix.conf `!include`s (never the system one).
#
# Provisioning (interactive, human-only):
# clan vars generate <machine> (prompts for github-<user> pat)
Expand All @@ -29,6 +32,7 @@ _: {
flake.nixosModules.git = {
config,
lib,
pkgs,
...
}: let
# Read-only scan of home-manager.users (attrsOf hmModule submodule —
Expand All @@ -51,6 +55,14 @@ _: {
((git.enable or false) && ((git.githubAuth or {}).enabled or false)) || mcpPatConsumer
)
config.home-manager.users;

# Users who also want the PAT as Nix access-tokens
# (homeSpec.programs.git.githubAuth.nix.enable). Same defensive scan.
nixTokenUsers =
lib.filterAttrs (
_: hmUser: ((((((hmUser.homeSpec or {}).programs or {}).git or {}).githubAuth or {}).nix or {}).enable or false)
)
authUsers;
in {
config = {
clan.core.vars.generators = lib.mkMerge (
Expand All @@ -73,6 +85,36 @@ _: {
}
)
authUsers
# nix-access-tokens-<user>: the github-<user> PAT rendered as one
# nix.conf line, derived (no prompt) so the PAT var is untouched.
# The token is validated here: a malformed include is a hard
# "syntax error in configuration line" for every nix command, so
# it must fail at `clan vars generate`, never on the machine.
# Regenerate it after rotating github-<user>.
++ lib.mapAttrsToList
(
userName: _: {
"nix-access-tokens-${userName}" = {
share = true;
dependencies = ["github-${userName}"];
files."access-tokens.conf" = {
owner = userName;
mode = "0400";
neededFor = "services";
};
runtimeInputs = with pkgs; [coreutils gnugrep];
script = ''
token=$(tr -d '[:space:]' < "$in/github-${userName}/pat")
if ! printf '%s' "$token" | grep -Eq '^(ghp_|github_pat_)[A-Za-z0-9_]+$'; then
echo "github-${userName}/pat is not a GitHub PAT (ghp_... or github_pat_...)" >&2
exit 1
fi
printf 'access-tokens = github.com=%s\n' "$token" > "$out/access-tokens.conf"
'';
};
}
)
nixTokenUsers
);

# Feed the generator's deployed path into every home module as
Expand All @@ -90,12 +132,21 @@ _: {
osConfig,
...
}: let
generators =
if osConfig == null
then {}
else osConfig.clan.core.vars.generators or {};
generator = "github-${config.home.username}";
hasGenerator = osConfig != null && ((osConfig.clan.core.vars.generators or {}) ? ${generator});
nixGenerator = "nix-access-tokens-${config.home.username}";
in {
homeSpec.programs.git.githubAuth.patFile =
lib.mkIf hasGenerator
osConfig.clan.core.vars.generators.${generator}.files.pat.path;
homeSpec.programs.git.githubAuth = {
patFile =
lib.mkIf (generators ? ${generator})
generators.${generator}.files.pat.path;
nix.accessTokensFile =
lib.mkIf (generators ? ${nixGenerator})
generators.${nixGenerator}.files."access-tokens.conf".path;
};
}
)
];
Expand Down
5 changes: 4 additions & 1 deletion tests/fixtures/nixos.nix
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@
enable = true;
userName = "alice";
userEmail = "alice@example.com";
githubAuth.enabled = true;
githubAuth = {
enabled = true;
nix.enable = true;
};
};
headroom.enable = true;
k9s.enable = true;
Expand Down
Loading