nix-community / vscode-nix-ide

Nix language support for VSCode editor [maintainer: @jnoortheen]
https://marketplace.visualstudio.com/items?itemName=jnoortheen.nix-ide
MIT License
273 stars 20 forks source link

Support "nix fmt" for flakes in non-project root directories? #307

Open devmattrick opened 1 year ago

devmattrick commented 1 year ago

This is kinda a specific issue that I'm having but I was hoping to use the nix fmt command to format my flake. The only problem is that my flake.nix file is not in the root directory of my project; instead it's nested in a nix/ directory, since I also want to include some non-nix stuff in this specific repository.

I have my Nix IDE plugin set up like so:

{
  "nix.enableLanguageServer": true,
  "nix.serverPath": "nil",
  "nix.serverSettings": {
    "nil": {
      "formatting": { "command": ["nix", "fmt"] }
    }
  },
  // ...
}

The language server is working fine, but when I save a Nix file, I get an error in the output:

[Error - 11:56:44 PM] Request textDocument/formatting failed.
  Message: Failed to run formatter: Process exited with exit status: 1.
path '/home/matt/projects/devmattrick/infrastructure' does not contain a 'flake.nix', searching up
error: path '/home/matt/projects/devmattrick/infrastructure' is not part of a flake (neither it nor its parent directories contain a 'flake.nix' file)

The issue is that my flake.nix (and all other .nix files) are in the nix/ directory of this project. I don't know if this is possible but I think the desired behavior of this would be to run the formatter command relative to the .nix file I'm saving instead of the VSCode project root.

Thanks for the help!


EDIT: It seems like nix fmt also doesn't accept input from stdin. nix fmt might need to be special cased since it will format the entire flake on save, which would probably be undesirable. It doesn't look like there's an easy way to tell nix fmt to "only format this one file" from what I can tell from the documentation.

hcsch commented 1 year ago

I think, as you are using nil for your formatting, the issue is more appropriate for the nil repo. https://github.com/oxalica/nil/issues/70 already exists there to track this issue. You can use "command": ["nix", "fmt", "--", "-"], if the formatter of the flake your editing supports stdin→stdout formatting when given - as its first positional argument (which is what the above "command" value is accomplishing). https://github.com/NixOS/nix/pull/8063 defines this API so in the future all nix formatters for use with flakes should hopefully comply with it (if they don't already).

lriesebos commented 2 weeks ago

@hcsch , maybe unrelated, but do you think your comment in anyway relates to the issue I observed? #413