-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.nix
More file actions
258 lines (236 loc) · 7.25 KB
/
git.nix
File metadata and controls
258 lines (236 loc) · 7.25 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
{
config,
inputs,
lib,
pkgs,
...
}:
let
inherit (builtins) concatStringsSep;
cfg = config.nixcfg.git;
identityCfg = cfg.identities;
includeType = lib.types.submodule {
options = {
path = lib.mkOption {
type = lib.types.str;
description = "Path to include in git config.";
};
condition = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "Optional git conditional include expression.";
};
};
};
mkInclude =
include:
{
inherit (include) path;
}
// lib.optionalAttrs (include.condition != null) {
inherit (include) condition;
};
batPackage = lib.attrByPath [
"programs"
"bat"
"package"
] pkgs.bat config;
# Delta resolves bat themes from bat's compiled cache, not directly from
# ~/.config/bat/themes. If that cache gets cleaned, git diff starts warning
# that the configured Catppuccin theme is unknown until `bat cache --build`
# is run again.
batCacheAwareDelta = pkgs.writeShellApplication {
name = "delta";
text = ''
set -eu
cache_home="''${XDG_CACHE_HOME:-$HOME/.cache}"
config_home="''${XDG_CONFIG_HOME:-$HOME/.config}"
cache_dir="$cache_home/bat"
config_dir="$config_home/bat"
themes_bin="$cache_dir/themes.bin"
syntaxes_bin="$cache_dir/syntaxes.bin"
needs_rebuild=0
if [ ! -f "$themes_bin" ] || [ ! -f "$syntaxes_bin" ]; then
needs_rebuild=1
fi
if [ "$needs_rebuild" -eq 0 ] && [ -d "$config_dir/themes" ] && \
${lib.getExe' pkgs.findutils "find"} "$config_dir/themes" -type f -newer "$themes_bin" | ${lib.getExe pkgs.gnugrep} -q .; then
needs_rebuild=1
fi
if [ "$needs_rebuild" -eq 0 ] && [ -d "$config_dir/syntaxes" ] && \
${lib.getExe' pkgs.findutils "find"} "$config_dir/syntaxes" -type f -newer "$syntaxes_bin" | ${lib.getExe pkgs.gnugrep} -q .; then
needs_rebuild=1
fi
if [ "$needs_rebuild" -eq 1 ]; then
${lib.getExe' pkgs.coreutils "mkdir"} -p "$cache_dir"
tmpdir="$(${lib.getExe' pkgs.coreutils "mktemp"} -d "''${TMPDIR:-/tmp}/delta-bat-cache.XXXXXX")"
cleanup() {
${lib.getExe' pkgs.coreutils "rm"} -rf "$tmpdir"
}
trap cleanup EXIT
(
export XDG_CACHE_HOME="$cache_home"
cd "$tmpdir"
${lib.getExe batPackage} cache --build >/dev/null 2>&1 || true
)
fi
exec ${lib.getExe pkgs.delta} "$@"
'';
};
in
{
options.nixcfg.git = {
enable = lib.mkEnableOption "opinionated git and delta configuration" // {
default = true;
};
signingKey = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "Signing key passed to git as user.signingkey when set.";
};
signCommitsByDefault = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Enable commit.gpgsign in git config.";
};
enableDelta = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Enable the delta pager with git integration.";
};
includeCatppuccinDeltaTheme = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Include the catppuccin delta theme snippet.";
};
ignores = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [
".DS_Store"
".direnv"
];
description = "Global git ignore entries.";
};
extraIncludes = lib.mkOption {
type = lib.types.listOf includeType;
default = [ ];
description = "Additional include entries appended to git includes.";
};
identities = lib.mkOption {
type = lib.types.attrsOf (
lib.types.submodule {
options = {
name = lib.mkOption {
type = lib.types.str;
description = "Git user name for this identity.";
};
email = lib.mkOption {
type = lib.types.str;
description = "Git email for this identity.";
};
conditions = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "Conditional include patterns (gitdir: format).";
};
};
}
);
default = { };
description = "Named git identities with conditional includes.";
};
};
config = lib.mkIf cfg.enable {
home.file = lib.mapAttrs' (
id: idConfig:
lib.nameValuePair "${config.xdg.configHome}/git/${id}" {
text = ''
[user]
name = ${idConfig.name}
email = ${idConfig.email}
'';
}
) identityCfg;
programs = {
delta = lib.mkIf cfg.enableDelta {
enable = true;
enableGitIntegration = true;
package = batCacheAwareDelta;
options = {
navigate = true;
side-by-side = true;
};
};
git = {
enable = true;
inherit (cfg) ignores;
includes =
lib.optionals cfg.includeCatppuccinDeltaTheme [
{ path = "${inputs.catppuccin-delta}/catppuccin.gitconfig"; }
]
++ lib.concatLists (
lib.mapAttrsToList (
id: idConfig:
map (condition: {
path = "${config.xdg.configHome}/git/${id}";
inherit condition;
}) idConfig.conditions
) identityCfg
)
++ map mkInclude cfg.extraIncludes;
lfs.enable = true;
settings = {
alias = {
branches =
let
format = concatStringsSep "\t" [
"%(color:red)%(ahead-behind:HEAD)"
"%(color:blue)%(refname:short)"
"%(color:yellow)%(committerdate:relative)"
"%(color:default)%(describe)"
];
header = concatStringsSep "," [
"Ahead"
"Behind"
"Branch Name"
"Last Commit"
"Description"
];
in
concatStringsSep " " [
"!git for-each-ref"
"--color"
"--sort=-committerdate"
"--format=$'${format}'"
"refs/heads/"
"--no-merged"
"|"
"${lib.getExe pkgs.gnused}"
"'s/ /\t/'"
"|"
"${pkgs.util-linux}/bin/column"
"--separator=$'\t'"
"--table"
"--table-columns='${header}'"
];
praise = "blame";
};
commit.gpgsign = cfg.signCommitsByDefault;
delta.features = config.theme.slug;
diff.colorMoved = "default";
fetch.prune = true;
init.defaultBranch = "main";
rebase.pull = true;
url."ssh://gitlab.gnome.org".insteadOf = "https://gitlab.gnome.org";
}
// lib.optionalAttrs (cfg.signingKey != null) {
user.signingkey = cfg.signingKey;
};
signing = {
format = lib.mkForce "openpgp";
signer = lib.getExe' pkgs.gnupg "gpg";
};
};
};
};
}