Skip to content

pre-commit run runs the hooks devenv.nix enables when no configuration is committed - #339

Open
leftibot wants to merge 1 commit into
cpp-best-practices:mainfrom
leftibot:devenv-hooks
Open

leftibot wants to merge 1 commit into
cpp-best-practices:mainfrom
leftibot:devenv-hooks

Conversation

@leftibot

Copy link
Copy Markdown
Collaborator

Part of #285: devenv's git-hooks.

What changes

cachix/devenv defines its hooks in devenv.nix, not in a committed .pre-commit-config.yaml:

git-hooks = {
  package = pkgs.prek;
  hooks.rustfmt.enable = true;
};

When devenv's shell starts, it writes .pre-commit-config.yaml from those hooks and git-ignores the file. CI then runs devenv shell -- prek run -a.

Until now, prek run found no configuration, so its hooks were unknown. Now, where no configuration is committed (and none is named with -c/--config), pre-commit run and prek run run the hooks that devenv.nix enables:

  • Each hook runs by its id. prek run <id> and SKIP= select hooks as they do for a YAML configuration.
  • Each counts as a hook (from_hook), so a formatter that changes a file fails the run.
  • devenv's older name for the hooks, pre-commit.hooks, is read too.
  • A committed configuration still wins.

devenv.nix is read structurally by a new, small reader of Nix attribute paths, project_validator/facts/nix_syntax.py. It handles:

  • nested and dotted attribute sets;
  • let … in and function arguments before an attribute set;
  • quoted attribute names;
  • strings and comments, whose braces and semicolons it ignores.

A computed value such as lib.mkIf cond { … } or a list is not read as an attribute set.

On the corpus

devenv's three enabled hooks (nixfmt, nixpkgs-fmt, rustfmt) are now found. Its workflow runs them as devenv shell -- prek run -a || true and commits the result back, so with #338 they are reported as rustfmt runs in CI but cannot fail it (|| true), where before it said "CI never runs rustfmt". The check results don't change.

Tests

  • tests/test_nix_syntax.py, for the reader.
  • PreCommitTests in tests/test_expansion.py, for enabled hooks, selection, SKIP, the older name, and a committed or named configuration winning.
  • The expected failure test_devenv_git_hooks_are_preks now passes, so its marker is removed.
  • RED was verified by disabling the devenv fallback, and separately the enable = true filter.

Gates: ruff, ruff format, mypy, lizard (30 lines), all 2,483 tests passing (12 expected failures), coverage 99% (100% on the changed files), 0 jscpd clones, and strict self-validation.

The rest of #285 is the git diff --quiet inside an if condition, which waits on the commit-back decision in #324.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt

…n is committed

devenv writes .pre-commit-config.yaml from devenv.nix's git-hooks (named
pre-commit before devenv 1.4) when its shell starts, and git-ignores the
file. Where no configuration is committed, `pre-commit run` and
`prek run` now run the hooks devenv.nix enables, each by its id, as
hooks: a hook fails the run when it changes a file.

devenv.nix is read structurally by a small reader of Nix attribute
paths (facts/nix_syntax.py): nested and dotted attribute sets, past
`let … in` and function arguments, with strings and comments hiding
their braces.

Part of cpp-best-practices#285

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.75%. Comparing base (d3dc4c2) to head (752c531).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #339   +/-   ##
=======================================
  Coverage   99.75%   99.75%           
=======================================
  Files         299      300    +1     
  Lines       18136    18212   +76     
  Branches     2086     2105   +19     
=======================================
+ Hits        18092    18168   +76     
  Misses         44       44           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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