From 55b98216505b209b09499cfa39b34a3d63bc9ca3 Mon Sep 17 00:00:00 2001 From: Sander Date: Sat, 24 Aug 2024 18:33:25 +0400 Subject: [PATCH] feat: register gc root for the generated .pre-commit-config.yaml --- modules/pre-commit.nix | 16 +++++++++++++++- nix/installation-test.nix | 2 +- nix/run.nix | 3 ++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/modules/pre-commit.nix b/modules/pre-commit.nix index 8e937a91..a5fae075 100644 --- a/modules/pre-commit.nix +++ b/modules/pre-commit.nix @@ -2,6 +2,7 @@ let inherit (lib) attrNames + boolToString concatStringsSep compare filterAttrs @@ -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 by hooks. + ''; + }; + assertions = lib.mkOption { type = types.listOf types.unspecified; internal = true; @@ -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}; 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 diff --git a/nix/installation-test.nix b/nix/installation-test.nix index 573a1441..2d79405d 100644 --- a/nix/installation-test.nix +++ b/nix/installation-test.nix @@ -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} diff --git a/nix/run.nix b/nix/run.nix index fad95901..4bff5a75 100644 --- a/nix/run.nix +++ b/nix/run.nix @@ -6,6 +6,7 @@ builtinStuff@{ pkgs, tools, isFlakes, pre-commit, git, runCommand, writeText, wr , excludes ? [ ] , tools ? { } , default_stages ? [ "commit" ] +, addGcRoot ? true , imports ? [ ] }: let @@ -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