nixd
nixd copied to clipboard
Option to use Flake's `outputs.formatter.${system}` for formatting
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.
"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.
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
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.
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?
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)