nixfmt
nixfmt copied to clipboard
Support single '-' operand and take it as reading from standard input
Currently passing the argument -
does not try to read the "file" from standard input.
E.g.:
nixfmt - < flake.nix
This is a common standard as suggested per POSIX.
This is especially required if using nixfmt via flake formatter nix fmt
. As nix fmt
does not handle standard input by default, but you need to run nix fmt -- -
. One could also argue that it would be neat if nix fmt
supported this.
Speaking of compatibility with nix fmt
. Just plainly running nix fmt
doesn't work as that will invoke. nixfmt .
which is also not supported by nixfmt
.
$ nix fmt
nixfmt: .: openFile: inappropriate type (is a directory)
Not the cleanest, but a current workaround is to create a wrapper and assign it to the formatter attribute, something like this:
pkgs.writeShellApplication {
name = "nixfmt-wrapper";
runtimeInputs = [ pkgs.coreutils pkgs.findutils pkgs.nixfmt ];
text = ''
case "$*" in
(-) cat - | exec nixfmt;;
(.) exec find . -name '*.nix' -exec nixfmt {} +;;
(*) exec nixfmt "$@";;
esac
'';
});
nixfmt already reads from stdin by default when no positional argument is passed.
I suppose this issue is about compatibility with nix fmt
in general. I can't find any specification that needs to be followed in order to be compatible with it, but if there is, I don't mind implementing it.
Yeah, I guess nix fmt
could be changed as well. Currently it does not support forwarding stdin by default, but you need to provide a -
in order to get that behavior. This could probably be changed. Especially since it is a bit annoying to do | nix fmt -- -
as opposed to just | nix fmt
Should I rename the issue to be about nix fmt
compatibility. Perhaps this is something that should be raised on that end as well, seeing it is still part of the unstable API.