Nix-lsp: Language server for nix

https://gitlab.com/jD91mZM2 has build a parser for nix and a language server for nix called nix-lsp.

It is still in early in development, but you can already test code formatting and renaming symbols.
The parser itself is able to read and dump nixpkgs already.

Since it depends on rust-nightly we can not put it in nixpkgs right-away.
However I created a NUR package: nur.repos.mic92.nix-lsp

$ nix-shell -p nur.repos.mic92.nix-lsp

Here is the configuration I use with LanguageClient-neovim:

let g:LanguageClient_serverCommands = {
      \ 'nix': ['nix-lsp'],
      \ }
let g:LanguageClient_loadSettings = 1
nnoremap <F5> :call LanguageClient_contextMenu()<CR>
nnoremap <silent> gh :call LanguageClient_textDocument_hover()<CR>
nnoremap <silent> gd :call LanguageClient_textDocument_definition()<CR>
nnoremap <silent> gr :call LanguageClient_textDocument_references()<CR>
nnoremap <silent> gs :call LanguageClient_textDocument_documentSymbol()<CR>
nnoremap <silent> <F2> :call LanguageClient_textDocument_rename()<CR>
nnoremap <silent> gf :call LanguageClient_textDocument_formatting()<CR>
16 Likes

Interesting development, but only if it actually works and never crashes. Anyone besides Mic92 that has used it?

My experience with such projects is that they never live up to their promises, so perhaps that user built something that actually delivers, but I am going to guess for now that it doesn’t. I’d say it’s announced too early. It should have a video with a demo of it working at the very least. Release early, release often is a thing, but I think there is also such a thing as being too early. I look forward to the day that nix-mode is implemented with language server support printing a small warning in my status buffer "You don’t have services.language_server.enable = yes; # which would create a superior user experience.

NUR also seems interesting, but the value of a package in nixpkgs is that there is build automation. I mostly hope that more maintainers will get commit access (almost 1000 PRs now(!)). As long as not a lot of people are allowed to touch “core packages” (like the kernel or glibc), I don’t really see an issue with increasing commit access. Also, for most packages the nix packages can be automatically generated, so what really needs to be done is not so much writing packages, but keeping packages updated with automation and that automation needs to be updated and reviewed (which can be done by a small group of people).

My experience with such projects is that they never live up to their
promises, so perhaps that user built something that actually
delivers, but I am going to guess for now that it doesn’t. I’d say
it’s announced too early. It should have a video with a demo of it
working at the very least. Release early, release often is a thing,
but I think there is also such a thing as being too early.

There’s a good change you are a part of the “early majority”
demographic[1] that doesn’t like unstable releases. There’s a
well-studied group of people called “early adopters/innovators” that
don’t mind early releases, as it’s a way to test new ideas and to
contribute code. I have a feeling most nix users are the “early
adopter/innovator” demographic so you should expect to see a lot of
stuff like this around here :wink:

[1] Technology adoption life cycle - Wikipedia

4 Likes

How much of your config do I need to copy to get this up and running?
I know absolutely nothing about Rust, so I’m hesitant to start copying the nightly builds stuff into my config.

I’d like to test this but it seems to not work anymore:

hash mismatch in fixed-output derivation '/nix/store/0fink1xf140r2dqhjn9kdgv1pwmvv4dz-nix-lsp-2018-11-18-vendor':
  wanted: sha256:0n4a684ybh109pdi5i40zqca532k3ir5scykp4hl89qjbvaf1frh
  got:    sha256:13fhaspvrgymbbr230j41ppbz3a5qm12xl667cs7x888h0jvsp5g

Any workaround?

1 Like

It’s possible that they might be able to leverage my tree-sitter grammar, so I figured I’d link to that discussion: Announcing tree-sitter-nix

1 Like

You could copy the package or make a PR fixing the cargoSha256 value, or override Nixpkgs 23.11 manual | Nix & NixOS the package in your config, fixing the cargoSha256 there.