Skip to content

Commit cfc9f7b

Browse files
authored
Merge pull request #634 from leiserfg/prek
Support alternative `pre-commit` implementations like `prek`
2 parents 54df955 + c167450 commit cfc9f7b

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Given the following `flake.nix` example:
7777
config = self.checks.${system}.pre-commit-check.config;
7878
inherit (config) package configFile;
7979
script = ''
80-
${package}/bin/pre-commit run --all-files --config ${configFile}
80+
${pkgs.lib.getExe package} run --all-files --config ${configFile}
8181
'';
8282
in
8383
pkgs.writeShellScriptBin "pre-commit-run" script

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/pre-commit.nix

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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 -c ${cfg.configPath} --hook-stage manual --all-files
107+
${lib.getExe cfg.package} run -c ${cfg.configPath} --hook-stage manual --all-files
108108
else
109109
echo "Running: $ pre-commit run --all-files"
110-
pre-commit run -c ${cfg.configPath} --all-files
110+
${lib.getExe cfg.package} run -c ${cfg.configPath} --all-files
111111
fi
112112
exitcode=$?
113113
git --no-pager diff --color
@@ -144,7 +144,7 @@ in
144144
default = true;
145145
description = ''
146146
Whether to enable the pre-commit hooks module.
147-
147+
148148
When set to false, this disables the entire module.
149149
'';
150150
};
@@ -155,7 +155,7 @@ in
155155
default = true;
156156
description = ''
157157
Whether to enable automatic installation of pre-commit hooks.
158-
158+
159159
When set to false, hooks will not be installed into the git repository,
160160
but all other module functionality (like configuration generation) will still work.
161161
'';
@@ -441,12 +441,11 @@ in
441441

442442
installationScript =
443443
''
444-
export PATH=${cfg.package}/bin:$PATH
445444
if ${boolToString cfg.install.enable}; then
446445
if ! ${cfg.gitPackage}/bin/git rev-parse --git-dir &> /dev/null; then
447446
echo 1>&2 "WARNING: git-hooks.nix: .git not found; skipping installation."
448447
else
449-
GIT_WC=`${cfg.gitPackage}/bin/git rev-parse --show-toplevel`
448+
GIT_WC=`${lib.getExe cfg.gitPackage} rev-parse --show-toplevel`
450449
451450
# These update procedures compare before they write, to avoid
452451
# filesystem churn. This improves performance with watch tools like lorri
@@ -474,9 +473,9 @@ in
474473
# Remove any previously installed hooks (since pre-commit itself has no convergent design)
475474
hooks="${concatStringsSep " " (remove "manual" supportedHooksLib.supportedHooks )}"
476475
for hook in $hooks; do
477-
pre-commit uninstall -t $hook
476+
${lib.getExe cfg.package} uninstall -t $hook
478477
done
479-
${cfg.gitPackage}/bin/git config --local core.hooksPath ""
478+
${lib.getExe cfg.gitPackage} config --local core.hooksPath ""
480479
# Add hooks for configured stages (only) ...
481480
if [ ! -z "${concatStringsSep " " install_stages}" ]; then
482481
for stage in ${concatStringsSep " " install_stages}; do
@@ -486,10 +485,10 @@ in
486485
# if you amend these switches please also review $hooks above
487486
commit | merge-commit | push)
488487
stage="pre-"$stage
489-
pre-commit install -c ${cfg.configPath} -t $stage
488+
${lib.getExe cfg.package} install -c ${cfg.configPath} -t $stage
490489
;;
491490
${concatStringsSep "|" supportedHooksLib.supportedHooks})
492-
pre-commit install -c ${cfg.configPath} -t $stage
491+
${lib.getExe cfg.package} install -c ${cfg.configPath} -t $stage
493492
;;
494493
*)
495494
echo 1>&2 "ERROR: git-hooks.nix: either $stage is not a valid stage or git-hooks.nix doesn't yet support it."
@@ -499,7 +498,7 @@ in
499498
done
500499
# ... or default 'pre-commit' hook
501500
else
502-
pre-commit install -c ${cfg.configPath}
501+
${lib.getExe cfg.package} install -c ${cfg.configPath}
503502
fi
504503
505504
# Fetch the absolute path to the git common directory. This will normally point to $GIT_WC/.git.
@@ -508,7 +507,7 @@ in
508507
# Convert the absolute path to a path relative to the toplevel working directory.
509508
common_dir=''${common_dir#''$GIT_WC/}
510509
511-
${cfg.gitPackage}/bin/git config --local core.hooksPath "''$common_dir/hooks"
510+
${lib.getExe cfg.gitPackage} config --local core.hooksPath "''$common_dir/hooks"
512511
fi
513512
fi
514513
fi

0 commit comments

Comments
 (0)