Skip to content

Support nix fmt as external formatter #97

Description

@provokateurin

When trying to use nix fmt as an external formatter it fails with path '/home/kate' does not contain a 'flake.nix', searching up error: could not find a flake.nix file.
This probably just means that the working directory isn't set to the project directory when the external formatter is executed (and is executed from the home directory instead).

Activity

  1. changed the title [-]Support `nix fmt`[/-] [+]Support `nix fmt` as external formatter[/+] on Jan 16, 2026
  2. JojOatXGME commented on Jan 18, 2026

    @JojOatXGME
    Contributor

    Thanks for your report. It looks like the working directory is currently inherited from IntelliJ's process. This means you might be able to work around the issue by starting IntelliJ from your project directory.

    The fix is probably to call GeneralCommandLine.withWorkDirectory(…) at the following location [correction]:

    public @NotNull GeneralCommandLine createCommandLine() throws ExecutionException {
    List<String> argv = ParametersListUtil.parse(myCommand, false, true);
    return new GeneralCommandLine(argv);

    However, the Javadoc of Project.getBaseDir() explicitly warns that the "project directory" has unexpected values for some projects and should not be used.

    https://github.com/JetBrains/intellij-community/blob/ece1b4e2bb644e0fd68ce7e43845d4fd64d67378/platform/core-api/src/com/intellij/openapi/project/Project.java#L34-L48

    Anyway, it might still be more reasonable to use the "project directory" (which probably still works in most projects?) than keeping the current behavior. What do you think? Do you have another suggestion?

  3. provokateurin commented on Jan 18, 2026

    @provokateurin
    MemberAuthor

    I tried running IntelliJ from the project directory and it seems to somewhat work, at least I don't get an error message anymore. However now the file is emptied and I have to go to another file and back to get dialog with "load filesystem change". Once I click that, the file content is back and correctly formatted.
    So going with the project directory seems like a good idea, but it's probably necessary to do some other work to make the experience smooth. I'm not sure how the other formatters work exactly, but I assume that this happens because the other formatters take the stdin and return the formatted code in the stdout. For nix fmt this is not the case, resulting in an empty file because there is no stdout. Only after that the changes are written to the file directly, requiring the manual reload.

  4. provokateurin commented on Jan 18, 2026

    @provokateurin
    MemberAuthor

    However, the Javadoc of Project.getBaseDir() explicitly warns that the "project directory" has unexpected values for some projects and should not be used.

    I think this might not even be problematic in this case, as long as the result is somewhere within the "real" project directory. nix fmt will look also look at parent directories for the flake.nix, so it should work.

  5. JojOatXGME commented on Jan 21, 2026

    @JojOatXGME
    Contributor

    The fix is probably to call GeneralCommandLine.withWorkDirectory(…) at the following location:

    Correction: The place which needs to be adjusted should be here:

    var commandLine = new GeneralCommandLine(argv);

    I realized that this ticket is about an external formatter, not about a language server. 😅 In this case, we probably have the path of the specific file you are formatting available. I guess we could just use the directory containing the file.

    I think this might not even be problematic in this case, as long as the result is somewhere within the "real" project directory.

    [Only relevant if we don't want to use the directory of the file for some reason.]
    I am not sure whether the directory would be inside the "real project directory". I don't know about the cases the Javadoc is concerned about. I guess one scenario might be when you have multiple projects imported in a single IntelliJ Window/Project. Maybe there are other cases as well. Anyway, using Project.getBaseDir()/Project.getBasePath() despite the warning might be the best simple option (except using the file path).

  6. provokateurin commented on Jan 21, 2026

    @provokateurin
    MemberAuthor

    Using the directory of the file should definitely work, however there are other issues with nix fmt as I pointed out above.

  7. bodiam commented on May 16, 2026

    @bodiam

    Hi @provokateurin , I've built another Nix plugin for IntelliJ, which has support for nix fmt (plus alejandra, nixfmt, etc)

    Image

    You can find the plugin here if you're interested: https://plugins.jetbrains.com/plugin/31649-nix-pro

  8. provokateurin commented on Jul 11, 2026

    @provokateurin
    MemberAuthor
  9. JojOatXGME commented on Jul 11, 2026

    @JojOatXGME
    Contributor

    Reopening this issue because #102 has only addressed part of the topic. Sorry for the confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions