Thanks to @Rizary for pushing it to the last mile!
I’ve been happy using GitHub - serokell/nixfmt: A formatter for Nix code for formatting;
is there formatting beyond just Nix language that nixpkg-fmt does ?
(Specific to it being a pkg for instance)
Wondering what’s the delta & whether it’s migrating to it.
The fundamental difference is that nixfmt is a pretty-printer, whereas nixpkgs-fmt uses a rewrite engine to format the code. This means that nixfmt needs to be very opinionated as there is only one representation possible for a given AST. On the other hand, nixpkgs-fmt will mainly focus on indenting the code properly. Both approaches have different advantages.
A few differences that come to mind:
- nixfmt doesn’t allow consecutive empty lines as a way to visually separate a block of code. To me, this is important for readability but YMMV.
- nixfmt collapses arrays that are on multiple lines back into a single line. nixpkgs-fmt only expands partially expanded arrays to one element per line.
- in general, code formatted with nixfmt is denser as it collapses elements vertically.
- nixpkgs-fmt works on invalid code. It can format until it reaches an unknown syntax. This can be useful during editing when you want your WIP code to be formatted.
If you want to compare the outputs, both tools have online versions of them so you can try them out:
Personally, I feel too boxed-in with nixfmt and just want liteweight formatting for my Nix code. Code formatting is a matter of personal preference so if you are happy with nixfmt then keep using it!