Skip to content

Commit 4a709a8

Browse files
authored
Merge pull request cachix#563 from cachix/fix/pre-commit-config-file-option
Add a `configPath` option
2 parents b6ee9db + 37eb734 commit 4a709a8

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

modules/pre-commit.nix

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ let
9595
set +e
9696
# Set HOME to a temporary directory for pre-commit to create its cache files in.
9797
HOME=$(mktemp -d)
98-
ln -fs ${cfg.configFile} .pre-commit-config.yaml
98+
ln -fs ${cfg.configFile} ${cfg.configPath}
9999
git init -q
100100
git add .
101101
git config --global user.email "you@example.com"
@@ -104,10 +104,10 @@ let
104104
if [[ ${toString (compare cfg.installStages [ "manual" ])} -eq 0 ]]
105105
then
106106
echo "Running: $ pre-commit run --hook-stage manual --all-files"
107-
pre-commit run --hook-stage manual --all-files
107+
pre-commit run -c ${cfg.configPath} --hook-stage manual --all-files
108108
else
109109
echo "Running: $ pre-commit run --all-files"
110-
pre-commit run --all-files
110+
pre-commit run -c ${cfg.configPath} --all-files
111111
fi
112112
exitcode=$?
113113
git --no-pager diff --color
@@ -281,6 +281,15 @@ in
281281
defaultText = "<derivation>";
282282
};
283283

284+
configPath = lib.mkOption {
285+
type = types.str;
286+
default = ".pre-commit-config.yaml";
287+
description =
288+
''
289+
The path to the generated pre-commit configuration file.
290+
'';
291+
};
292+
284293
src =
285294
lib.mkOption {
286295
description = ''
@@ -347,7 +356,7 @@ in
347356
type = types.bool;
348357
default = true;
349358
description = ''
350-
Whether to add the generated pre-commit-config.yaml to the garbage collector roots.
359+
Whether to add the generated pre-commit config to the garbage collector roots.
351360
This prevents Nix from garbage-collecting the tools used by hooks.
352361
'';
353362
};
@@ -419,22 +428,22 @@ in
419428
# filesystem churn. This improves performance with watch tools like lorri
420429
# and prevents installation loops by lorri.
421430
422-
if ! readlink "''${GIT_WC}/.pre-commit-config.yaml" >/dev/null \
423-
|| [[ $(readlink "''${GIT_WC}/.pre-commit-config.yaml") != ${configFile} ]]; then
431+
if ! readlink "''${GIT_WC}/${cfg.configPath}" >/dev/null \
432+
|| [[ $(readlink "''${GIT_WC}/${cfg.configPath}") != ${cfg.configFile} ]]; then
424433
echo 1>&2 "git-hooks.nix: updating $PWD repo"
425-
[ -L .pre-commit-config.yaml ] && unlink .pre-commit-config.yaml
434+
[ -L ${cfg.configPath} ] && unlink ${cfg.configPath}
426435
427-
if [ -e "''${GIT_WC}/.pre-commit-config.yaml" ]; then
428-
echo 1>&2 "git-hooks.nix: WARNING: Refusing to install because of pre-existing .pre-commit-config.yaml"
429-
echo 1>&2 " 1. Translate .pre-commit-config.yaml contents to the new syntax in your Nix file"
436+
if [ -e "''${GIT_WC}/${cfg.configPath}" ]; then
437+
echo 1>&2 "git-hooks.nix: WARNING: Refusing to install because of pre-existing ${cfg.configPath}"
438+
echo 1>&2 " 1. Translate ${cfg.configPath} contents to the new syntax in your Nix file"
430439
echo 1>&2 " see https://github.com/cachix/git-hooks.nix#getting-started"
431-
echo 1>&2 " 2. remove .pre-commit-config.yaml"
432-
echo 1>&2 " 3. add .pre-commit-config.yaml to .gitignore"
440+
echo 1>&2 " 2. remove ${cfg.configPath}"
441+
echo 1>&2 " 3. add ${cfg.configPath} to .gitignore"
433442
else
434443
if ${boolToString cfg.addGcRoot}; then
435-
nix-store --add-root "''${GIT_WC}/.pre-commit-config.yaml" --indirect --realise ${configFile}
444+
nix-store --add-root "''${GIT_WC}/${cfg.configPath}" --indirect --realise ${cfg.configFile}
436445
else
437-
ln -fs ${configFile} "''${GIT_WC}/.pre-commit-config.yaml"
446+
ln -fs ${cfg.configFile} "''${GIT_WC}/${cfg.configPath}"
438447
fi
439448
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
440449
hooks="${concatStringsSep " " (remove "manual" supportedHooksLib.supportedHooks )}"
@@ -451,10 +460,10 @@ in
451460
# if you amend these switches please also review $hooks above
452461
commit | merge-commit | push)
453462
stage="pre-"$stage
454-
pre-commit install -t $stage
463+
pre-commit install -c ${cfg.configPath} -t $stage
455464
;;
456465
${concatStringsSep "|" supportedHooksLib.supportedHooks})
457-
pre-commit install -t $stage
466+
pre-commit install -c ${cfg.configPath} -t $stage
458467
;;
459468
*)
460469
echo 1>&2 "ERROR: git-hooks.nix: either $stage is not a valid stage or git-hooks.nix doesn't yet support it."
@@ -464,7 +473,7 @@ in
464473
done
465474
# ... or default 'pre-commit' hook
466475
else
467-
pre-commit install
476+
pre-commit install -c ${cfg.configPath}
468477
fi
469478
470479
# Fetch the absolute path to the git common directory. This will normally point to $GIT_WC/.git.

0 commit comments

Comments
 (0)