Nixd: nix language server

I also think this is the best long-term solution. As you say, this guarantees 100% consistency between LSP and actual evaluation. I think it might also result in the least maintenance effort.

To actually do this, it might make most sense for you to just create a fork of nix, implement the features you need, and them submit them as a PR to upstream.

Potentially, this could be a faster solution to quickly get the most functionality possible into the hands of users.

I can’t judge the amount of effort either solution requires, though.

1 Like

Our new feature: goto definition for packages, really.

goto-def-package

18 Likes

And new feature: goto definition on more generic values. (e.g. where is mkOrder defined?)

lambda-location

10 Likes

Are you going to remove the syntax error as soon as you start to type anything?

8 Likes

Fine, that might be annoying.

8 Likes

How can we complete nixos options on /etc/nixos/configuration.nix and package names on environment.systemPackages of /etc/nixos/configuration.nix? And can we have document hover for nixos options and package description on /etc/nixos/configuration.nix? I always cost many time to switch between browser which open https://search.nixos.org/options and https://search.nixos.org/packages and vim which open /etc/nixos/configuration.nix. If there are a language server can complete and hover nixos options and package names, it will be nice. :smile:

1 Like

How can we complete nixos options

Nixd do these kind of completion now, but it is based on string manipulation, not for ASTs, (the AST-based implementation is working in progress).

See Nixd 1.1.0 released

package names

It is already implemented. See this example: Nixd 1.1.0 released

document hover

The documentation + example are provided with the completion list. “Hover” documentation is working in progress.

package description

v1.1.0 release has support “goto definition” for packages. The description & version is somehow easy to implemented, how about submit a formal issue (for feature-request) on github?

1 Like

Really weird. I use coc.nvim:

~/.config/nvim/coc-settings.json:

  "languageserver": {
    "nix": {
      "command": "nixd",
      "filetypes": ["nix"]
    }
  }

However, in /etc/nixos/configuration.nix, press n will get some completions like nix, nixpkgs. press nix. will get not any completion. Does it need any configuration?

{ config, pkgs, ... }:

rec {
  ...
}

Have you read the doc: https://github.com/nix-community/nixd/blob/96cc1f7290902caefbc2bf265540279f4543ea66/docs/user-guide.md ?

I have read and git clone the example code and try to edit. There are some comments in the example code like press . will complete XXX but they does not work for me. I don’t know what something I miss :frowning:

You do need to configure it for your repository: https://github.com/nix-community/nixd/blob/96cc1f7290902caefbc2bf265540279f4543ea66/docs/user-guide.md#options

<flakeref> and <name> are not literal, that’s supposed to be the specific nixosConfigurations attribute you want to evaluate. For a non-flake setup presumably you just point it at configuration.nix (some clarification @inclyc ?).

There are some other settings that look like they need to be set as well, the docs aren’t particularly clear on what is required for which use cases (and don’t cover use cases like multi-output flakes - though I suspect that’s just setting multiple installables).

I’ve yet to get a config working too, wasn’t trying too hard. Gave up for now when it started segfaulting and freezing my editor.

The following example comes from https://github.com/nix-community/nixd/blob/96cc1f7290902caefbc2bf265540279f4543ea66/docs/user-guide.md#evaluation.

$ cat .nixd.json
{
  "eval": {
    "target": {
      "args": [
        "--expr",
        "with import <nixpkgs> { }; callPackage ./some-package.nix { } "
      ],
      "installable": ""
    }
  }
}
$ cat some-package.nix
{ stdenv
, lib
}:

stdenv.mkDerivation {
    pname = "...";
    version = "...";
}
$ jq '.languageserver.nix' ~/.config/nvim/coc-settings.json
{
  "command": "nixd",
  "filetypes": [
    "nix"
  ]
}
$ vi some-package.nix

Press 2ji, p, the result is
Screenshot from 2023-07-10 03-05-07

I hope it can complete some package started with p like python :smile:, but it complete some weird placeholder.

1 Like

And this is output of nixd:

