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

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

Open
devmattrick opened this issue Nov 26, 2022 · 2 comments
Open

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

devmattrick opened this issue Nov 26, 2022 · 2 comments

Comments

@devmattrick
Copy link

devmattrick commented Nov 26, 2022

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
Copy link

hcsch commented Apr 10, 2023

I think, as you are using nil for your formatting, the issue is more appropriate for the nil repo. oxalica/nil#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). NixOS/nix#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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants