diff --git a/flake-parts/homeModules/git.nix b/flake-parts/homeModules/git.nix index 9d55705..4661f3c 100644 --- a/flake-parts/homeModules/git.nix +++ b/flake-parts/homeModules/git.nix @@ -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- 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; diff --git a/flake-parts/nixosModules/git.nix b/flake-parts/nixosModules/git.nix index 3f1b46b..fd56ade 100644 --- a/flake-parts/nixosModules/git.nix +++ b/flake-parts/nixosModules/git.nix @@ -12,6 +12,9 @@ _: { # owned by that user. sops-nix deploys it to # /run/secrets/vars/shared/github-/pat # (owner = user, mode 0400, neededFor = services). + # With homeSpec.programs.git.githubAuth.nix.enable, a derived generator + # "nix-access-tokens-" 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 (prompts for github- pat) @@ -29,6 +32,7 @@ _: { flake.nixosModules.git = { config, lib, + pkgs, ... }: let # Read-only scan of home-manager.users (attrsOf hmModule submodule — @@ -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 ( @@ -73,6 +85,36 @@ _: { } ) authUsers + # nix-access-tokens-: the github- 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-. + ++ 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 @@ -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; + }; } ) ]; diff --git a/tests/fixtures/nixos.nix b/tests/fixtures/nixos.nix index 58f12df..e2c367d 100644 --- a/tests/fixtures/nixos.nix +++ b/tests/fixtures/nixos.nix @@ -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;