Skip to content

always run git-hooks installation script #1751

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
45 changes: 30 additions & 15 deletions src/modules/integrations/git-hooks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ let
mkdir -p $out/bin
ln -s ${cfg.package}/bin/pre-commit $out/bin/pre-commit
'';

anyEnabled = ((lib.filterAttrs (id: value: value.enable) cfg.hooks) != { });
in
{
imports = [
Expand All @@ -40,20 +42,33 @@ in
description = "Integration with https://github.com/cachix/git-hooks.nix";
};

config = lib.mkIf ((lib.filterAttrs (id: value: value.enable) cfg.hooks) != { }) {
ci = [ cfg.run ];
# Add the packages for any enabled hooks at the end to avoid overriding the language-defined packages.
packages = lib.mkAfter ([ packageBin ] ++ (cfg.enabledPackages or [ ]));
tasks = {
# TODO: split installation script into status + exec
"devenv:git-hooks:install" = {
exec = cfg.installationScript;
before = [ "devenv:enterShell" ];
};
"devenv:git-hooks:run" = {
exec = "pre-commit run -a";
before = [ "devenv:enterTest" ];
config = lib.mkMerge [
(lib.mkIf (!anyEnabled) {
# Remove .pre-commit-config.yaml if it exists and is in the nix store
enterShell = ''
preCommitConfig="$DEVENV_ROOT/.pre-commit-config.yaml"
if $(nix-store --quiet --verify-path "$preCommitConfig" > /dev/null 2>&1); then
echo Removing "$preCommitConfig"
rm -rf "$preCommitConfig"
fi
'';
})

(lib.mkIf anyEnabled {
ci = [ cfg.run ];
# Add the packages for any enabled hooks at the end to avoid overriding the language-defined packages.
packages = lib.mkAfter ([ packageBin ] ++ (cfg.enabledPackages or [ ]));
tasks = {
# TODO: split installation script into status + exec
"devenv:git-hooks:install" = {
exec = cfg.installationScript;
before = [ "devenv:enterShell" ];
};
"devenv:git-hooks:run" = {
exec = "pre-commit run -a";
before = [ "devenv:enterTest" ];
};
};
};
};
})
];
}
6 changes: 6 additions & 0 deletions tests/git-hooks-all-disabled/.setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if ! test -f "$DEVENV_ROOT/.pre-commit-config.yaml"; then
echo "Test not setup correctly: .pre-commit-config.yaml not found"
exit 1
fi

echo "{ lib, ... }: { git-hooks.hooks.no-op.enable = lib.mkForce false; }" > devenv.local.nix
19 changes: 19 additions & 0 deletions tests/git-hooks-all-disabled/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Assert that .pre-commit-config.yaml is removed when all hooks are disabled
{
git-hooks.hooks = {
no-op = {
enable = true;
name = "No Op";
pass_filenames = false;
raw.always_run = true;
entry = "true";
};
};

enterTest = ''
if test -f "$DEVENV_ROOT/.pre-commit-config.yaml"; then
echo ".pre-commit-config.yaml not removed"
exit 1
fi
'';
}