You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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]:
However, the Javadoc of Project.getBaseDir() explicitly warns that the "project directory" has unexpected values for some projects and should not be used.
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?
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.
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.
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).
Activity
[-]Support `nix fmt`[/-][+]Support `nix fmt` as external formatter[/+]JojOatXGME commentedon Jan 18, 2026
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]:nix-idea/src/main/java/org/nixos/idea/lsp/NixLspServerDescriptor.java
Lines 25 to 27 in 94d472d
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?
provokateurin commentedon Jan 18, 2026
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 fmtthis 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.provokateurin commentedon Jan 18, 2026
I think this might not even be problematic in this case, as long as the result is somewhere within the "real" project directory.
nix fmtwill look also look at parent directories for the flake.nix, so it should work.JojOatXGME commentedon Jan 21, 2026
Correction: The place which needs to be adjusted should be here:
nix-idea/src/main/java/org/nixos/idea/format/NixExternalFormatter.java
Line 61 in 94d472d
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.
[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).provokateurin commentedon Jan 21, 2026
Using the directory of the file should definitely work, however there are other issues with
nix fmtas I pointed out above.bodiam commentedon May 16, 2026
Hi @provokateurin , I've built another Nix plugin for IntelliJ, which has support for
nix fmt(plusalejandra,nixfmt, etc)You can find the plugin here if you're interested: https://plugins.jetbrains.com/plugin/31649-nix-pro
provokateurin commentedon Jul 11, 2026
#103
JojOatXGME commentedon Jul 11, 2026
Reopening this issue because #102 has only addressed part of the topic. Sorry for the confusion.