fork/home-manager

git clone https://git.julia.blue/fork/home-manager

satoqzgh: option to enable helper for additional hosts (#4288)4fd794d3

master
3.6 KiB144 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.gh;
8
9  yamlFormat = pkgs.formats.yaml { };
10
11  settingsType = types.submodule {
12    freeformType = yamlFormat.type;
13    # These options are only here for the mkRenamedOptionModule support
14    options = {
15      aliases = mkOption {
16        type = with types; attrsOf str;
17        default = { };
18        example = literalExpression ''
19          {
20            co = "pr checkout";
21            pv = "pr view";
22          }
23        '';
24        description = ''
25          Aliases that allow you to create nicknames for gh commands.
26        '';
27      };
28      editor = mkOption {
29        type = types.str;
30        default = "";
31        description = ''
32          The editor that gh should run when creating issues, pull requests, etc.
33          If blank, will refer to environment.
34        '';
35      };
36      git_protocol = mkOption {
37        type = types.str;
38        default = "https";
39        example = "ssh";
40        description = ''
41          The protocol to use when performing Git operations.
42        '';
43      };
44    };
45  };
46
47in {
48  meta.maintainers = [ maintainers.gerschtli maintainers.berbiche ];
49
50  imports = (map (x:
51    mkRenamedOptionModule [ "programs" "gh" x ] [
52      "programs"
53      "gh"
54      "settings"
55      x
56    ]) [ "aliases" "editor" ]) ++ [
57      (mkRenamedOptionModule [ "programs" "gh" "gitProtocol" ] [
58        "programs"
59        "gh"
60        "settings"
61        "git_protocol"
62      ])
63      (mkRenamedOptionModule [ "programs" "gh" "enableGitCredentialHelper" ] [
64        "programs"
65        "gh"
66        "gitCredentialHelper"
67        "enable"
68      ])
69    ];
70
71  options.programs.gh = {
72    enable = mkEnableOption "GitHub CLI tool";
73
74    package = mkOption {
75      type = types.package;
76      default = pkgs.gh;
77      defaultText = literalExpression "pkgs.gh";
78      description = "Package providing {command}`gh`.";
79    };
80
81    settings = mkOption {
82      type = settingsType;
83      default = { };
84      description =
85        "Configuration written to {file}`$XDG_CONFIG_HOME/gh/config.yml`.";
86      example = literalExpression ''
87        {
88          git_protocol = "ssh";
89
90          prompt = "enabled";
91
92          aliases = {
93            co = "pr checkout";
94            pv = "pr view";
95          };
96        };
97      '';
98    };
99
100    gitCredentialHelper = {
101      enable = mkEnableOption "the gh git credential helper" // {
102        default = true;
103      };
104
105      hosts = mkOption {
106        type = types.listOf types.str;
107        default = [ "https://github.com" ];
108        description = "GitHub hosts to enable the gh git credential helper for";
109        example = literalExpression ''
110          [ "https://github.com" "https://github.example.com" ]
111        '';
112      };
113    };
114
115    extensions = mkOption {
116      type = types.listOf types.package;
117      default = [ ];
118      description = ''
119        gh extensions, see <https://cli.github.com/manual/gh_extension>.
120      '';
121      example = literalExpression "[ pkgs.gh-eco ]";
122    };
123  };
124
125  config = mkIf cfg.enable {
126    home.packages = [ cfg.package ];
127
128    xdg.configFile."gh/config.yml".source =
129      yamlFormat.generate "gh-config.yml" cfg.settings;
130
131    programs.git.extraConfig.credential = mkIf cfg.gitCredentialHelper.enable
132      (builtins.listToAttrs (map (host:
133        lib.nameValuePair host {
134          helper = "${cfg.package}/bin/gh auth git-credential";
135        }) cfg.gitCredentialHelper.hosts));
136
137    xdg.dataFile."gh/extensions" = mkIf (cfg.extensions != [ ]) {
138      source = pkgs.linkFarm "gh-extensions" (builtins.map (p: {
139        name = p.pname;
140        path = "${p}/bin";
141      }) cfg.extensions);
142    };
143  };
144}