fork/home-manager

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

VwCSXggit: replace gitToInit with lib functionfeb70061

master
15.3 KiB527 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.git;
8
9  gitIniType = with types;
10    let
11      primitiveType = either str (either bool int);
12      multipleType = either primitiveType (listOf primitiveType);
13      sectionType = attrsOf multipleType;
14      supersectionType = attrsOf (either multipleType sectionType);
15    in attrsOf supersectionType;
16
17  signModule = types.submodule {
18    options = {
19      key = mkOption {
20        type = types.nullOr types.str;
21        description = ''
22          The default GPG signing key fingerprint.
23
24          Set to `null` to let GnuPG decide what signing key
25          to use depending on commit’s author.
26        '';
27      };
28
29      signByDefault = mkOption {
30        type = types.bool;
31        default = false;
32        description = "Whether commits and tags should be signed by default.";
33      };
34
35      gpgPath = mkOption {
36        type = types.str;
37        default = "${pkgs.gnupg}/bin/gpg2";
38        defaultText = "\${pkgs.gnupg}/bin/gpg2";
39        description = "Path to GnuPG binary to use.";
40      };
41    };
42  };
43
44  includeModule = types.submodule ({ config, ... }: {
45    options = {
46      condition = mkOption {
47        type = types.nullOr types.str;
48        default = null;
49        description = ''
50          Include this configuration only when {var}`condition`
51          matches. Allowed conditions are described in
52          {manpage}`git-config(1)`.
53        '';
54      };
55
56      path = mkOption {
57        type = with types; either str path;
58        description = "Path of the configuration file to include.";
59      };
60
61      contents = mkOption {
62        type = types.attrsOf types.anything;
63        default = { };
64        example = literalExpression ''
65          {
66            user = {
67              email = "bob@work.example.com";
68              name = "Bob Work";
69              signingKey = "1A2B3C4D5E6F7G8H";
70            };
71            commit = {
72              gpgSign = true;
73            };
74          };
75        '';
76        description = ''
77          Configuration to include. If empty then a path must be given.
78
79          This follows the configuration structure as described in
80          {manpage}`git-config(1)`.
81        '';
82      };
83
84      contentSuffix = mkOption {
85        type = types.str;
86        default = "gitconfig";
87        description = ''
88          Nix store name for the git configuration text file,
89          when generating the configuration text from nix options.
90        '';
91
92      };
93    };
94    config.path = mkIf (config.contents != { }) (mkDefault
95      (pkgs.writeText (hm.strings.storeFileName config.contentSuffix)
96        (generators.toGitINI config.contents)));
97  });
98
99in {
100  meta.maintainers = [ maintainers.rycee ];
101
102  options = {
103    programs.git = {
104      enable = mkEnableOption "Git";
105
106      package = mkOption {
107        type = types.package;
108        default = pkgs.git;
109        defaultText = literalExpression "pkgs.git";
110        description = ''
111          Git package to install. Use {var}`pkgs.gitAndTools.gitFull`
112          to gain access to {command}`git send-email` for instance.
113        '';
114      };
115
116      userName = mkOption {
117        type = types.nullOr types.str;
118        default = null;
119        description = "Default user name to use.";
120      };
121
122      userEmail = mkOption {
123        type = types.nullOr types.str;
124        default = null;
125        description = "Default user email to use.";
126      };
127
128      aliases = mkOption {
129        type = types.attrsOf types.str;
130        default = { };
131        example = { co = "checkout"; };
132        description = "Git aliases to define.";
133      };
134
135      signing = mkOption {
136        type = types.nullOr signModule;
137        default = null;
138        description = "Options related to signing commits using GnuPG.";
139      };
140
141      extraConfig = mkOption {
142        type = types.either types.lines gitIniType;
143        default = { };
144        example = {
145          core = { whitespace = "trailing-space,space-before-tab"; };
146          url."ssh://git@host".insteadOf = "otherhost";
147        };
148        description = ''
149          Additional configuration to add. The use of string values is
150          deprecated and will be removed in the future.
151        '';
152      };
153
154      hooks = mkOption {
155        type = types.attrsOf types.path;
156        default = { };
157        example = literalExpression ''
158          {
159            pre-commit = ./pre-commit-script;
160          }
161        '';
162        description = ''
163          Configuration helper for Git hooks.
164          See <https://git-scm.com/docs/githooks>
165          for reference.
166        '';
167      };
168
169      iniContent = mkOption {
170        type = gitIniType;
171        internal = true;
172      };
173
174      ignores = mkOption {
175        type = types.listOf types.str;
176        default = [ ];
177        example = [ "*~" "*.swp" ];
178        description = "List of paths that should be globally ignored.";
179      };
180
181      attributes = mkOption {
182        type = types.listOf types.str;
183        default = [ ];
184        example = [ "*.pdf diff=pdf" ];
185        description = "List of defining attributes set globally.";
186      };
187
188      includes = mkOption {
189        type = types.listOf includeModule;
190        default = [ ];
191        example = literalExpression ''
192          [
193            { path = "~/path/to/config.inc"; }
194            {
195              path = "~/path/to/conditional.inc";
196              condition = "gitdir:~/src/dir";
197            }
198          ]
199        '';
200        description = "List of configuration files to include.";
201      };
202
203      lfs = {
204        enable = mkEnableOption "Git Large File Storage";
205
206        skipSmudge = mkOption {
207          type = types.bool;
208          default = false;
209          description = ''
210            Skip automatic downloading of objects on clone or pull.
211            This requires a manual {command}`git lfs pull`
212            every time a new commit is checked out on your repository.
213          '';
214        };
215      };
216
217      difftastic = {
218        enable = mkEnableOption "" // {
219          description = ''
220            Enable the {command}`difftastic` syntax highlighter.
221            See <https://github.com/Wilfred/difftastic>.
222          '';
223        };
224
225        background = mkOption {
226          type = types.enum [ "light" "dark" ];
227          default = "light";
228          example = "dark";
229          description = ''
230            Determines whether difftastic should use the lighter or darker colors
231            for syntax highlighting.
232          '';
233        };
234
235        color = mkOption {
236          type = types.enum [ "always" "auto" "never" ];
237          default = "auto";
238          example = "always";
239          description = ''
240            Determines when difftastic should color its output.
241          '';
242        };
243
244        display = mkOption {
245          type =
246            types.enum [ "side-by-side" "side-by-side-show-both" "inline" ];
247          default = "side-by-side";
248          example = "inline";
249          description = ''
250            Determines how the output displays - in one column or two columns.
251          '';
252        };
253      };
254
255      delta = {
256        enable = mkEnableOption "" // {
257          description = ''
258            Whether to enable the {command}`delta` syntax highlighter.
259            See <https://github.com/dandavison/delta>.
260          '';
261        };
262
263        package = mkPackageOption pkgs "delta" { };
264
265        options = mkOption {
266          type = with types;
267            let
268              primitiveType = either str (either bool int);
269              sectionType = attrsOf primitiveType;
270            in attrsOf (either primitiveType sectionType);
271          default = { };
272          example = {
273            features = "decorations";
274            whitespace-error-style = "22 reverse";
275            decorations = {
276              commit-decoration-style = "bold yellow box ul";
277              file-style = "bold yellow ul";
278              file-decoration-style = "none";
279            };
280          };
281          description = ''
282            Options to configure delta.
283          '';
284        };
285      };
286
287      diff-so-fancy = {
288        enable = mkEnableOption "" // {
289          description = ''
290            Enable the {command}`diff-so-fancy` diff colorizer.
291            See <https://github.com/so-fancy/diff-so-fancy>.
292          '';
293        };
294
295        pagerOpts = mkOption {
296          type = types.listOf types.str;
297          default = [ "--tabs=4" "-RFX" ];
298          description = ''
299            Arguments to be passed to {command}`less`.
300          '';
301        };
302
303        markEmptyLines = mkOption {
304          type = types.bool;
305          default = true;
306          example = false;
307          description = ''
308            Whether the first block of an empty line should be colored.
309          '';
310        };
311
312        changeHunkIndicators = mkOption {
313          type = types.bool;
314          default = true;
315          example = false;
316          description = ''
317            Simplify git header chunks to a more human readable format.
318          '';
319        };
320
321        stripLeadingSymbols = mkOption {
322          type = types.bool;
323          default = true;
324          example = false;
325          description = ''
326            Whether the `+` or `-` at
327            line-start should be removed.
328          '';
329        };
330
331        useUnicodeRuler = mkOption {
332          type = types.bool;
333          default = true;
334          example = false;
335          description = ''
336            By default, the separator for the file header uses Unicode
337            line-drawing characters. If this is causing output errors on
338            your terminal, set this to false to use ASCII characters instead.
339          '';
340        };
341
342        rulerWidth = mkOption {
343          type = types.nullOr types.int;
344          default = null;
345          example = false;
346          description = ''
347            By default, the separator for the file header spans the full
348            width of the terminal. Use this setting to set the width of
349            the file header manually.
350          '';
351        };
352      };
353    };
354  };
355
356  config = mkIf cfg.enable (mkMerge [
357    {
358      home.packages = [ cfg.package ];
359      assertions = [{
360        assertion = let
361          enabled =
362            [ cfg.delta.enable cfg.diff-so-fancy.enable cfg.difftastic.enable ];
363        in count id enabled <= 1;
364        message =
365          "Only one of 'programs.git.delta.enable' or 'programs.git.difftastic.enable' or 'programs.git.diff-so-fancy.enable' can be set to true at the same time.";
366      }];
367
368      programs.git.iniContent.user = {
369        name = mkIf (cfg.userName != null) cfg.userName;
370        email = mkIf (cfg.userEmail != null) cfg.userEmail;
371      };
372
373      xdg.configFile = {
374        "git/config".text = generators.toGitINI cfg.iniContent;
375
376        "git/ignore" = mkIf (cfg.ignores != [ ]) {
377          text = concatStringsSep "\n" cfg.ignores + "\n";
378        };
379
380        "git/attributes" = mkIf (cfg.attributes != [ ]) {
381          text = concatStringsSep "\n" cfg.attributes + "\n";
382        };
383      };
384    }
385
386    {
387      programs.git.iniContent = let
388        hasSmtp = name: account: account.smtp != null;
389
390        genIdentity = name: account:
391          with account;
392          nameValuePair "sendemail.${name}" (if account.msmtp.enable then {
393            smtpServer = "${pkgs.msmtp}/bin/msmtp";
394            envelopeSender = "auto";
395            from = address;
396          } else
397            {
398              smtpEncryption = if smtp.tls.enable then
399                (if smtp.tls.useStartTls
400                || versionOlder config.home.stateVersion "20.09" then
401                  "tls"
402                else
403                  "ssl")
404              else
405                "";
406              smtpSslCertPath =
407                mkIf smtp.tls.enable (toString smtp.tls.certificatesFile);
408              smtpServer = smtp.host;
409              smtpUser = userName;
410              from = address;
411            } // optionalAttrs (smtp.port != null) {
412              smtpServerPort = smtp.port;
413            });
414      in mapAttrs' genIdentity
415      (filterAttrs hasSmtp config.accounts.email.accounts);
416    }
417
418    (mkIf (cfg.signing != null) {
419      programs.git.iniContent = {
420        user.signingKey = mkIf (cfg.signing.key != null) cfg.signing.key;
421        commit.gpgSign = mkDefault cfg.signing.signByDefault;
422        tag.gpgSign = mkDefault cfg.signing.signByDefault;
423        gpg.program = cfg.signing.gpgPath;
424      };
425    })
426
427    (mkIf (cfg.hooks != { }) {
428      programs.git.iniContent = {
429        core.hooksPath = let
430          entries =
431            mapAttrsToList (name: path: { inherit name path; }) cfg.hooks;
432        in toString (pkgs.linkFarm "git-hooks" entries);
433      };
434    })
435
436    (mkIf (cfg.aliases != { }) { programs.git.iniContent.alias = cfg.aliases; })
437
438    (mkIf (lib.isAttrs cfg.extraConfig) {
439      programs.git.iniContent = cfg.extraConfig;
440    })
441
442    (mkIf (lib.isString cfg.extraConfig) {
443      warnings = [''
444        Using programs.git.extraConfig as a string option is
445        deprecated and will be removed in the future. Please
446        change to using it as an attribute set instead.
447      ''];
448
449      xdg.configFile."git/config".text = cfg.extraConfig;
450    })
451
452    (mkIf (cfg.includes != [ ]) {
453      xdg.configFile."git/config".text = let
454        include = i:
455          with i;
456          if condition != null then {
457            includeIf.${condition}.path = "${path}";
458          } else {
459            include.path = "${path}";
460          };
461      in mkAfter (concatStringsSep "\n"
462        (map generators.toGitINI (map include cfg.includes)));
463    })
464
465    (mkIf cfg.lfs.enable {
466      home.packages = [ pkgs.git-lfs ];
467
468      programs.git.iniContent.filter.lfs =
469        let skipArg = optional cfg.lfs.skipSmudge "--skip";
470        in {
471          clean = "git-lfs clean -- %f";
472          process =
473            concatStringsSep " " ([ "git-lfs" "filter-process" ] ++ skipArg);
474          required = true;
475          smudge = concatStringsSep " "
476            ([ "git-lfs" "smudge" ] ++ skipArg ++ [ "--" "%f" ]);
477        };
478    })
479
480    (mkIf cfg.difftastic.enable {
481      home.packages = [ pkgs.difftastic ];
482
483      programs.git.iniContent = let
484        difftCommand = concatStringsSep " " [
485          "${pkgs.difftastic}/bin/difft"
486          "--color ${cfg.difftastic.color}"
487          "--background ${cfg.difftastic.background}"
488          "--display ${cfg.difftastic.display}"
489        ];
490      in { diff.external = difftCommand; };
491    })
492
493    (let
494      deltaPackage = cfg.delta.package;
495      deltaCommand = "${deltaPackage}/bin/delta";
496    in mkIf cfg.delta.enable {
497      home.packages = [ deltaPackage ];
498
499      programs.git.iniContent = {
500        core.pager = deltaCommand;
501        interactive.diffFilter = "${deltaCommand} --color-only";
502        delta = cfg.delta.options;
503      };
504    })
505
506    (mkIf cfg.diff-so-fancy.enable {
507      home.packages = [ pkgs.diff-so-fancy ];
508
509      programs.git.iniContent =
510        let dsfCommand = "${pkgs.diff-so-fancy}/bin/diff-so-fancy";
511        in {
512          core.pager = "${dsfCommand} | ${pkgs.less}/bin/less ${
513              escapeShellArgs cfg.diff-so-fancy.pagerOpts
514            }";
515          interactive.diffFilter = "${dsfCommand} --patch";
516          diff-so-fancy = {
517            markEmptyLines = cfg.diff-so-fancy.markEmptyLines;
518            changeHunkIndicators = cfg.diff-so-fancy.changeHunkIndicators;
519            stripLeadingSymbols = cfg.diff-so-fancy.stripLeadingSymbols;
520            useUnicodeRuler = cfg.diff-so-fancy.useUnicodeRuler;
521            rulerWidth = mkIf (cfg.diff-so-fancy.rulerWidth != null)
522              (cfg.diff-so-fancy.rulerWidth);
523          };
524        };
525    })
526  ]);
527}