From 4fd794d3df88735dcf9662155d77b08a2e2dde29 Mon Sep 17 00:00:00 2001 From: satoqz <40795431+satoqz@users.noreply.github.com> Date: Mon, 31 Jul 2023 16:40:37 -0700 Subject: [PATCH] gh: option to enable helper for additional hosts (#4288) * gh: option to enable helper for additional hosts `gh` can also be used with github enterprise hosts, for which there exists no easy option to enable the credential helper except for directly working with `programs.git.extraConfig`. Not sure if this is a needed addition since it's somewhat niche, at the same time it's not very complex and makes the life of github enterprise a little easier. * gh: update credential-helper tests * gh: refactor credential helper option this moves from `enableGitCredentialHelper` to `gitCredentialHelper.enable` and `gitCredentialHelper.hosts`. * gh: lib.mkIf -> mkIf --- modules/programs/gh.nix | 28 +++++++++++++++---- .../programs/gh/credential-helper.git.conf | 3 ++ .../modules/programs/gh/credential-helper.nix | 5 +++- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/modules/programs/gh.nix b/modules/programs/gh.nix index fcf0f8f3..526010a5 100644 --- a/modules/programs/gh.nix +++ b/modules/programs/gh.nix @@ -60,6 +60,12 @@ in { "settings" "git_protocol" ]) + (mkRenamedOptionModule [ "programs" "gh" "enableGitCredentialHelper" ] [ + "programs" + "gh" + "gitCredentialHelper" + "enable" + ]) ]; options.programs.gh = { @@ -91,11 +97,21 @@ in { ''; }; - enableGitCredentialHelper = - mkEnableOption "the gh git credential helper for github.com" // { + gitCredentialHelper = { + enable = mkEnableOption "the gh git credential helper" // { default = true; }; + hosts = mkOption { + type = types.listOf types.str; + default = [ "https://github.com" ]; + description = "GitHub hosts to enable the gh git credential helper for"; + example = literalExpression '' + [ "https://github.com" "https://github.example.com" ] + ''; + }; + }; + extensions = mkOption { type = types.listOf types.package; default = [ ]; @@ -112,9 +128,11 @@ in { xdg.configFile."gh/config.yml".source = yamlFormat.generate "gh-config.yml" cfg.settings; - programs.git.extraConfig.credential."https://github.com".helper = - mkIf cfg.enableGitCredentialHelper - "${cfg.package}/bin/gh auth git-credential"; + programs.git.extraConfig.credential = mkIf cfg.gitCredentialHelper.enable + (builtins.listToAttrs (map (host: + lib.nameValuePair host { + helper = "${cfg.package}/bin/gh auth git-credential"; + }) cfg.gitCredentialHelper.hosts)); xdg.dataFile."gh/extensions" = mkIf (cfg.extensions != [ ]) { source = pkgs.linkFarm "gh-extensions" (builtins.map (p: { diff --git a/tests/modules/programs/gh/credential-helper.git.conf b/tests/modules/programs/gh/credential-helper.git.conf index ea2c61ee..529d6725 100644 --- a/tests/modules/programs/gh/credential-helper.git.conf +++ b/tests/modules/programs/gh/credential-helper.git.conf @@ -1,2 +1,5 @@ [credential "https://github.com"] helper = "@gh@/bin/gh auth git-credential" + +[credential "https://github.example.com"] + helper = "@gh@/bin/gh auth git-credential" diff --git a/tests/modules/programs/gh/credential-helper.nix b/tests/modules/programs/gh/credential-helper.nix index 0c5ed872..77157231 100644 --- a/tests/modules/programs/gh/credential-helper.nix +++ b/tests/modules/programs/gh/credential-helper.nix @@ -3,7 +3,10 @@ { programs.gh = { enable = true; - enableGitCredentialHelper = true; + gitCredentialHelper = { + enable = true; + hosts = [ "https://github.com" "https://github.example.com" ]; + }; }; programs.git.enable = true;