NixOS / nix

Nix, the purely functional package manager
https://nixos.org/
GNU Lesser General Public License v2.1
12.07k stars 1.47k forks source link

`nix fmt` formats files in .git, leading to a corrupted repository #9359

Open whentze opened 9 months ago

whentze commented 9 months ago

Describe the bug

If you make a git branch/tag whose name ends in .nix, git creates files like .git/refs/heads/add-stuff-to-default.nix that don't contain nix code but end up being "formatted" by nix fmt anyway. This leads to a corrupted git repository and very confusing messages like:

$ git log
fatal: your current branch appears to be broken
$ git checkout
fatal: You are on a branch yet to be born
$ git stash save
You do not have the initial commit yet

Steps To Reproduce

  1. Make a flake and add a formatter
  2. git switch -C add-stuff-to-default.nix
  3. nix fmt
  4. everything gets really fucky

Expected behavior

I think nix fmt should entirely ignore .git. Perhaps it should also skip gitignored stuff, but I'm not sure.

nix-env --version output

nix-env (Nix) 2.13.6

Radvendii commented 9 months ago

It would also be good to have flags to nix fmt, and options for formatter in the flake.nix file, which control which files are formatted. e.g. --exclude

roberth commented 9 months ago

nix fmt is almost nix run.

If this is the kind of bug report we can expect from this feature, with formatters having varying behaviors when it comes to files and directories, this would be another reason to remove nix fmt.

With something like nix run, there would be no expectations that the formatter magically behaves correctly; just that it gets invoked. (Also: nix dev)

Here's the other issue

roberth commented 9 months ago

It would also be good to have flags to nix fmt, and

We'd have scope creep until we've duplicated treefmt, but worse.

whentze commented 9 months ago

If this is the kind of bug report we can expect from this feature, with formatters having varying behaviors when it comes to files and directories, this would be another reason to remove nix fmt.

Huh, interesting. I had somehow assumed that nix fmt, like other flake-based commands, would work on the flake tree, which does not have .git, I think.