Best ide for writing nix expressions

I have been writing nix expressions mostly using command line editors like nano, but as things get more complex I find myself more and more confused, especially when reading other peoples code, i.e. when reading nixpkgs code.

I often come across code which includes a bunch of namespaces using with, i.e. with builtins; with lib; with foo; with bar followed by a bunch of function calls and names that I have no idea where they originate from.
Most IDEs have features which allow the user to search for the definition of a function or name which is incrediby useful to learn more about frameworks and libraries imo. Also auto completion would be really helpful to get to know which functions or members are available on certain objects.
I.e. geting autocompletion after typing builtins. would be much quicker than scrolling through its source file on github.

Is something like that available for nix in any ide or text editor?

If so, which offers the most complete solution? So far I have tried the vscode and intellij plugins and lsp support for both vscode and intellij using the nix-lsp, but both are pretty disappointing.

5 Likes

Not the answer you are looking for, but:

  • nix repl supports completion, i.e. you can type builtins.<TAB> and it will list all builtin functions. You can launch it with the nixpkgs attrset loaded like this: nix repl '<nixpkgs>' and then you can do pkgs.<TAB> also;
  • nix-doc seems pretty helpful (I haven’t used it much yet though).

I haven’t found an IDE that would provide auto-completion for Nix, I’m writing Nix mostly in vim, usually side by side with the Nix and nixpkgs manuals.

2 Likes

Good point. Still, makes me wonder why there isn’t any ide with the same features as the repl. It is clearly something that works generally.

Did you try GitHub - nix-community/rnix-lsp: WIP Language Server for Nix! [maintainer=@aaronjanse] ?

5 Likes

Yeah, but I cannot get autocomplete or anything else to work. Enabling support for it in the nix vscode plugin also does not seem to change anything other than maybe a bit better error checking.
But it’s definitely doing something because it creates a folder called lsp with a bunch of logs in it.

1 Like

Probably the closest you’ll get is manix rnix-lsp fork.

1 Like

Haven’t tried it yet, but this one looks promising: GitHub - aaronjanse/nix-eval-lsp: Nix language server that evaluates code

2 Likes

The short version:

Pick an editor/IDE with support for LSP.

The longer version:

You are going to be disappointed if you expect an environment with support for the nix language similar to what you get with java using IntelliJ or C# with Visual Studio.

That being said, trying to get anything done with an extremely limited editor like nano is making things harder than they have to be. Do yourself a favour and learn emacs or VIM. Many people swear by VS Code, but I’ve never used it and have nothing to say about it. All 3 support LSP and the experience will therefore improve as the various LSP server implementations improve. Keep in mind that all 3 are text editors with various degrees of IDE-like functionality.

2 Likes

This is probably because the completion doesn’t do much right now. It mainly supports builtins and local variables.

However @aaronjanse has started porting his changes to rnix-lsp add evaluator by aaronjanse · Pull Request #38 · nix-community/rnix-lsp · GitHub. It’s still on my todo list to review it to help them getting it into a mergable state :slight_smile:

3 Likes

I did not. I will now, thanks for the pointer.

1 Like

I am using rnix-lsp on neovim. It checks syntax real-time but doesn’t do much autocomplete. Often only local variables and builtins.

The way I do is to open an nix-repl to inspect the things. It’s not that convenient but works.

BTW its really hard to do this kind of tooling on a lazy evaluated dynamic language. Haskell, for example, is also lazy but an tool like this can inspect this type of thing using only the type mapping.

Because of the build nature of nix its also hard to not trigger a derivation, at least for now. Maybe some type of flag or environment variable can disable derivation realization on a session.

–dry-run can work as a eval but not build option?

The examples look great, I am excited to try this out once it’s merged <3

1 Like

I am working on getting Nix support in Helix. It’s a tree-sitter based editor so it should be pretty great when it’s done, but I’m having issues with the lsp not working. If I can figure it out, it might be a good option, although helix is still quite young.

2 Likes

Give it a try?

We complete items in with expressions in nixd.

Also all kinds of nixpkgs module systems & goto declaration (with docs)

home-options-decl

nixos-option-docs

2 Likes