Repository navigation
Expand file tree
/
Copy pathgit.nix
More file actions
636 lines (580 loc) · 19.7 KB
/
Copy pathgit.nix
File metadata and controls
636 lines (580 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
{
config,
lib,
pkgs,
...
}:
let
inherit (lib)
concatStringsSep
mkDefault
mkEnableOption
mkIf
mkOption
mkOptionDefault
types
;
cfg = config.programs.git;
signingFormatStateVersionDefault = lib.hm.deprecations.mkStateVersionOptionDefault {
inherit (config.home) stateVersion;
since = "25.05";
optionPath = [
"programs"
"git"
"signing"
"format"
];
legacy.value = "openpgp";
current.value = null;
};
in
{
meta.maintainers = with lib.maintainers; [
khaneliman
rycee
];
imports =
(lib.mapAttrsToList (name: message: lib.mkRemovedOptionModule [ "programs" "git" name ] message) {
aliases = "Use `programs.git.settings.alias` instead.";
extraConfig = "Use `programs.git.settings` instead.";
userEmail = "Use `programs.git.settings.user.email` instead.";
userName = "Use `programs.git.settings.user.name` instead.";
})
++ [
(lib.mkRemovedOptionModule [
"programs"
"git"
"signing"
"gpgPath"
] "Use `programs.git.signing.signer` instead.")
];
options =
let
gitIniType =
with types;
let
primitiveType = either str (either bool int);
multipleType = either primitiveType (listOf primitiveType);
sectionType = attrsOf multipleType;
supersectionType = attrsOf (either multipleType sectionType);
in
attrsOf supersectionType;
in
{
programs.git = {
enable = mkEnableOption "Git";
package = lib.mkPackageOption pkgs "git" {
nullable = true;
example = "pkgs.gitFull";
extraDescription = ''
Use {var}`pkgs.gitFull`
to gain access to {command}`git send-email` for instance.
'';
};
signing = {
allowedSigners = mkOption {
type = types.lines;
default = "";
example = ''
user@example.com namespaces="git" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...
'';
description = ''
SSH public keys trusted when verifying signed commits and tags.
Each line must follow the allowed signers format described in
{manpage}`ssh-keygen(1)`. When non-empty, the content is written
to {file}`$XDG_CONFIG_HOME/git/allowed_signers` and configured as
Git's `gpg.ssh.allowedSignersFile` setting.
'';
};
key = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
The default signing key fingerprint.
Set to `null` to let the signer decide what signing key
to use depending on commit’s author.
'';
};
format = mkOption {
type = types.nullOr (
types.enum [
"openpgp"
"ssh"
"x509"
]
);
inherit (signingFormatStateVersionDefault) defaultText;
description = ''
The signing method to use when signing commits and tags.
Valid values are `openpgp` (OpenPGP/GnuPG), `ssh` (SSH), and `x509` (X.509 certificates).
'';
};
signByDefault = mkOption {
type = types.nullOr types.bool;
default = null;
description = "Whether commits and tags should be signed by default.";
};
signer = mkOption {
type = types.nullOr types.str;
description = "Path to signer binary to use.";
};
};
settings = mkOption {
type = with types; either gitIniType (listOf gitIniType);
default = { };
example = [
{
core = {
whitespace = "trailing-space,space-before-tab";
};
url."ssh://git@host".insteadOf = "otherhost";
}
{
credential."https://example.com".helper = "";
}
{
credential."https://example.com".helper = "oauth";
}
];
description = ''
Configuration written to {file}`$XDG_CONFIG_HOME/git/config`.
This may be either a single attrset of Git settings or an ordered
list of attrset fragments when repeated sections or explicit
ordering matter.
See {manpage}`git-config(1)` for details.
'';
};
hooks = mkOption {
type = types.attrsOf types.path;
default = { };
example = lib.literalExpression ''
{
pre-commit = ./pre-commit-script;
}
'';
description = ''
Configuration helper for Git hooks.
See <https://git-scm.com/docs/githooks>
for reference.
'';
};
iniContent = mkOption {
type = gitIniType;
internal = true;
};
ignores = mkOption {
type = types.listOf types.str;
default = [ ];
example = [
"*~"
"*.swp"
];
description = "List of paths that should be globally ignored.";
};
attributes = mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "*.pdf diff=pdf" ];
description = "List of defining attributes set globally.";
};
includes = mkOption {
type = types.listOf (
types.submodule (
{ config, ... }:
{
options = {
condition = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Include this configuration only when {var}`condition`
matches. Allowed conditions are described in
{manpage}`git-config(1)`.
'';
};
path = mkOption {
type = with types; either str path;
description = "Path of the configuration file to include.";
};
contents = mkOption {
type = types.attrsOf types.anything;
default = { };
example = {
user = {
email = "bob@work.example.com";
name = "Bob Work";
signingKey = "1A2B3C4D5E6F7G8H";
};
commit = {
gpgSign = true;
};
};
description = ''
Configuration to include. If empty then a path must be given.
This follows the configuration structure as described in
{manpage}`git-config(1)`.
'';
};
contentSuffix = mkOption {
type = types.str;
default = "gitconfig";
description = ''
Nix store name for the git configuration text file,
when generating the configuration text from nix options.
'';
};
};
config.path = mkIf (config.contents != { }) (
mkDefault (
pkgs.writeText (lib.hm.strings.storeFileName config.contentSuffix) (
lib.generators.toGitINI config.contents
)
)
);
}
)
);
default = [ ];
example = [
{ path = "~/path/to/config.inc"; }
{
path = "~/path/to/conditional.inc";
condition = "gitdir:~/src/dir";
}
];
description = "List of configuration files to include.";
};
lfs = {
enable = mkEnableOption "Git Large File Storage";
package = lib.mkPackageOption pkgs "git-lfs" { nullable = true; };
skipSmudge = mkOption {
type = types.bool;
default = false;
description = ''
Skip automatic downloading of objects on clone or pull.
This requires a manual {command}`git lfs pull`
every time a new commit is checked out on your repository.
'';
};
};
maintenance = {
enable = mkEnableOption "" // {
description = ''
Enable the automatic {command}`git maintenance`.
If you have SSH remotes, set {option}`programs.git.package` to a
git version with SSH support (eg: `pkgs.gitFull`).
See <https://git-scm.com/docs/git-maintenance>.
'';
};
repositories = mkOption {
type = with types; listOf str;
default = [ ];
description = ''
Repositories on which {command}`git maintenance` should run.
Should be a list of absolute paths.
'';
};
timers = mkOption {
type = types.attrsOf types.str;
default = {
hourly = "*-*-* 1..23:53:00";
daily = "Tue..Sun *-*-* 0:53:00";
weekly = "Mon 0:53:00";
};
description = ''
Systemd timers to create for scheduled {command}`git maintenance`.
Key is passed to `--schedule` argument in {command}`git maintenance run`
and value is passed to `Timer.OnCalendar` in `systemd.user.timers`.
'';
};
};
};
};
config = mkIf cfg.enable (
let
settingsFragments = lib.filter (fragment: fragment != { }) (
if builtins.isList cfg.settings then cfg.settings else [ ]
);
renderedIniFragments = lib.filter (text: lib.match "[[:space:]]*" text == null) (
[ (lib.generators.toGitINI cfg.iniContent) ] ++ map lib.generators.toGitINI settingsFragments
);
in
lib.mkMerge [
{
home.packages = lib.optionals (cfg.package != null) [ cfg.package ];
assertions = [
(
let
configOf =
{
name,
gitIntegrationOption ? [ "enableGitIntegration" ],
}:
{
name = "programs.${name}.${lib.concatStringsSep "." gitIntegrationOption}";
value =
config.programs.${name}.enable && lib.getAttrFromPath gitIntegrationOption config.programs.${name};
};
enabled = builtins.filter (x: x.value) (
map configOf [
{ name = "delta"; }
{ name = "diff-highlight"; }
{ name = "diff-so-fancy"; }
{
name = "difftastic";
gitIntegrationOption = [
"git"
"enable"
];
}
{ name = "patdiff"; }
{ name = "riff"; }
]
);
in
{
assertion = lib.length enabled <= 1;
message = ''
Only one of the following options can be enabled at a time.
- ${lib.concatStringsSep "\n - " (map (x: "`${x.name}'") enabled)}
'';
}
)
];
xdg.configFile = {
"git/config".text = concatStringsSep "\n" renderedIniFragments;
"git/ignore" = mkIf (cfg.ignores != [ ]) {
text = concatStringsSep "\n" cfg.ignores + "\n";
};
"git/attributes" = mkIf (cfg.attributes != [ ]) {
text = concatStringsSep "\n" cfg.attributes + "\n";
};
};
}
{
programs.git.iniContent =
let
hasSmtp = _name: account: account.enable && account.smtp != null;
genIdentity =
name: account:
let
inherit (account)
address
realName
smtp
userName
;
in
lib.nameValuePair "sendemail.${name}" (
if account.msmtp.enable then
{
sendmailCmd = lib.getExe config.programs.msmtp.package;
envelopeSender = "auto";
from = "${realName} <${address}>";
}
else
{
smtpEncryption =
if smtp.tls.enable then
(if smtp.tls.useStartTls || lib.versionOlder config.home.stateVersion "20.09" then "tls" else "ssl")
else
"";
smtpSslCertPath = mkIf smtp.tls.enable (toString smtp.tls.certificatesFile);
smtpServer = smtp.host;
smtpUser = userName;
from = "${realName} <${address}>";
}
// lib.optionalAttrs (smtp.port != null) {
smtpServerPort = smtp.port;
}
);
in
lib.mapAttrs' genIdentity (lib.filterAttrs hasSmtp config.accounts.email.accounts);
}
(mkIf (cfg.signing != { }) {
programs.git = {
signing = {
format = mkOptionDefault signingFormatStateVersionDefault.effectiveDefault;
signer =
let
defaultSigners = {
openpgp = lib.getExe config.programs.gpg.package;
ssh = lib.getExe' pkgs.openssh "ssh-keygen";
x509 = lib.getExe' config.programs.gpg.package "gpgsm";
};
in
mkIf (cfg.signing.format != null) (mkOptionDefault defaultSigners.${cfg.signing.format});
};
iniContent = lib.mkMerge [
(mkIf (cfg.signing.key != null) {
user.signingKey = mkDefault cfg.signing.key;
})
(mkIf (cfg.signing.signByDefault != null) {
commit.gpgSign = mkDefault cfg.signing.signByDefault;
tag.gpgSign = mkDefault cfg.signing.signByDefault;
})
(mkIf (cfg.signing.format != null) {
gpg = {
format = mkDefault cfg.signing.format;
${cfg.signing.format}.program = mkDefault cfg.signing.signer;
};
})
];
};
})
(mkIf (cfg.signing.allowedSigners != "") {
xdg.configFile."git/allowed_signers".text = cfg.signing.allowedSigners;
programs.git.iniContent.gpg.ssh.allowedSignersFile =
mkDefault "${config.xdg.configHome}/git/allowed_signers";
})
(mkIf (cfg.hooks != { }) {
programs.git.iniContent = {
core.hooksPath =
let
entries = lib.mapAttrsToList (name: path: { inherit name path; }) cfg.hooks;
in
toString (pkgs.linkFarm "git-hooks" entries);
};
})
(mkIf (!builtins.isList cfg.settings && cfg.settings != { }) {
programs.git.iniContent = cfg.settings;
})
(mkIf (cfg.includes != [ ]) {
xdg.configFile."git/config".text =
let
include =
i:
with i;
if condition != null then
{
includeIf.${condition}.path = "${path}";
}
else
{
include.path = "${path}";
};
in
lib.mkAfter (concatStringsSep "\n" (map lib.generators.toGitINI (map include cfg.includes)));
})
(mkIf cfg.lfs.enable {
home.packages = lib.mkIf (cfg.lfs.package != null) [ cfg.lfs.package ];
programs.git.iniContent.filter.lfs =
let
skipArg = lib.optional cfg.lfs.skipSmudge "--skip";
lfsPath = if cfg.lfs.package != null then lib.getExe cfg.lfs.package else "git-lfs";
in
{
clean = "${lfsPath} clean -- %f";
process = concatStringsSep " " (
[
lfsPath
"filter-process"
]
++ skipArg
);
required = true;
smudge = concatStringsSep " " (
[
lfsPath
"smudge"
]
++ skipArg
++ [
"--"
"%f"
]
);
};
})
(mkIf cfg.maintenance.enable {
programs.git.iniContent.maintenance.repo = cfg.maintenance.repositories;
systemd.user.services."git-maintenance@" = {
Unit = {
Description = "Optimize Git repositories data";
Documentation = [ "man:git-maintenance(1)" ];
};
Service = {
Type = "oneshot";
ExecStart =
let
exe = if cfg.package != null then lib.getExe cfg.package else "git";
in
''
"${exe}" for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=%i
'';
LockPersonality = "yes";
MemoryDenyWriteExecute = "yes";
NoNewPrivileges = "yes";
RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6 AF_VSOCK";
RestrictNamespaces = "yes";
RestrictRealtime = "yes";
RestrictSUIDSGID = "yes";
SystemCallArchitectures = "native";
SystemCallFilter = "@system-service";
};
};
systemd.user.timers =
let
toSystemdTimer =
name: time:
lib.attrsets.nameValuePair "git-maintenance@${name}" {
Unit.Description = "Optimize Git repositories data";
Timer = {
OnCalendar = time;
Persistent = true;
};
Install.WantedBy = [ "timers.target" ];
};
in
lib.attrsets.mapAttrs' toSystemdTimer cfg.maintenance.timers;
launchd.agents =
let
baseArguments = [
"${if cfg.package != null then lib.getExe cfg.package else "git"}"
"for-each-repo"
"--keep-going"
"--config=maintenance.repo"
"maintenance"
"run"
];
in
{
"git-maintenance-hourly" = {
enable = true;
config = {
ProgramArguments = baseArguments ++ [ "--schedule=hourly" ];
StartCalendarInterval = map (hour: {
Hour = hour;
Minute = 53;
}) (lib.range 1 23);
};
};
"git-maintenance-daily" = {
enable = true;
config = {
ProgramArguments = baseArguments ++ [ "--schedule=daily" ];
StartCalendarInterval = map (weekday: {
Weekday = weekday;
Hour = 0;
Minute = 53;
}) (lib.range 1 6);
};
};
"git-maintenance-weekly" = {
enable = true;
config = {
ProgramArguments = baseArguments ++ [ "--schedule=weekly" ];
StartCalendarInterval = [
{
Weekday = 0;
Hour = 0;
Minute = 53;
}
];
};
};
};
})
]
);
}