forked from cachix/git-hooks.nix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit.nix
412 lines (363 loc) · 13.2 KB
/
pre-commit.nix
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
{ config, lib, pkgs, hookModule, ... }:
let
inherit (lib)
attrNames
boolToString
concatStringsSep
compare
filterAttrs
literalExample
mapAttrsToList
mkIf
mkOption
types
remove
;
inherit (pkgs) runCommand writeText git;
cfg = config;
install_stages = lib.unique (builtins.concatLists (lib.mapAttrsToList (_: h: h.stages) enabledHooks));
supportedHooksLib = import ./supported-hooks.nix { inherit lib; };
hookType = types.submodule hookModule;
mergeExcludes =
excludes:
if excludes == [ ] then "^$" else "(${concatStringsSep "|" excludes})";
enabledHooks = filterAttrs (id: value: value.enable) cfg.hooks;
processedHooks =
mapAttrsToList (id: value: value.raw // { inherit id; }) enabledHooks;
configFile =
performAssertions (
runCommand "pre-commit-config.json"
{
buildInputs = [
pkgs.jq
# needs to be an input so we regenerate the config and reinstall the hooks
# when the package changes
cfg.package
];
passAsFile = [ "rawJSON" ];
rawJSON = builtins.toJSON cfg.rawConfig;
} ''
{
echo '# DO NOT MODIFY';
echo '# This file was generated by git-hooks.nix';
jq . <"$rawJSONPath"
} >$out
''
);
run =
runCommand "pre-commit-run" { buildInputs = [ git ]; } ''
set +e
HOME=$PWD
# Use `chmod +w` instead of `cp --no-preserve=mode` to be able to write and to
# preserve the executable bit at the same time
cp -R ${cfg.rootSrc} src
chmod -R +w src
ln -fs ${configFile} src/.pre-commit-config.yaml
cd src
rm -rf .git
git init -q
git add .
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
git commit -m "init" -q
if [[ ${toString (compare install_stages [ "manual" ])} -eq 0 ]]
then
echo "Running: $ pre-commit run --hook-stage manual --all-files"
${cfg.package}/bin/pre-commit run --hook-stage manual --all-files
else
echo "Running: $ pre-commit run --all-files"
${cfg.package}/bin/pre-commit run --all-files
fi
exitcode=$?
git --no-pager diff --color
touch $out
[ $? -eq 0 ] && exit $exitcode
'';
failedAssertions = builtins.map (x: x.message) (builtins.filter (x: !x.assertion) config.assertions);
performAssertions =
let
formatAssertionMessage = message:
let
lines = lib.splitString "\n" message;
in
"- ${lib.concatStringsSep "\n " lines}";
in
if failedAssertions != [ ]
then
throw ''
Failed assertions:
${lib.concatStringsSep "\n" (builtins.map formatAssertionMessage failedAssertions)}
''
else lib.trivial.showWarnings config.warnings;
in
{
options =
{
package =
mkOption {
type = types.package;
description =
''
The `pre-commit` package to use.
'';
defaultText =
lib.literalExpression or literalExample ''
pkgs.pre-commit
'';
};
tools =
mkOption {
type = types.lazyAttrsOf (types.nullOr types.package);
description =
''
Tool set from which `nix-pre-commit-hooks` will pick binaries.
`nix-pre-commit-hooks` comes with its own set of packages for this purpose.
'';
defaultText =
lib.literalExpression or literalExample ''git-hooks.nix-pkgs.callPackage tools-dot-nix { inherit (pkgs) system; }'';
};
enabledPackages = mkOption {
type = types.listOf types.unspecified;
description =
''
All packages provided by hooks that are enabled.
Useful for including into the developer environment.
'';
default = lib.pipe config.hooks [
builtins.attrValues
(lib.filter (hook: hook.enable))
(builtins.concatMap (hook:
(lib.optional (hook.package != null) hook.package)
++ hook.extraPackages
))
];
};
hooks =
mkOption {
type = types.submodule {
freeformType = types.attrsOf hookType;
};
description =
''
The hook definitions.
You can both specify your own hooks here and you can enable predefined hooks.
Example of enabling a predefined hook:
```nix
hooks.nixpkgs-fmt.enable = true;
```
Example of a custom hook:
```nix
hooks.my-tool = {
enable = true;
name = "my-tool";
description = "Run MyTool on all files in the project";
files = "\\.mtl$";
entry = "''${pkgs.my-tool}/bin/mytoolctl";
};
```
The predefined hooks are:
${
concatStringsSep
"\n"
(lib.mapAttrsToList
(hookName: hookConf:
''
**`${hookName}`**
${hookConf.description}
'')
config.hooks)
}
'';
default = { };
};
run =
mkOption {
type = types.package;
description =
''
A derivation that tests whether the pre-commit hooks run cleanly on
the entire project.
'';
readOnly = true;
default = run;
defaultText = "<derivation>";
};
installationScript =
mkOption {
type = types.str;
description =
''
A bash snippet that installs nix-pre-commit-hooks in the current directory
'';
readOnly = true;
};
src =
lib.mkOption {
description = ''
Root of the project. By default this will be filtered with the `gitignoreSource`
function later, unless `rootSrc` is specified.
If you use the `flakeModule`, the default is `self.outPath`; the whole flake
sources.
'';
type = lib.types.path;
};
rootSrc =
mkOption {
type = types.path;
description =
''
The source of the project to be checked.
This is used in the derivation that performs the check.
If you use the `flakeModule`, the default is `self.outPath`; the whole flake
sources.
'';
defaultText = lib.literalExpression or literalExample ''gitignoreSource config.src'';
};
excludes =
mkOption {
type = types.listOf types.str;
description =
''
Exclude files that were matched by these patterns.
'';
default = [ ];
};
default_stages =
mkOption {
type = supportedHooksLib.supportedHooksType;
description =
''
A configuration wide option for the stages property.
Installs hooks to the defined stages.
See [https://pre-commit.com/#confining-hooks-to-run-at-certain-stages](https://pre-commit.com/#confining-hooks-to-run-at-certain-stages).
'';
default = [ "pre-commit" ];
};
rawConfig =
mkOption {
type = types.attrs;
description =
''
The raw configuration before writing to file.
This option does not have an appropriate merge function.
It is accessible in case you need to set an attribute that doesn't have an option.
'';
internal = true;
};
addGcRoot = mkOption {
type = types.bool;
default = true;
description = ''
Whether to add the generated pre-commit-config.yaml to the garbage collector roots.
This prevents Nix from garbage-collecting the tools used by hooks.
'';
};
assertions = lib.mkOption {
type = types.listOf types.unspecified;
internal = true;
default = [ ];
example = [{ assertion = false; message = "you can't enable this for that reason"; }];
description = ''
This option allows modules to express conditions that must
hold for the evaluation of the configuration to succeed,
along with associated error messages for the user.
'';
};
warnings = lib.mkOption {
type = types.listOf types.str;
internal = true;
default = [ ];
example = [ "you should fix this or that" ];
description = ''
This option allows modules to express warnings about the
configuration. For example, `lib.mkRenamedOptionModule` uses this to
display a warning message when a renamed option is used.
'';
};
};
config =
{
rawConfig =
{
repos =
[
{
repo = "local";
hooks = processedHooks;
}
];
} // lib.optionalAttrs (cfg.excludes != [ ]) {
exclude = mergeExcludes cfg.excludes;
} // lib.optionalAttrs (cfg.default_stages != [ ]) {
default_stages = cfg.default_stages;
};
installationScript =
''
export PATH=${cfg.package}/bin:$PATH
if ! type -t git >/dev/null; then
# This happens in pure shells, including lorri
echo 1>&2 "WARNING: git-hooks.nix: git command not found; skipping installation."
elif ! ${git}/bin/git rev-parse --git-dir &> /dev/null; then
echo 1>&2 "WARNING: git-hooks.nix: .git not found; skipping installation."
else
GIT_WC=`${git}/bin/git rev-parse --show-toplevel`
# These update procedures compare before they write, to avoid
# filesystem churn. This improves performance with watch tools like lorri
# and prevents installation loops by lorri.
if ! readlink "''${GIT_WC}/.pre-commit-config.yaml" >/dev/null \
|| [[ $(readlink "''${GIT_WC}/.pre-commit-config.yaml") != ${configFile} ]]; then
echo 1>&2 "git-hooks.nix: updating $PWD repo"
[ -L .pre-commit-config.yaml ] && unlink .pre-commit-config.yaml
if [ -e "''${GIT_WC}/.pre-commit-config.yaml" ]; then
echo 1>&2 "git-hooks.nix: WARNING: Refusing to install because of pre-existing .pre-commit-config.yaml"
echo 1>&2 " 1. Translate .pre-commit-config.yaml contents to the new syntax in your Nix file"
echo 1>&2 " see https://github.com/cachix/git-hooks.nix#getting-started"
echo 1>&2 " 2. remove .pre-commit-config.yaml"
echo 1>&2 " 3. add .pre-commit-config.yaml to .gitignore"
else
if ${boolToString cfg.addGcRoot}; then
nix-store --add-root "''${GIT_WC}/.pre-commit-config.yaml" --indirect --realise ${configFile}
else
ln -fs ${configFile} "''${GIT_WC}/.pre-commit-config.yaml"
fi
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
hooks="${concatStringsSep " " (remove "manual" supportedHooksLib.supportedHooks )}"
for hook in $hooks; do
pre-commit uninstall -t $hook
done
${git}/bin/git config --local core.hooksPath ""
# Add hooks for configured stages (only) ...
if [ ! -z "${concatStringsSep " " install_stages}" ]; then
for stage in ${concatStringsSep " " install_stages}; do
case $stage in
manual)
;;
# if you amend these switches please also review $hooks above
commit | merge-commit | push)
stage="pre-"$stage
pre-commit install -t $stage
;;
${concatStringsSep "|" supportedHooksLib.supportedHooks})
pre-commit install -t $stage
;;
*)
echo 1>&2 "ERROR: git-hooks.nix: either $stage is not a valid stage or git-hooks.nix doesn't yet support it."
exit 1
;;
esac
done
# ... or default 'pre-commit' hook
else
pre-commit install
fi
# Fetch the absolute path to the git common directory. This will normally point to $GIT_WC/.git.
common_dir=''$(${git}/bin/git rev-parse --path-format=absolute --git-common-dir)
# Convert the absolute path to a path relative to the toplevel working directory.
common_dir=''${common_dir#''$GIT_WC/}
${git}/bin/git config --local core.hooksPath "''$common_dir/hooks"
fi
fi
fi
'';
};
}