Skip to content

Commit 55b9821

Browse files
committed
feat: register gc root for the generated .pre-commit-config.yaml
1 parent c8a5405 commit 55b9821

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

Diff for: modules/pre-commit.nix

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
let
33
inherit (lib)
44
attrNames
5+
boolToString
56
concatStringsSep
67
compare
78
filterAttrs
@@ -281,6 +282,15 @@ in
281282
internal = true;
282283
};
283284

285+
addGcRoot = mkOption {
286+
type = types.bool;
287+
default = true;
288+
description = ''
289+
Whether to add the generated pre-commit-config.yaml to the garbage collector roots.
290+
This prevents Nix from garbage-collecting the tools used by hooks.
291+
'';
292+
};
293+
284294
assertions = lib.mkOption {
285295
type = types.listOf types.unspecified;
286296
internal = true;
@@ -351,7 +361,11 @@ in
351361
echo 1>&2 " 2. remove .pre-commit-config.yaml"
352362
echo 1>&2 " 3. add .pre-commit-config.yaml to .gitignore"
353363
else
354-
ln -fs ${configFile} "''${GIT_WC}/.pre-commit-config.yaml"
364+
if ${boolToString cfg.addGcRoot}; then
365+
nix-store --add-root "''${GIT_WC}/.pre-commit-config.yaml" --indirect --realise ${configFile}
366+
else
367+
ln -fs ${configFile} "''${GIT_WC}/.pre-commit-config.yaml"
368+
fi
355369
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
356370
hooks="${concatStringsSep " " (remove "manual" supportedHooksLib.supportedHooks )}"
357371
for hook in $hooks; do

Diff for: nix/installation-test.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ let
6161

6262
executeTest = lib.mapAttrsToList
6363
(name: test:
64-
let runDerivation = run ({ src = null; } // test.conf);
64+
let runDerivation = run ({ src = null; addGcRoot = false; } // test.conf);
6565
in ''
6666
rm -f ~/.git/hooks/*
6767
${runDerivation.shellHook}

Diff for: nix/run.nix

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ builtinStuff@{ pkgs, tools, isFlakes, pre-commit, git, runCommand, writeText, wr
66
, excludes ? [ ]
77
, tools ? { }
88
, default_stages ? [ "commit" ]
9+
, addGcRoot ? true
910
, imports ? [ ]
1011
}:
1112
let
@@ -19,7 +20,7 @@ let
1920
{
2021
_module.args.pkgs = pkgs;
2122
_module.args.gitignore-nix-src = gitignore-nix-src;
22-
inherit hooks excludes default_stages settings;
23+
inherit hooks excludes default_stages settings addGcRoot;
2324
tools = builtinStuff.tools // tools;
2425
package = pre-commit;
2526
} // (if isFlakes

0 commit comments

Comments
 (0)