Skip to content
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

nix fmt support #70

Open
opeik opened this issue Mar 10, 2023 · 8 comments
Open

nix fmt support #70

opeik opened this issue Mar 10, 2023 · 8 comments
Labels
C-support Catagory: support questions

Comments

@opeik
Copy link

opeik commented Mar 10, 2023

Hi there,

I'm using vscode-nix-ide which breaks if you ask nil to format with nix fmt. Instead of formatting the file, it clears it instead. This seems to be caused by nix fmt not accepting input from stdin.

On a side note, it would be cool if nil could use self.formatter."${system}" by default.

VSCode configuration
"nix.enableLanguageServer": true,
"nix.serverPath": "nil",
"nix.serverSettings": {
  "nil": {
    "formatting": {
      "command": ["nix", "fmt"]
    }
  }
},
App versions
@oxalica oxalica added the C-support Catagory: support questions label Mar 10, 2023
@oxalica
Copy link
Owner

oxalica commented Mar 10, 2023

This seems to be caused by nix fmt not accepting input from stdin.

nix fmt automatically add an argument . to the underlying formatter if there is none, which makes it format the whole directory instead of using stdio interface.

After some digging, I found that you setting formatting command to ["nix", "fmt", "--", "--"] work for me, as long as the underlying formatter supports stdio interface.

On a side note, it would be cool if nil could use self.formatter."${system}" by default.

I'm afraid not. nix fmt requires dumping the whole directory, evaluation and maybe building on every change. It would cause unacceptable delay/hanging with format-on-save.

My suggestion is to set fixed and responsive formatter for nil instead, and use nix fmt only in CI or pre-commit hooks.

@figsoda
Copy link
Contributor

figsoda commented Mar 11, 2023

maybe nil can evaluate .#formatter.${system} once at startup and use that as a fallback to nil.formatting.command if it exists

@oxalica
Copy link
Owner

oxalica commented Mar 12, 2023

maybe nil can evaluate .#formatter.${system} once at startup and use that as a fallback to nil.formatting.command if it exists

formatter interface uses file IO by design, which is not compatible with ours. So we cannot do it in general. If only Nix Flakes can enforce the stdio interface by using, to say nix fmt -- -, then it can be guaranteed to work.

@oxalica
Copy link
Owner

oxalica commented Mar 17, 2023

XRef: NixOS/nix#8063

@nyabinary
Copy link

This seems to be caused by nix fmt not accepting input from stdin.

nix fmt automatically add an argument . to the underlying formatter if there is none, which makes it format the whole directory instead of using stdio interface.

After some digging, I found that you setting formatting command to ["nix", "fmt", "--", "--"] work for me, as long as the underlying formatter supports stdio interface.

On a side note, it would be cool if nil could use self.formatter."${system}" by default.

I'm afraid not. nix fmt requires dumping the whole directory, evaluation and maybe building on every change. It would cause unacceptable delay/hanging with format-on-save.

My suggestion is to set fixed and responsive formatter for nil instead, and use nix fmt only in CI or pre-commit hooks.

["nix", "fmt", "--", "-"] also works :3

@losnappas
Copy link

nix fmt (with nix fmt -- - using nixfmt-rfc-style) is incredibly slow, any methods of speeding it up?

@sellout
Copy link

sellout commented Oct 4, 2024

nix fmt (with nix fmt -- - using nixfmt-rfc-style) is incredibly slow, any methods of speeding it up?

My trick for speeding up nix fmt is to get an alias into my environment. E.g.,

devShells.${system}.default = pkgs.mkShell {
  shellHook = ''
    alias flake-fmt=${self.formatter.${system}.meta.mainProgram}
  '';
};

this avoids the need to evaluate the flake on each execution of the formatter and now I can set up flake-fmt to run-on-save in my editor and it’s very speedy.

Unfortunately, it doesn’t work quite as smoothly with direnv/use flake, but direnv/direnv#73 has a lot of workarounds for getting aliases into the shell from direnv.

@sellout
Copy link

sellout commented Oct 8, 2024

I’ve modified this a bit. My infra is a bit more involved, but it boils down to

devShells.${system}.default = pkgs.mkShell {
  shellHook = ''
    export PROJECT_FORMATTER=${lib.getExe self.formatter.${system}}
  '';
};

and then I have a script on the PATH that does roughly

"$PROJECT_FORMATTER" "$@"

And this now has no issues with using Direnv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-support Catagory: support questions
Projects
None yet
Development

No branches or pull requests

6 participants