Skip to content

feat: update pre-commit.nix to be "run" customizable #494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions modules/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,27 @@ let
mkDerivation {
name = "pre-commit-run";
src = cfg.rootSrc;
buildInputs = [ cfg.gitPackage ];
buildInputs = [ cfg.gitPackage cfg.package ];
nativeBuildInputs = enabledExtraPackages
++ lib.optional (config.settings.rust.check.cargoDeps != null) cargoSetupHook;
cargoDeps = config.settings.rust.check.cargoDeps;
buildPhase = ''
set +e
# Set HOME to a temporary directory for pre-commit to create its cache files in.
HOME=$(mktemp -d)
ln -fs ${configFile} .pre-commit-config.yaml
ln -fs ${cfg.configFile} .pre-commit-config.yaml
git init -q
git add .
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
git commit -m "init" -q
if [[ ${toString (compare install_stages [ "manual" ])} -eq 0 ]]
if [[ ${toString (compare cfg.installStages [ "manual" ])} -eq 0 ]]
then
echo "Running: $ pre-commit run --hook-stage manual --all-files"
${cfg.package}/bin/pre-commit run --hook-stage manual --all-files
pre-commit run --hook-stage manual --all-files
else
echo "Running: $ pre-commit run --all-files"
${cfg.package}/bin/pre-commit run --all-files
pre-commit run --all-files
fi
exitcode=$?
git --no-pager diff --color
Expand Down Expand Up @@ -254,7 +254,7 @@ in
A derivation that tests whether the pre-commit hooks run cleanly on
the entire project.
'';
readOnly = true;
readOnly = false;
default = run;
defaultText = lib.literalExpression "<derivation>";
};
Expand All @@ -269,6 +269,18 @@ in
readOnly = true;
};

configFile =
mkOption {
type = types.package;
description =
''
The pre-commit configuration file.
'';
readOnly = true;
default = configFile;
defaultText = "<derivation>";
};

src =
lib.mkOption {
description = ''
Expand Down Expand Up @@ -363,6 +375,15 @@ in
display a warning message when a renamed option is used.
'';
};

installStages = lib.mkOption {
type = types.listOf (types.either types.str types.anything);
description = ''
The stages to install the hooks to.
'';
default = install_stages;
readOnly = true;
};
};

config =
Expand Down