Skip to content

feat(flake): generate .pre-commit-config.yaml from one flake hook definition, consumer-extensible - #908

Merged
c-vigo merged 9 commits into
devfrom
feature/883-flake-generated-hooks
Jul 7, 2026
Merged

c-vigo merged 9 commits into
devfrom
feature/883-flake-generated-hooks

Conversation

@c-vigo

@c-vigo c-vigo commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

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.nix defines every hook exactly once and renders three artifacts:

  1. checks.pre-commit — the sandbox-pure gate (git-hooks.nix + prek). Refactor-verified: the gate's rawConfig is byte-identical to the pre-refactor baseline.
  2. The PATH-portable runner YAML — the committed .pre-commit-config.yaml and the scaffold copy. They stay committed (portable, no store-path churn), but are no longer hand-synced: tests/test_flake_hooks.py diffs the render (nix eval .#lib.hooksPortable) against both files (normalized: every hook id, args, files, excludes, stages) and fails CI on any drift. The sync-manifest transform 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.
  3. The consumer surface — mkProjectShell gains opt-in hooks (toggle base hooks, per-hook excludes/overrides via module-system merge, fully custom hooks with the full pre-commit schema) and hooksExcludes (global excludes). Opting in wires a config-only snippet into the shellHook: 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), so core.hooksPath and .git/hooks are never modified and the scaffold's .githooks stays 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-os raw-YAML opt-out flag is cross-referenced to #885 (not implemented here). Residual for generated configs: pymarkdown is not in nixpkgs, so it is not in the generated base set (documented).

Parity: with no hooks/hooksExcludes argument the dev-shell derivation is byte-identical (drvPath unchanged vs baseline; asserted continuously by TestZeroHooksParity).

Type of Change

  • feat -- New feature

Modifiers

  • Breaking change (!) -- This change breaks backward compatibility

Changes Made

  • nix/hooks.nix (new): one hook-set definition — per-hook yaml (portable render), check (gate fragment), consumer (generation fragment) records; renderers portable.{runner,scaffold}, checkArgs, consumer.
  • flake.nix: preCommitCheck now consumes hooksModule.checkArgs (rawConfig unchanged); mkProjectShell gains hooks ? null / hooksExcludes ? [ ] (base at module priority 999 so consumer fragments win per-field; passthru.hooksConfigFile exposed only when opted in); lib.hooksPortable exported 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: commented hooks/hooksExcludes example.
  • scripts/manifest.toml: .pre-commit-config.yaml entry 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

  • Flake-generated pre-commit hooks: one definition, consumer-extensible (#883)
    • nix/hooks.nix now defines the pre-commit hook set once; it renders the sandbox-pure checks.pre-commit gate, the committed .pre-commit-config.yaml + scaffold copy (drift CI-gated by tests/test_flake_hooks.py against nix eval .#lib.hooksPortable — the hand-synced triangle and the manifest transform chain are retired), and the consumer surface.
    • mkProjectShell gains opt-in hooks (toggle base hooks, per-hook excludes/overrides, fully custom hooks) and hooksExcludes (global excludes): entering the shell installs the rendered config as a repo-root symlink without ever touching core.hooksPath — the scaffold's .githooks entry point stays in charge; a preserved hand-edited YAML (#878) is never overwritten, and the zero-hooks dev-shell stays byte-identical.
    • Consumer contract and migration steps in docs/MIGRATION.md; the .vig-os manifest 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.hooksPath and installing only the pre-commit stage into .git/hooks on opt-in — is fixed structurally:

  • install.enable = false on the consumer git-hooks.nix run disables the stock installation script entirely; only the rendered configFile is consumed.
  • mkProjectShell owns a minimal shellHook snippet that replicates the script's config maintenance verbatim minus the git wiring: staleness check on the .pre-commit-config.yaml symlink, 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 via nix-store --add-root (with ln -fs fallback). It never reads or writes core.hooksPath and never installs into .git/hooks, so opting in/out leaves the scaffold's .githooks wiring untouched (no flap on rebuild-vs-bump ordering).
  • Generated hooks still execute: assets/workspace/.githooks/pre-commit runs prek run with no -c, so prek reads .pre-commit-config.yaml at 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.hooksPath remains .githooks, .git/hooks gains no files, a second entry is a no-op, and a hand-edited regular file triggers the refusal warning untouched.
  • Tests: test_opted_in_shellhook_never_rewires_hookspath (no core.hooksPath, no uninstall in the generated shellHook) + refusal-message assertion in test_opted_in_shellhook_installs_config; zero-hooks drvPath re-verified identical to the dev tip. Review nit also taken: the fidelity gate now flags unexpected top-level keys (e.g. fail_fast: true) in either committed YAML.

Testing

  • Tests pass locally (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 performed (describe below)

Manual Testing Details

  • Fidelity gate: nix eval .#lib.hooksPortable diffed against both committed YAMLs — normalized diff empty.
  • Gate equivalence: nix eval .#checks.x86_64-linux.pre-commit.config.rawConfig identical before/after the refactor; nix build .#checks.x86_64-linux.pre-commit green.
  • Parity: nix eval .#devShells.x86_64-linux.default.drvPath identical before/after.
  • nix flake check --no-build green; deadnix/statix checks built green.
  • Full hook suite: just precommit (prek, all files) — all hooks Passed/Skipped, 0 failed.

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have updated the documentation accordingly (edit docs/templates/, then run just docs)
  • I have updated CHANGELOG.md in the [Unreleased] section (and pasted the entry above)
  • My changes generate no new warnings or errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

Closes #883

c-vigo added 3 commits July 7, 2026 15:51
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 c-vigo left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.sh wires core.hooksPath .githooks; after opt-in the .githooks shims stop running — the sanctioned-environment guard (IN_CONTAINER/IN_NIX_SHELL) on all three stages and any consumer-owned .githooks scripts are silently disabled. Only a pre-commit-stage hook lands in .git/hooks (every consumer-profile hook has default stages, so install_stages == ["pre-commit"]).
  • It flaps: the install block is guarded by config-symlink staleness, so a container rebuild re-points core.hooksPath to .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 .githooks wiring.

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.

c-vigo added 6 commits July 7, 2026 16:23
…rated-hooks

# Conflicts:
#	CHANGELOG.md
#	assets/workspace/.devcontainer/CHANGELOG.md
…tray top-level YAML keys

The opted-in generation shellHook must install the rendered config
without touching core.hooksPath or .git/hooks (PR #908 review), and the
fidelity gate must flag hand-added top-level keys (e.g. fail_fast) the
per-hook comparison would miss.

Refs: #883
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
@c-vigo
c-vigo merged commit 2355daf into dev Jul 7, 2026
15 checks passed
@c-vigo
c-vigo deleted the feature/883-flake-generated-hooks branch July 7, 2026 14:56
c-vigo added a commit that referenced this pull request Jul 15, 2026
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
c-vigo added a commit that referenced this pull request Jul 15, 2026
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
c-vigo added a commit that referenced this pull request Jul 15, 2026
)

## 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant