You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Redundant parentheses should be removed automatically. nixf-tidy / nixf-diagnose reports them, but can't auto-fix. I assume that removing these is likely much easier implemented in nixfmt than nixf-diagnose, which doesn't do any rewriting, so far.
Small example input
Random example from running nixf-diagnose on Nixpkgs:
with pkgs.perl.pkgs; makeFullPerlPath ([ mod_perl2 ])
Expected output
with pkgs.perl.pkgs; makeFullPerlPath [ mod_perl2 ]
Actual output
with pkgs.perl.pkgs; makeFullPerlPath ([ mod_perl2 ])
If this is done, #251 should probably be done first. In a way, this is also a counter-example that shows us that "redundant" is subjective... On the other side, if we want this, then that's an argument to put it into nixfmt, because that's the only way to make it play along with that issue.
I'm personally in favour and I consider this and #251 to both be different examples of where nixfmt should have an opinion of how parentheses are used.
But I'll note that in general we've been very hesitant to implement syntax transformations in nixfmt. @piegamesde in partular has been able to highlight examples of edge-cases where transforming syntax can quickly get complex, especially when there are intermediate tokens, such as comments nested between/within redundant parentheses.
nixf-tidy / nixf-diagnose reports them, but can't auto-fix. I assume that removing these is likely much easier implemented in nixfmt than nixf-diagnose, which doesn't do any rewriting, so far.
Even if nixf-tidy and nixf-diagnose don't currently implement an "auto fix", that doesn't mean they or another linter couldn't in the future. There's no fundamental rule that formatters can edit the file but linters cant.
If it turns out it is difficult to normalise parentheses in nixfmt and it is difficult to implement auto-fix in existing linters, then it's possible the easiest fix will actually be a new linter, even if it initially only deals with parentheses?
That said, one advantage of having it done by nixfmt, is that adding/removing parentheses often changes the overall formatting. If that is done by a separate tool, then you'd have to separately run nixfmt again after fixing the parentheses.
That said, one advantage of having it done by nixfmt, is that adding/removing parentheses often changes the overall formatting. If that is done by a separate tool, then you'd have to separately run nixfmt again after fixing the parentheses.
FWIW, I think that's just a matter of setting the right priorities in treefmt, so that nixfmt always runs after the respective linter on each nix file.
I still think that this is the job of a linter to do and not of a formatter. Linters have all the infrastructure to ignore specific lints (both at a project configuration level as well as on a code level) and distinct CI vs autofix modes. Nixfmt has none of that, and while other linters may or may not have such infrastructure at the moment, as linters they should, and as a formatter Nixfmt shouldn't.
Btw I don't think this should be too hard to implement in a code base that uses the proper abstractions built for that job (primarily a proper and full CST). Maybe this could even be done with some tree sitter queries? I've seen at least a handful of tools that provide syntax-aware search-and-replace for Nix (no links at hand rn, sorry), surely at least one of them should be suited as a basis for such transformations?
I'd be interested in seeing any prior art from other languages on this topic. Rustfmt seems to have the same reasoning for not implementing it: rust-lang/rustfmt#3823 A quick search on gofmt indicates that it doesn't do it either, although I couldn't find any discussion.
but can't auto-fix. I assume that removing these is likely much easier implemented in nixfmt than nixf-diagnose, which doesn't do any rewriting, so far.
Rewritings have already landed in the upstream library of nixf-diagnose:
Activity
MattSturgeon commentedon Aug 30, 2025
I'm personally in favour and I consider this and #251 to both be different examples of where nixfmt should have an opinion of how parentheses are used.
But I'll note that in general we've been very hesitant to implement syntax transformations in nixfmt. @piegamesde in partular has been able to highlight examples of edge-cases where transforming syntax can quickly get complex, especially when there are intermediate tokens, such as comments nested between/within redundant parentheses.
See:
Even if
nixf-tidyandnixf-diagnosedon't currently implement an "auto fix", that doesn't mean they or another linter couldn't in the future. There's no fundamental rule that formatters can edit the file but linters cant.If it turns out it is difficult to normalise parentheses in nixfmt and it is difficult to implement auto-fix in existing linters, then it's possible the easiest fix will actually be a new linter, even if it initially only deals with parentheses?
That said, one advantage of having it done by nixfmt, is that adding/removing parentheses often changes the overall formatting. If that is done by a separate tool, then you'd have to separately run nixfmt again after fixing the parentheses.
wolfgangwalther commentedon Aug 30, 2025
FWIW, I think that's just a matter of setting the right priorities in treefmt, so that nixfmt always runs after the respective linter on each nix file.
wolfgangwalther commentedon Aug 30, 2025
I had before, but reading one of the mentioned tickets again I find #321 (comment):
So I guess that answers this.
piegamesde commentedon Aug 30, 2025
I still think that this is the job of a linter to do and not of a formatter. Linters have all the infrastructure to ignore specific lints (both at a project configuration level as well as on a code level) and distinct CI vs autofix modes. Nixfmt has none of that, and while other linters may or may not have such infrastructure at the moment, as linters they should, and as a formatter Nixfmt shouldn't.
Btw I don't think this should be too hard to implement in a code base that uses the proper abstractions built for that job (primarily a proper and full CST). Maybe this could even be done with some tree sitter queries? I've seen at least a handful of tools that provide syntax-aware search-and-replace for Nix (no links at hand rn, sorry), surely at least one of them should be suited as a basis for such transformations?
I'd be interested in seeing any prior art from other languages on this topic. Rustfmt seems to have the same reasoning for not implementing it: rust-lang/rustfmt#3823 A quick search on gofmt indicates that it doesn't do it either, although I couldn't find any discussion.
inclyc commentedon Sep 1, 2025
Rewritings have already landed in the upstream library of
nixf-diagnose:https://github.com/nix-community/nixd/blob/15a3376f65de9e7984429b975777f3569430b8a6/libnixf/src/Parse/ParseSimple.cpp#L57C7-L59C52
If you like it, I think it would be easy to write lines of code in
nixf-diagnoserepo to support auto-fixes.wolfgangwalther commentedon Sep 1, 2025
That would be great, because fixing these things manually is a pain, especially initially when the full tree needs to be fixed! :)