$ nixd
Please file an issue at https://github.com/nix-community/nixd/issues/
nixd version: 1.1.0
Previous run crashed:
 0# nixd::sigHandler(int) in nixd
 1# 0x00007F231A052D30
 2# 0x00007F231A0A1A8C
 3# 0x00007F231A052C86
 4# 0x00007F231A03C8BA
 5# 0x00007F231A2A9A89
 6# 0x00007F231A2B4F8A
 7# 0x00007F231A2B3FF9
 8# 0x00007F231A2B4716
 9# 0x00007F2323262864
10# 0x00007F2323262F21
11# 0x00007F231A2B523A
12# 0x00007F231A2AC713
13# 0x00007F231A2E067E
14# nixd::Server::~Server() in nixd
15# main in nixd
16# 0x00007F231A03DACE
17# 0x00007F231A03DB89
18# _start in nixd

I[03:13:50.021] 3860865: nixd 1.1.0 started
E[03:13:50.023] 3860882: created child worker process 3860882
E[03:13:50.025] 3860884: created child worker process 3860884
warning: unknown setting 'allowed-users'
warning: unknown setting 'trusted-users'
E[03:13:50.027] 3860887: created child worker process 3860887
warning: unknown setting 'allowed-users'
warning: unknown setting 'trusted-users'
warning: unknown setting 'allowed-users'
warning: unknown setting 'trusted-users'
I[03:13:50.032] 3860884: --> notify nixd/ipc/diagnostic
I[03:13:50.032] 3860884: --> notify nixd/ipc/diagnostic
I[03:13:50.033] 3860884: --> notify nixd/ipc/finished
I[03:13:50.033] 3860865: <-- nixd/ipc/diagnostic
I[03:13:50.033] 3860865: received diagnostic from worker: 2
I[03:13:50.033] 3860865: <-- nixd/ipc/diagnostic
I[03:13:50.033] 3860865: received diagnostic from worker: 2
I[03:13:50.033] 3860865: <-- nixd/ipc/finished
I[03:13:50.034] 3860887: --> notify nixd/ipc/diagnostic
I[03:13:50.034] 3860887: evaluation on installable , requested depth: 0
I[03:13:50.034] 3860865: <-- nixd/ipc/diagnostic
I[03:13:50.034] 3860865: received diagnostic from worker: 2
I[03:13:50.458] 3860887: evaluation done on worspace version: 2
I[03:13:50.458] 3860887: --> notify nixd/ipc/diagnostic
I[03:13:50.458] 3860887: --> notify nixd/ipc/finished
I[03:13:50.458] 3860865: <-- nixd/ipc/diagnostic
I[03:13:50.459] 3860865: received diagnostic from worker: 2
I[03:13:50.459] 3860865: <-- nixd/ipc/finished

To tell the truth, I like the name of nixd which recall me the good experience from clangd. However, I really cannot make it work for me :cry:

You are writing a lambda which has some formal args, and these formals are not required to be a package name for callPackage. Actually it can be anything. Can you try some completion task from the examples?

Join our matrix group so that you can get real-time technical support.

And looks like the server crashed for some reason. How about compiling the main brach from source?

v1.1.0 has some known bugs in the option system (missing error handling) and the nix library itself.

I have known what happened:

The following can work for me:

git clone --depth=1 https://github.com/nix-community/nixd
cd nixd/docs/examples/options/nixos
vi module.nix

The following cannot work:

git clone --depth=1 https://github.com/nix-community/nixd
vi nixd/docs/examples/options/nixos/module.nix

.nixd.json only work when it exist current working directory. Is it right?

Sometimes nixd-backtrace.dump will be generated. Why not put them to ~/.cache/nixd to avoid users git add them accidentally?

1 Like

Yes, that’s expected behavior. Something like compile_commands.json does expect you have correct working directory.

I have made a PR: docs/examples: talking about cwd by inclyc · Pull Request #220 · nix-community/nixd · GitHub to clarify.

It has been removed in nixd/driver: don't write stack dump file by inclyc · Pull Request #177 · nix-community/nixd · GitHub.

Thank you @inclyc for this excellent project.

I can’t find a way in vscode to disable this popup:

image

It happens often - for example when I open a new (unsaved) file and vscode assumes that it’ll have Nix code in it.

I understand reason for the failure but I have no way to mute the warnings and it happens too often - it’s really annoying. Could you please help?

Submit an issue in the repo? It should be suppressed I guess.

Done. Thank you.

https://github.com/nix-community/nixd/issues/246