NixOS/nixfmt

git-breaking behavior when running `nix fmt` on a branch with `*.nix` in the name

Closed this issue · 2 comments

Is there a way to get nixfmt to ignore git refs that include *.nix? Or just ignore the .git directory in general? One of our contributors created a feature branch with .nix in the name and when we try to run nix fmt it formats the refs and breaks some basic git functionality. From what i've seem it's only recoverable by rm'ing the ref's manually. We are using nixfmt-rfc-style on nixos-unstable-small.

For now we're renaming the branch, but it would be nice to know if there's an alternate solution in case someone does something similar in the future.

Example trying to run nix fmt on the affected branch

nix-dotfiles on  feature/create-base.nix [$?⇡] 
❯ nix fmt   
./.git/logs/refs/heads/feature/create-base.nix:1:106:
  |
1 | 0000000000000000000000000000000000000000 691fc9e1517bd564dd1122de387bf0c678ab645f ahuston-0 <aliceghuston@gmail.com> 1713039286 -0400   branch: Created from refs/remotes/origin/feature/create-base.nix
  |                                                                                                          ^
unexpected '@'
expecting expression

./.git/logs/refs/remotes/origin/feature/create-base.nix:1:106:
  |
1 | 0000000000000000000000000000000000000000 691fc9e1517bd564dd1122de387bf0c678ab645f ahuston-0 <aliceghuston@gmail.com> 1713039282 -0400   fetch: storing head
  |                                                                                                          ^
unexpected '@'
expecting expression


nix-dotfiles on  HEAD [$+?] 
❯ git branch --list          
fatal: failed to resolve HEAD as a valid ref 

I just took a look at #151, is this an issue I should open with NixOS/nix instead?

This is definitely the right repository, and I'll consider this a duplicate of #151. The decision there was to not bother trying to encode filtering into nixfmt, see #151 (comment) for more info. This is high in our priorities to address.

Instead you can let treefmt do the filtering for you (programs.nixfmt-rfc-style.enable = true;). Or alternatively, nixfmt $(fd -e nix).