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 · 4 comments
Open

nix fmt support #70

opeik opened this issue Mar 10, 2023 · 4 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

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

3 participants