Repository navigation
feat(flake): generate .pre-commit-config.yaml from one flake hook definition, consumer-extensible - #908
Conversation
RED: nix/hooks.nix and the lib.hooksPortable / mkProjectShell hooks surface do not exist yet, so the fidelity and consumer-surface tests fail. The zero-hooks parity tests pin today's behavior. Refs: #883
…consumer surface nix/hooks.nix defines every pre-commit hook once and renders three artifacts: the sandbox-pure checks.pre-commit gate (unchanged rawConfig), the PATH-portable committed .pre-commit-config.yaml + scaffold copy (drift-gated via lib.hooksPortable), and an opt-in consumer surface — mkProjectShell gains hooks (per-hook toggle/override/custom hooks) and hooksExcludes (global excludes) wired through git-hooks.nix, whose shellHook installs the rendered config on shell entry. The zero-hooks shell derivation is byte-identical to the previous builder. Refs: #883
…ffold example docs/NIX.md supersedes the two-artifact section with the three-render model; docs/MIGRATION.md documents the opt-in generation contract (preserved YAML never overwritten, gitignore the generated file, #885 manifest opt-out cross-reference); the scaffold consumer flake gains a commented hooks example; scripts/manifest.toml drops the .pre-commit-config.yaml transform chain (the fidelity test is the single agreement mechanism); CHANGELOG Unreleased/Added entry + mirror. Refs: #883
c-vigo
left a comment
There was a problem hiding this comment.
Opting in silently rewires core.hooksPath away from the scaffold's .githooks — undocumented behavior loss.
From the pinned git-hooks.nix (3bbec39, modules/pre-commit.nix installationScript): once a consumer completes the migration (deletes the preserved YAML), the first shell entry runs, inside the install branch:
git config --local --unset-all core.hooksPath || true
prek install -c .pre-commit-config.yaml # pre-commit stage only (install_stages)
git config --local core.hooksPath "$common_dir/hooks"
For a scaffold consumer this is a functional loss, not just churn:
setup-git-conf.shwirescore.hooksPath .githooks; after opt-in the.githooksshims stop running — the sanctioned-environment guard (IN_CONTAINER/IN_NIX_SHELL) on all three stages and any consumer-owned.githooksscripts are silently disabled. Only apre-commit-stage hook lands in.git/hooks(every consumer-profile hook has default stages, soinstall_stages == ["pre-commit"]).- It flaps: the install block is guarded by config-symlink staleness, so a container rebuild re-points
core.hooksPathto.githooks(setup-git-conf.sh) and it stays there until the next toolchain bump rewires it back to.git/hooks. Which hook mechanism governs depends on rebuild-vs-bump ordering. - Opting back out (removing
hooks) never restores the.githookswiring.
docs/MIGRATION.md documents only the never-overwrite guarantee (verified — the refusal branch does leave hooksPath alone). Either document the rewiring loudly in the MIGRATION contract (what is lost, how to restore .githooks), or avoid the fight: install.enable = false on the git-hooks.nix run plus a minimal shellHook that only maintains the config symlink keeps .githooks the single hook entry point — .githooks/pre-commit already runs prek run, which picks up the generated config.
Minor, take or leave:
- Fidelity gate: verified it trips on a hook-arg edit and on hook removal in either YAML; note it only compares
exclude/default_language_version/hooks — a hand-added top-level key (e.g.fail_fast: true) and hook order pass unnoticed. - Needs a rebase for the routine add/add CHANGELOG (+ mirror) conflict vs current dev.
Reproduced green: rawConfig byte-identical vs dev, nix build .#checks.x86_64-linux.pre-commit passes, zero-hooks drvPath identical to dev tip, settings.pattern override keeps --branch __none__ (per-leaf merge), sync-manifest clean after the transform-chain removal.
…rated-hooks # Conflicts: # CHANGELOG.md # assets/workspace/.devcontainer/CHANGELOG.md
Disable git-hooks.nix's installation script (install.enable = false) on the consumer hooks run and own the shellHook snippet: it only maintains the .pre-commit-config.yaml -> store symlink (staleness check, refusal on a regular file, GC root via nix-store when available). The stock script unset/reset core.hooksPath and installed only the pre-commit stage into .git/hooks, silently bypassing the scaffold's .githooks entry point and its sanctioned-environment guard (PR #908 review). .githooks/pre-commit runs prek, which reads the repo-root config, so the generated hooks execute through the existing wiring. Refs: #883
The direnv/`nix develop` path never set `core.hooksPath`, so commit-time hooks were silently inactive until the consumer set it by hand. Add a `TestGithooksPathWiring` suite asserting the base dev-shell shellHook wires `core.hooksPath` -> `.githooks`, guards on a scaffold-shaped `.githooks/` dir and the main worktree, and never unsets it. Refine the #908 opt-in guard to allow the sanctioned `.githooks` set while still rejecting any other hooksPath mutation. Refs: #1112
In direnv / `nix develop` mode the dev-shell never set `core.hooksPath`, so commit-time hooks (pre-commit / commit-msg via prek) were silently inactive until the consumer set it by hand — a local commit could bypass the gate with only CI catching it later. Devcontainer mode already wired it in setup-git-conf.sh. mkProjectShell's base shellHook now runs `git config core.hooksPath .githooks` on shell entry, mirroring the devcontainer and reinforcing the `.githooks` single-entry-point invariant (#908) — it only ever sets `.githooks`, never unsets/resets it or installs into `.git/hooks`. It lives in the shared base so the default `hooks = null` shell and an opted-in shell change identically, keeping the zero-hooks parity. Guarded to a scaffold-shaped repo (`.githooks/` at the git toplevel) and to the main worktree (worktree git-dir == common git-dir), so it leaves non-scaffold consumers untouched and never fights the worktree flow (justfile.worktree deliberately unsets core.hooksPath and installs prek hooks directly in a linked worktree); idempotent on re-entry. Refs: #1112
) ## Description In direnv mode the scaffold never set `core.hooksPath`, so commit-time hooks (pre-commit / commit-msg via prek) were silently inactive until the consumer set it by hand — a local commit could bypass the gate, with only CI catching violations later. Devcontainer mode already wires it (`setup-git-conf.sh` runs `git config core.hooksPath .githooks`). The dev shell now mirrors that: entering the direnv / `nix develop` shell wires `core.hooksPath` → `.githooks` automatically. Closes #1112 ## Type of Change - [ ] `feat` -- New feature - [x] `fix` -- Bug fix - [ ] `docs` -- Documentation only - [ ] `chore` -- Maintenance task (deps, config, etc.) - [ ] `refactor` -- Code restructuring (no behavior change) - [ ] `test` -- Adding or updating tests - [ ] `ci` -- CI/CD pipeline changes - [ ] `build` -- Build system or dependency changes - [ ] `revert` -- Reverts a previous commit - [ ] `style` -- Code style (formatting, whitespace) ### Modifiers - [ ] Breaking change (`!`) -- This change breaks backward compatibility ## Changes Made - `flake.nix`: new `githooksPathHook` fragment in `mkProjectShell`'s shared base shellHook. Implemented here (not in the scaffolded `.envrc`) so every direnv consumer gets it on the next flake-pin bump without a re-scaffold, and the devkit's own `devShells.default` exercises it. Three guards, each justified in the code comment: 1. `.githooks/` must exist at the git toplevel — only scaffold-shaped repos are touched; 2. **main worktree only** (worktree git-dir == common git-dir) — a linked worktree is owned by `justfile.worktree`, which deliberately unsets `core.hooksPath` and installs prek hooks directly; this guard keeps the shellHook from fighting that flow; 3. only when the value differs from `.githooks` — idempotent on re-entry. The hook only ever **sets** `.githooks` — never unsets/resets it or installs into `.git/hooks` — reinforcing the #908 invariant that `.githooks` stays the single entry point. - `tests/test_flake_hooks.py`: new `TestGithooksPathWiring` suite (wiring present, `.githooks`-dir guard, main-worktree guard, never unsets) + the existing #908 guard test refined to allow the sanctioned `.githooks` write while still rejecting any other `core.hooksPath` mutation. - `docs/COMMIT_MESSAGE_STANDARD.md` (SSoT; `assets/workspace/` mirror regenerated by `sync-manifest`): updated the "if `core.hooksPath` is unset the guard is silently absent" caveat — both sanctioned environments now wire it automatically. ## Changelog Entry ### Fixed - **direnv dev-shell wires `core.hooksPath` → `.githooks` automatically** ([#1112](#1112)) - Devcontainer mode set the hook entry point during setup, but a direnv / `nix develop` consumer never got it, so commit-time hooks (pre-commit / commit-msg via prek) were silently inactive until set by hand. - `mkProjectShell`'s base shellHook now sets it on shell entry, guarded to scaffold-shaped repos (a `.githooks/` dir), the main worktree (linked worktrees stay owned by `justfile.worktree`'s direct prek install), and idempotent re-entry; it only ever sets the sanctioned `.githooks` value (#908 invariant). ## Testing - [x] Tests pass locally (`just test`) - [x] Manual testing performed (describe below) ### Manual Testing Details - TDD: `test_default_shellhook_sets_core_hookspath_to_githooks` committed failing (RED), turned GREEN by the fix. - `tests/test_flake_hooks.py` + `tests/test_flake_devshell.py`: 38 passed, 1 skipped (FHS-only leak guard, N/A on NixOS host) — implementation run + independent review run, re-run after merging current `dev` (post podman-DooD flake change). - End-to-end against temp repos: main worktree + `.githooks` → set; linked worktree → left unset; no `.githooks` → untouched; re-entry → idempotent. - Tier-0 flake gates `nix run .#nix-fast-build -- --flake ".#checks.x86_64-linux"` → exit 0 (includes treefmt/nixfmt + dev-shell build + git-hooks check). Downstream scaffold-stub `nix flake check ./assets/workspace --override-input vigos "path:$PWD"` → all checks passed. - `prek run --all-files`: green. ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have updated the documentation accordingly (edit `docs/templates/`, then run `just docs`) - [x] I have updated `CHANGELOG.md` in the `[Unreleased]` section (and pasted the entry above) - [x] My changes generate no new warnings or errors - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] Any dependent changes have been merged and published ## Additional Notes Part of the commit-action-deployment bug batch. Shared-config semantics note: `core.hooksPath` lives in the repo's shared config, so entering the main-worktree dev shell sets it repo-wide; in a linked worktree the `.githooks` entry point then also runs `prek run`, so hooks stay enforced either way — no functional regression, just an observation. Refs: #1112
Description
Makes the flake the single definition of the pre-commit hook set and exposes it as consumer-extensible config on
mkProjectShell, per the #883 spec.nix/hooks.nixdefines every hook exactly once and renders three artifacts:checks.pre-commit— the sandbox-pure gate (git-hooks.nix +prek). Refactor-verified: the gate'srawConfigis byte-identical to the pre-refactor baseline..pre-commit-config.yamland the scaffold copy. They stay committed (portable, no store-path churn), but are no longer hand-synced:tests/test_flake_hooks.pydiffs the render (nix eval .#lib.hooksPortable) against both files (normalized: every hook id, args, files, excludes, stages) and fails CI on any drift. Thesync-manifesttransform chain for the scaffold copy is retired (scripts/manifest.toml) — collapse, not a fourth representation. The sandbox-impure subset (generators,pymarkdown, stage hooks, …) is included as render-only entries, so the normalized diff is empty — no residual.mkProjectShellgains opt-inhooks(toggle base hooks, per-hookexcludes/overrides via module-system merge, fully custom hooks with the full pre-commit schema) andhooksExcludes(global excludes). Opting in wires a config-only snippet into theshellHook: entering the shell installs the rendered.pre-commit-config.yaml(gitignored symlink, regenerated on toolchain bumps) and touches nothing else — git-hooks.nix's own installation script is disabled (install.enable = false), socore.hooksPathand.git/hooksare never modified and the scaffold's.githooksstays the single hook entry point.Consumer contract (docs/MIGRATION.md): generation is opt-in; a preserved hand-edited YAML (#878) is never overwritten — git-hooks.nix refuses and warns, so the consumer ports excludes into the flake, deletes the YAML and gitignores it. The
.vig-osraw-YAML opt-out flag is cross-referenced to #885 (not implemented here). Residual for generated configs:pymarkdownis not in nixpkgs, so it is not in the generated base set (documented).Parity: with no
hooks/hooksExcludesargument the dev-shell derivation is byte-identical (drvPathunchanged vs baseline; asserted continuously byTestZeroHooksParity).Type of Change
feat-- New featureModifiers
!) -- This change breaks backward compatibilityChanges Made
nix/hooks.nix(new): one hook-set definition — per-hookyaml(portable render),check(gate fragment),consumer(generation fragment) records; renderersportable.{runner,scaffold},checkArgs,consumer.flake.nix:preCommitChecknow consumeshooksModule.checkArgs(rawConfig unchanged);mkProjectShellgainshooks ? null/hooksExcludes ? [ ](base at module priority 999 so consumer fragments win per-field;passthru.hooksConfigFileexposed only when opted in);lib.hooksPortableexported for the drift gate.tests/test_flake_hooks.py(new, TDD RED→GREEN): fidelity diff (runner + scaffold + scaffold⊆runner), consumer surface (custom hook, disable, per-hook/global excludes), zero-hooks parity.assets/workspace/flake.nix: commentedhooks/hooksExcludesexample.scripts/manifest.toml:.pre-commit-config.yamlentry removed (drift gate replaces the transform chain).docs/NIX.md: "two hook artifacts" section superseded by "one hook definition, three renders".docs/MIGRATION.md: consumer contract + migration steps.Changelog Entry
Added
nix/hooks.nixnow defines the pre-commit hook set once; it renders the sandbox-purechecks.pre-commitgate, the committed.pre-commit-config.yaml+ scaffold copy (drift CI-gated bytests/test_flake_hooks.pyagainstnix eval .#lib.hooksPortable— the hand-synced triangle and the manifest transform chain are retired), and the consumer surface.mkProjectShellgains opt-inhooks(toggle base hooks, per-hookexcludes/overrides, fully custom hooks) andhooksExcludes(global excludes): entering the shell installs the rendered config as a repo-root symlink without ever touchingcore.hooksPath— the scaffold's.githooksentry point stays in charge; a preserved hand-edited YAML (#878) is never overwritten, and the zero-hooks dev-shell stays byte-identical.docs/MIGRATION.md; the.vig-osmanifest raw-YAML opt-out flag is tracked in #885.Review follow-up (hooksPath defect)
The review-confirmed defect — git-hooks.nix's stock installation script unsetting/resetting
core.hooksPathand installing only thepre-commitstage into.git/hookson opt-in — is fixed structurally:install.enable = falseon the consumer git-hooks.nix run disables the stock installation script entirely; only the renderedconfigFileis consumed.mkProjectShellowns a minimal shellHook snippet that replicates the script's config maintenance verbatim minus the git wiring: staleness check on the.pre-commit-config.yamlsymlink, refusal on an existing regular file (fix(workspace): scaffold upgrade replaces .pre-commit-config.yaml, silently clobbering repo-specific hook config #878 semantics preserved), GC root vianix-store --add-root(withln -fsfallback). It never reads or writescore.hooksPathand never installs into.git/hooks, so opting in/out leaves the scaffold's.githookswiring untouched (no flap on rebuild-vs-bump ordering).assets/workspace/.githooks/pre-commitrunsprek runwith no-c, so prek reads.pre-commit-config.yamlat the repo root — exactly the symlink to the generated store config. Verified in a scratch git repo: after the shellHook the symlink points at the generated config,core.hooksPathremains.githooks,.git/hooksgains no files, a second entry is a no-op, and a hand-edited regular file triggers the refusal warning untouched.test_opted_in_shellhook_never_rewires_hookspath(nocore.hooksPath, nouninstallin the generated shellHook) + refusal-message assertion intest_opted_in_shellhook_installs_config; zero-hooksdrvPathre-verified identical to thedevtip. Review nit also taken: the fidelity gate now flags unexpected top-level keys (e.g.fail_fast: true) in either committed YAML.Testing
just test) — targeted suites:tests/test_flake_hooks.py(11 passed),tests/test_transforms.py(4 passed),tests/test_downstream_flake.py(1 passed)Manual Testing Details
nix eval .#lib.hooksPortablediffed against both committed YAMLs — normalized diff empty.nix eval .#checks.x86_64-linux.pre-commit.config.rawConfigidentical before/after the refactor;nix build .#checks.x86_64-linux.pre-commitgreen.nix eval .#devShells.x86_64-linux.default.drvPathidentical before/after.nix flake check --no-buildgreen;deadnix/statixchecks built green.just precommit(prek, all files) — all hooks Passed/Skipped, 0 failed.Checklist
docs/templates/, then runjust docs)CHANGELOG.mdin the[Unreleased]section (and pasted the entry above)Closes #883