3.1 KiB
3.1 KiB
Git Hooks - Simple Declarative Setup
Concept
Hooks are defined in Nix (flake.nix) and installed by running nix flake check once.
No systemd services, no activation scripts, no complexity.
How It Works
1. Hooks Defined in Nix
flake.nix:
checks.${system}.pre-commit-check = git-hooks.lib.${system}.run {
src = ./.;
hooks = {
nixfmt.enable = true;
statix.enable = true;
deadnix.enable = true;
};
};
2. Installation
Run once on each host:
nix flake check
This installs the hooks and creates .git/hooks/pre-commit.
3. Automatic
- ✅ Hooks run on every
git commit - ✅ CI runs
nix flake checkautomatically - ✅ Hooks checked on every push/PR
Usage
Install Hooks (One-Time Per Host)
# From the flake directory
nix flake check
# You should see hooks installing
Verify Installation
ls -la .git/hooks/
Should show pre-commit (and potentially other hooks).
Test Hooks
# Create a file with bad formatting
echo "broken { }" > test.nix
# Try to commit (should fail)
git add test.nix
git commit -m "test"
# Clean up
rm test.nix
What's Declarative
| Aspect | Status |
|---|---|
| Hook definitions | ✅ Yes - in flake.nix |
| Hook installation | ✅ Yes - via nix flake check |
| CI integration | ✅ Yes - via nix flake check in workflows |
| Local git hooks | ✅ Yes - run automatically on commit |
| No systemd services | ✅ Removed - too complex |
| No activation scripts | ✅ Removed - unnecessary |
| One-time setup | ✅ Yes - run nix flake check once per host |
Files
| File | Status |
|---|---|
flake.nix |
✅ Hook definitions |
.github/workflows/flake-check.yaml |
✅ CI uses nix flake check |
.editorconfig |
✅ Code style |
modules/git-hooks/default.nix |
❌ DELETED - Not needed |
hosts/*/default.nix |
✅ CLEANED - Removed git-hooks |
Next Steps
-
Test locally:
nix flake check ls -la .git/hooks/ echo "broken { }" > test.nix git add test.nix git commit -m "test" # Should fail rm test.nix -
Commit changes:
git add . git commit -m "Simplify: Git hooks via nix flake check (no systemd, no activation)" git push -
Run
nix flake checkon each host when you next rebuild
This Is The Right Approach Because
| Issue | Overcomplicated Solution | Simple Solution |
|---|---|---|
| Declarative | ❌ Systemd service is separate from Nix | ✅ Hooks in flake.nix, install via nix flake check |
| Simple | ❌ Multiple layers (activation, systemd) | ✅ One command: nix flake check |
| Idempotent | ❌ Runs on every rebuild | ✅ Idempotent - run once per host |
| Reproducible | ❌ Depends on systemd state | ✅ Pure Nix |
Summary
The simplest declarative approach:
- Define hooks in
flake.nix✅ Already done - Run
nix flake checkonce per host ✅ To do - That's it! Hooks work automatically ✅ Declarative
No systemd services. No activation scripts. No complexity.