Nixd: nix language server

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

Hi @inclyc ,

I recently upgraded to 24.05 and also upgraded nixd, vscodium and jnoortheen.nix-ide and the most visible update is definitely nixd. Great new features, thank you!

Could you please advise how to disable unused binding warning?

Iā€™m getting a lot of:

definition `config` is not used
nixf(sema-def-not-used) [Ln 67, Col 22]

Iā€™ll eventually fix it but at this moment I get too many warnings and itā€™s overwhelming.

I figured out that I need to pass some flag into my VSCodium config but I canā€™t figure out howā€¦

{
    // ...

    "[nix]": {
        "editor.tabSize": 4,
        "editor.formatOnPaste": false,
        "editor.hover.enabled": false
    },
    "nix.enableLanguageServer": true,
    // "rnix-lsp", "nil", "nixd"
    "nix.serverPath": "nixd",
    "nix.serverSettings": {
        // settings for 'nixd' LSP
        "nixd": {
            "eval": {
                // stuff
            },
            "formatting": {
                "command": "nixpkgs-fmt"
            },
            "options": {
                "enable": true,
                "target": {
                    // tweak arguments here
                    "args": []
                    // NixOS options
                    // "installable": "<flakeref>#nixosConfigurations.<name>.options"
                    // Flake-parts options
                    // "installable": "<flakeref>#debug.options"
                    // Home-manager options
                    // "installable": "<flakeref>#homeConfigurations.<name>.options"
                }
            }
        }
    }

    // ...
}

Just read the docs about diagnostic control, it is available since 2.2.0

Thank you. I changed my settings but I donā€™t see any effect.

    "nix.serverSettings": {
        "nixd": {
            "eval": {},
            "formatting": {
                "command": "nixpkgs-fmt"
            },
            "diagnostic": {
                "suppress": [
                    "sema-def-not-used"
                ]
            },
            "options": {
                "enable": true,
                "target": {}
            }
        }
    },

Maybe you are using some outdated releases. Also the configuration basically changed in version 2.0, and not backward-compatible. Please read the docs on github :wink:

I have the same problem - the config has no effect.

I copy/pasted the config from the readme:

{
    "nix.enableLanguageServer": true,
    "nix.serverPath": "nixd",
    "nix.serverSettings": {
        "nixd": {
            "eval": {},
            "formatting": {
                "command": "nixpkgs-fmt"
            },
            "diagnostic": {
                "suppress": [
                    "sema-escaping-with",
                    "sema-unused-rec",
                    "sema-def-not-used"
                ]
            },
            "options": {
                "enable": true,
                "target": {}
            }
        }
    }
}

and all my packages are from flaked NixOS 24.05 so everything is up-to-date.

The config you pasted here is apparently outdated. Please read nixd/nixd/docs/configuration.md at be5ad5ec113595e2900e6391a08cf0e4784a9cfe Ā· nix-community/nixd Ā· GitHub

VSCode extensions usually have long release cycle and you may read some outdated docs on the marketplace.

Specifically eval should be removed at all. Then formatting command is an array.

2 Likes

Thank you. I can see the difference now. To make it as simple as possible, I copy/pasted it without options and I added diagnostic from Neovim example (missing in VSCode example):

{
    "nix.enableLanguageServer": true,
    "nix.serverPath": "nixd",
    // https://github.com/nix-community/nixd/blob/6811dcf03ac055752a3f28cbabf90bd0b0cee417/nixd/docs/configuration.md
    "nix.serverSettings": {
        "nixd": {
            "formatting": {
                "command": [
                    "nixpkgs-fmt"
                ]
            },
            "diagnostic": {
                "suppress": [
                    "sema-def-not-used"
                ]
            }
        }
    }
}

I restarted VSCode and:
image