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.
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.
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.
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.
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.
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.