nixd icon indicating copy to clipboard operation
nixd copied to clipboard

Option to use Flake's `outputs.formatter.${system}` for formatting

Open nekowinston opened this issue 1 year ago • 5 comments

Hi, thanks for this great language server! 🙂

Since flakes can set a preferred formatter in the output schema, it'd be cool if the language server defaulted to the formatter specified in the root flake. I'm jumping between projects that use alejandra and nixfmt-rfc-style, and it'd be great if I didn't have to specify an extra option for nixd in my project settings.

nekowinston avatar May 07 '24 14:05 nekowinston

    "nix.serverPath" = lib.getExe pkgs.nixd;
    "nix.serverSettings".nixd = {
      formatting.command = [ "nix" "fmt" "--" "--" ];
    };

You could also make a wrapper script that does something else if you can't eval the flake's formatter.

spikespaz avatar May 14 '24 06:05 spikespaz

I think @spikespaz 's solution is better and more clean, it seems like I don't need to integrate this into nixd itself. Agree? @nekowinston

inclyc avatar May 23 '24 12:05 inclyc

Running nix fmt on a single file takes ~15 seconds for me in my dotfiles flake, and ~4.5 seconds on a new flake.nix with a single nixpkgs input. I would've hoped that nixd would cache the path of the outputs.formatter.${system} evaluation, and call that binary directly, rather than waiting for nix to evaluate & run the binary.

nekowinston avatar May 23 '24 13:05 nekowinston

I agree that using that nix command is sub-optimal. It takes a long time to evaluate, build, and then format, and it seems to want to evaluate (at least) every time. Also seems like garbage collection cleans it up. We should probably not rely on this, and if the formatter is flake we need to evaluate the binary path of the formatter output and use it until the output changes. What is the fastest way to check if an output has changed, without evaluating?

spikespaz avatar May 25 '24 02:05 spikespaz

Using @spikespaz nix fmt formatting configuration for nixd results in the LSP server returning errors :thinking:

jsonrpc-request: jsonrpc-error: "request id=120 failed:",
 (jsonrpc-error-code . -32001), 
 (jsonrpc-error-message . "formatting nix command exited with 256"), 
 (jsonrpc-error-data)

skykanin avatar Jun 15 '24 17:06 skykanin