Skip to content

Add initial first-class support for nix fmt - #104

Draft
JojOatXGME wants to merge 2 commits into
masterfrom
nixfmt
Draft

JojOatXGME wants to merge 2 commits into
masterfrom
nixfmt

Conversation

@JojOatXGME

@JojOatXGME JojOatXGME commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

This is my attempt to resolve #97.

This PR takes initial steps for first-class support of nix fmt, together with a new option “Use CLI argument” for custom commands.

@Meallia I am mentioning you because you worked on #102, and because you also considered looking into this topic in the future. Feel free to comment if you have any suggestions, but no pressure.

Approach and remaining problems

Unfortunately, my testing shows some problems with the current implementation. Here is my approach:

  1. Ensure the focused file is written to disk. (Already done by the platform)
  2. Call nix fmt {path-to-file}
  3. Call VfsUtil.markDirtyAndRefresh to inform IntelliJ that the file has changed

This leads to the following dialog if the user continuous to type after string the formatter:

grafik

I currently see two potential solutions to address these problems:

  • Rely on treefmt-specific command line arguments:
    nix fmt -- --stdin -- {path-to-file}

  • Copy the formatted file, and pass the new temporary file to the command instead of the original.
    Afterward, copy back the formatted changes from the temporary file to IntelliJ.

Not sure if there is a third viable approach.

I also noticed that the current performance is rather bad. Running the formatter from IntelliJ takes much longer then calling in from the terminal. I have not yet profiled the issue.

Future Work

Beside the previously mentioned issues, there are some open topics which could be discussed in the future if the initial implementation has been released:

  • nix fmt is often configured to also handle other types of files, not just *.nix. It might be valuable to enable the external formatter for these files as well. The main challenge would be how to figure out from the plugin which files can be formatted by the current configuration of nix fmt.

  • IntelliJ cancels the formatting task if it takes more than 30 seconds. This can easily happen if the formatter is not yet available and has to be built (or downloaded). We should try to figure out if we can find a way around this timeout.

  • When running the formatter, stderr is currently only shown to the user once the process returns a non-zero exit code. However, for lengthy build processes, we should try to make the progress visible to the user.

Screenshots

Here are two screenshots of the new settings page:

grafik grafik

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

Successfully merging this pull request may close these issues.

Support nix fmt as external formatter

1 participant