-
-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improving flake formatting experience #273
Comments
That one is kinda awkward, since it was already decided that recursively finding nix files to be formatted is a non-trivial issue that is kinda out-of-scope for a tool that just wants to be able to format a nix file. IIRC, the main complexities come from ignoring Maybe this could be worked around by renaming The wrapper package could of course use an existing lib/tool to implement that. And treefmt users should probably be able to use either the wrapped or unwrapped package. |
It seems quite trivial, actually; this decision comes off more like it's just intentionally making it harder to use flakes. Using a random third-party tool just to be able to use the official formatter with official nix is a nonstarter IMO. And there's some irony here that the nix docs still give the example of setting In any case, as amusing as whining about nix* community dysfunction is, since I'd rather just get things done,
I specifically chose To be able to effectively use formatter.x86_64-linux =
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
in
pkgs.writeShellApplication {
name = "nixfmt-wrapper";
runtimeInputs = [
pkgs.fd
pkgs.nixfmt-rfc-style
];
text = ''
fd "$@" -t f -e nix -x nixfmt '{}'
'';
}; The args that are passed (to the formatter package's executable) appear to be the path(s) that are (im/ex)plicitly passed to |
Yes, the observation behind this issue was that is should be dead simple to format a flake the "official way". |
Sure, I'm not entirely disagreeing that having nixfmt behave differently from the other formatters is surprising; I just wanted to share (with anyone watching this issue) a straightforward workaround that works today and will continue to work even if directory support is removed from nixfmt entirely. |
Discussed in todays meeting (though we didn't fully read the previous discussion here): It's possible to create a binary that runs treefmt with a default config, two options:
See also NixOS/nix#11252 which is related
|
This issue has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/formatting-team-meeting-2024-12-10-and-2025-01-07/58466/1 |
I think I agree with @eclairevoyant that we should avoid tightly coupling nixfmt with treefmt. I prefer their proposal of a wrapper that uses something more generic like I'd propose having:
The wrapper script could either be maintained in nixpkgs, or here in nixfmt. I don't think that's too important. If the wrapper script is maintained here, then perhaps there's no need for separate wrapper packages? I also think we should avoid going down the rabbit hole of changing how |
The ultimate test is whether it works for Nixpkgs development, which the simple |
+1 to not having One minor thing to keep in mind: treefmt/treefmt-nix make an effort to have
I believe neither @eclairevoyant's |
(All credit of the POC goes to @dasJ! Updated the meeting notes to point that out 😄 ) |
Correct me if I'm wrong, but with the current nix3 and flake implementation doesn't this require some pretty nasty hacks and/or assumptions? No matter what treefmt-nix attempts, there isn't a deterministic way to accurately & reliably find the flake root. Right? I assume using treefmt without treefmt-nix doesn't bring this baggage, though? |
Yeah, that's what treefmt-nix's And now I realize I've steered us towards that rabbit hole of bike shedding you mentioned above... |
This issue has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/formatting-team-meeting-2025-01-21/59183/1 |
I've just opened NixOS/nixpkgs#384857 to mostly address this. Also #287 to make it more discoverable. |
In order to have your flake formatted with
nixfmt-rfc-style
, you have two options:formatter = pkgs.nixfmt-rfc-style;
. This solution is concise (ignoring the fact that the name "nixfmt-rfc-style" is not really intuitive compared to simply "nixfmt").However, this is not expected to be supported for long as:
Passing directories or non-Nix files (such as ".") is deprecated and will be unsupported soon, please use https://treefmt.com/ instead, e.g. via https://github.com/numtide/treefmt-nix
.I think it should be great to support the existing
formatter = pkgs.nixfmt[-rfc-style];
syntax as it gives a very efficient and user-friendly way to format a flake while keeping it lean code and input wise.In the long term, I could see the benefit of further "officializing" the
nixfmt
formatter and having aformat = true;
flag to automatically format the flake. This would have to be thought more.cc @infinisil
The text was updated successfully, but these errors were encountered: