Skip to content

Commit

Permalink
feat: register gc root for the generated .pre-commit-config.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
sandydoo committed Aug 24, 2024
1 parent c8a5405 commit 7f88be4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
16 changes: 15 additions & 1 deletion modules/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
let
inherit (lib)
attrNames
boolToString
concatStringsSep
compare
filterAttrs
Expand Down Expand Up @@ -281,6 +282,15 @@ in
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 in the hooks.
'';
};

assertions = lib.mkOption {
type = types.listOf types.unspecified;
internal = true;
Expand Down Expand Up @@ -351,7 +361,11 @@ in
echo 1>&2 " 2. remove .pre-commit-config.yaml"
echo 1>&2 " 3. add .pre-commit-config.yaml to .gitignore"
else
ln -fs ${configFile} "''${GIT_WC}/.pre-commit-config.yaml"
if [ "${boolToString cfg.addGcRoot}" = "true" ]; then
nix-store --add-root "''${GIT_WC}/.pre-commit-config.yaml" --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
Expand Down
4 changes: 2 additions & 2 deletions nix/installation-test.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Test that checks whether the correct hooks are created in the hooks folder.
{ git, perl, coreutils, runCommand, run, lib, mktemp }:
{ git, perl, nix, coreutils, runCommand, run, lib, mktemp }:
let
tests = {
basic-test = {
Expand Down Expand Up @@ -61,7 +61,7 @@ let

executeTest = lib.mapAttrsToList
(name: test:
let runDerivation = run ({ src = null; } // test.conf);
let runDerivation = run ({ src = null; addGcRoot = false; } // test.conf);
in ''
rm -f ~/.git/hooks/*
${runDerivation.shellHook}
Expand Down
3 changes: 2 additions & 1 deletion nix/run.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ builtinStuff@{ pkgs, tools, isFlakes, pre-commit, git, runCommand, writeText, wr
, excludes ? [ ]
, tools ? { }
, default_stages ? [ "commit" ]
, addGcRoot ? true
, imports ? [ ]
}:
let
Expand All @@ -19,7 +20,7 @@ let
{
_module.args.pkgs = pkgs;
_module.args.gitignore-nix-src = gitignore-nix-src;
inherit hooks excludes default_stages settings;
inherit hooks excludes default_stages settings addGcRoot;
tools = builtinStuff.tools // tools;
package = pre-commit;
} // (if isFlakes
Expand Down

0 comments on commit 7f88be4

Please sign in to comment.