git-hooks.nix
git-hooks.nix copied to clipboard
Uninstall pre-commit hooks when they are disabled
In the shell code, if the user removed all hooks, uninstall the hook.
For this to work reliably, we'd need to mark that it was installed by pre-commit-hooks.nix
This would be detectable if we customize the hook installation more. Nixpkgs already patches that code, but the gc root management isn't up to par yet. We may also want to move the config file from <proj>/.pre-commit-hooks.yaml to <proj>/.git/hooks/.pre-commit-hooks-nix.yaml + <proj>/.git/hooks/.pre-commit-bin for the program. All reasons to patch the hook generation code, and all detectable just by reading the generated hook script <proj>/.git/hooks/<hook>.
Currently it looks like
#!/nix/store/znkypmyvykawwg71xawqzb98qbllijv8-bash-5.1-p16/bin/bash
# File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03
# start templated
INSTALL_PYTHON=/nix/store/9srs642k875z3qdk8glapjycncf2pa51-python3-3.10.7/bin/python3.10
ARGS=(hook-impl --config=.pre-commit-config.yaml --hook-type=pre-commit)
# end templated
HERE="$(cd "$(dirname "$0")" && pwd)"
ARGS+=(--hook-dir "$HERE" -- "$@")
exec /nix/store/5d25296lls7rvrqc62jxvcwsyg60q4ag-python3.10-pre-commit-2.20.0/bin/pre-commit "${ARGS[@]}"
The last line only tells us that pre-commit from Nixpkgs was used.
If we change that to a local gcroot symlink in the hooks directory, we'll know that we manage it.
Sound good to me!