git-hooks.nix icon indicating copy to clipboard operation
git-hooks.nix copied to clipboard

Flakes support seemingly broken

Open l0b0 opened this issue 11 months ago • 4 comments

I've tried following the Flakes support section, but the resulting setup is broken in two ways:

  • nix flake check prints the diff necessary to satisfy the formatters, but doesn't actually apply them.
  • nix develop (after a direnv deny) does not set up pre-commit hooks, so git commit skips all of them.

Relevant diff

l0b0 avatar Mar 22 '25 09:03 l0b0

nix flake check prints the diff necessary to satisfy the formatters, but doesn't actually apply them.

This is expected behaviour though, no? For nix build and nix flake check, flakes require the src to be copied over to the store. So the diff is being applied during the build, just not to your local copy. If you want the changes to be applied locally, run pre-commit run -a inside nix develop.

nix develop (after a direnv deny) does not set up pre-commit hooks, so git commit skips all of them.

Do you have some more detailed steps I can follow to reproduce this?

sandydoo avatar Mar 22 '25 13:03 sandydoo

nix flake check prints the diff necessary to satisfy the formatters, but doesn't actually apply them.

This is expected behaviour though, no? For nix build and nix flake check, flakes require the src to be copied over to the store. So the diff is being applied during the build, just not to your local copy. If you want the changes to be applied locally, run pre-commit run -a inside nix develop.

I might've missed it, but I wish this sort of giant caveat was spelled out before trying it out. I thought git-hooks.nix would just create a pre-commit "bundle" which would then be auto-referenced from Git hooks, to be run against the current state of the code.

nix develop (after a direnv deny) does not set up pre-commit hooks, so git commit skips all of them.

Do you have some more detailed steps I can follow to reproduce this?

  1. Clone repo above
  2. Check out commit above
  3. Run nix develop
  4. Try to git commit something

l0b0 avatar Mar 22 '25 13:03 l0b0

I wish this sort of giant caveat was spelled out before trying it out. I thought git-hooks.nix would just create a pre-commit "bundle" which would then be auto-referenced from Git hooks, to be run against the current state of the code.

Yeah, we should say that nix flake check can only validate formatting, not fix.

This might be possible to achieve with nix fmt. You can set formatters in the flake and then point it to a bash script that runs pre-commit run -a. PRs very welcome on this front.

sandydoo avatar Mar 25 '25 13:03 sandydoo

Indeed, nix flake checks run in a sandbox and therefore cannot alter the contents of your repository. This is not specific to git-hooks.nix but of anything you put in a nix flake check. The documentation of flakes support does say “to run the all the hooks on CI”, but can maybe be made clearer.

You can however:

  • Go the nix fmt route. This would be very nice, I think, but requires quite a bit of code.
  • Expose a nix run .#pre-commit utility of some kind.
  • Expose the pre-commit configuration in your devShell, which will set up the pre-commit, but also allow you to run pre-commit run -a.

I personally have been using the last option and been very happy with it.

Niols avatar May 20 '25 14:05 Niols

Check the shellHook env variable, if it's not set you might have started the wrong dev shell. Don't ask how I know this and how long it took to figure out..

It should be something like this:

env | grep shellHook
shellHook=export PATH=/nix/store/v0sf67x7sw6pg277amhgf3j84m60wrqn-pre-commit-4.2.0/bin:$PATH

Joker9944 avatar Aug 23 '25 08:08 Joker9944

I've added more examples to the README, explaining how to configure and use nix develop and nix fmt, as well as when to avoid nix flake check.

sandydoo avatar Aug 23 '25 15:08 sandydoo