Neovim And Nixos....Star-crossed lovers?

Okay, I’ve given up trying to work this out, so a really stupid question:

How do I install a properly spec’d version of Neovim on Nixos?

And an even more impertinent question:

If I am the sole user of my PC, does it really matter if I install it in my system config?

And a controversial statement to conclude:

I don’t understand why people do 2hr videos explaining things, with Lilliputian text.
Surely expository material is better served as plain text?

1 Like

What do you mean by that? Have you looked at the programs.neovim options?

1 Like

Hi Justinas,

I am caught between two stools. I am not competent enough to use Nixos, but I’m trying to give it a go as the alternative, (Fedora), while an exceptional OS, is markedly inferior.

I am really clutching at this idea, a boil-in-the-bag config:

1 Like

Applying the config that the video refers to verbatim will be challenging. With Nix you will probably want to use the Nix-native way to generate a Neovim config (I use the “oldschool” VimL configs, I am not sure about using Lua) and install plugins.

I will not be able to translate the entire large config for you, but once again, please look at programs.neovim, particularly programs.neovim.configure which has an example of how to add plugins and custom configuration file.

1 Like

Thank you Justinas. I will indeed

Here is how i use nix with my neovim config:

I install plugins using nixpkgs but configure neovim using lua. Works pretty flawlessly for me.

2 Likes

This way is ok but I prefer bundling the configuration with the plugin rather than putting everything in extraConfig although the tradeoff is that you don’t get highlighting. It also allows you to reference binary paths explicitly in the configuration with nix rather than relying on the binary in a global sense.

Here’s my configuration

As for OP, I would recommend using home-manager which will allow you to do what me and sunsunsunsunsunsun are doing.

1 Like

Thanks Colin…

I put the neovim.nix configuration in .config/nixpkgs/

When I attempt to reference it

13 { config, pkgs, ... }:
 12 
 11 {
 10   # Home Manager needs a bit of information about you and the
  9   # paths it should manage.
  8   home.username = "marcus";
  7   home.homeDirectory = "/home/marcus";
  6  
  5 programs.neovim = {
  4   enable = true;
  3   extraConfig = lib.fileContents ../.config/nixpkgs/neovim.nix;
  2     set number relativenumber
  1   '';
14  };

It throws an error


[marcus@nixos:~]$ home-manager switch
error: syntax error, unexpected ID, expecting '.' or '='

       at /home/marcus/.config/nixpkgs/home.nix:12:9:

           11|   extraConfig = lib.fileContents ../.config/nixpkgs/neovim.nix;
           12|     set number relativenumber
             |         ^
           13|   '';
(use '--show-trace' to show detailed location information)

So lib.fileContents takes a file as input and produces a string, therefore you need to concatenate the multiline string below it (which is missing the opening '' btw) to produce a resulting string for extraConfig. Try this below and see if it fixes your issue :slightly_smiling_face:

programs.neovim = {
  enable = true;
  extraConfig = (lib.fileContents ../.config/nixpkgs/neovim.nix) + ''
    set number relativenumber
  '';
};

I am just apeing what the Wiki says Collin. Not very well obviously

With Home Manager

The Home Manager module does not expose many configuration options. Therefore, the easiest way to get started is to use the extraConfig option. You can copy your old config or directly load your default Neovim config via:

programs.neovim.extraConfig = lib.fileContents ../path/to/your/init.vim;

programs.neovim = {
  1   enable = true;
  2   extraConfig = (lib.fileContents ../.config/nixpkgs/neovim.nix) + '' 
  3   set number relativenumber
  4   '';
  5 };
home-manager switch
error: undefined variable 'lib'

       at /home/marcus/.config/nixpkgs/home.nix:11:18:

           10|   enable = true;
           11|   extraConfig = (lib.fileContents ../.config/nixpkgs/neovim.nix) + ''
             |                  ^
           12|   set number relativenumber
(use '--show-trace' to show detailed location information)

Syntax and scope issues aside (they’re what are producing the error messages in this comment and subsequent iterations), you’re attempting to append verbatim Nix source to your Neovim configuration file. That won’t work, of course, because Neovim expects Lua or VimL in its configuration files, not Nix code.

What you have in that ~/.config/nixpkgs/neovim.nix file is a Home Manager module. If you want to use Home Manager to configure Neovim, try getting started with Home Manager.

But you’re gonna need to slow down and try to understand more of what you’re piecing together here, I think. Have you ever done the interactive Nix tutorial here?

Hi PXC. I thought I’d post some low level questions and see how I got on. My use cases are fairly minimal, but for now it is not happening. I think Nixos should correctly be described as a spinoff of Linux, rather than Linux per se. F-linux if you like.

I simply have other priorities right now, but in 6 months time I will try and address the deficiencies in my knowledge.

Thank you for the resource - bookmarked for reference.

I really want to do something similar, but the lack of intellisense is a little disappointing. It’s not only the syntax highlighting (which you could get back by putting the config in a different file), but also autocomplete and the like.

Another option is to use something like Nix2Vim and try to write all of your config in native Nix.

It’s worked for me with some mixed results. Some configs are close to fully native, while others are mostly chunks of Lua code or areas where I just import separate Lua files.

The upsides are:

  • Much more config in Nix code, including highlighting and syntax checks
  • Build and bundle personal config into Neovim package itself
  • Ensure that an external program required by Neovim is installed

The downsides are:

  • Configuration is messy and hard to follow, switching between Nix and Lua
  • No documentation which can be frustrating
  • Requires rebuild with every change
2 Likes

Well I can get the syntax highlighting if I use lua files but I couldn’t resolve nix paths if I did that. Or at least, I’m not aware of how to do it. Apparently there is a way to get embedded highlighting with treesitter but I have no idea how to go about implementing that.

https://www.reddit.com/r/NixOS/comments/ymdgm9/how_can_i_get_vim_syntax_highlighting_within_a/

2 Likes

I have written several guides on using Nix, Neovim, and Home Manager together. Here is the first and most simple in the series:

1 Like

FWIW I just builtins.readFile the Lua file, and do a trivial string replacement to insert any Nix paths I need. For example, nix-dotfiles/home-manager/nvim/nvim-dap-python.lua at d3ec6b02c3faa327e104459b481243f8be95c53d · Smaug123/nix-dotfiles · GitHub and nix-dotfiles/home-manager/home.nix at d3ec6b02c3faa327e104459b481243f8be95c53d · Smaug123/nix-dotfiles · GitHub . That way, you’re in a genuine Lua file which you can use an IDE to edit. (I do the same for shell scripts.)

There are more idiomatic ways which I’ll adopt when I get time (home-manager: extract inline shell script to file · nix-community/home-manager@b787726 · GitHub is an example).

What I ended up doing is writing a small nix module for nix → lua generation, and a small lua “runtime” for my config. Turns out, 95% of a neovim configuration (arbitrary nesting of keybinds, autocmds, vim.? assignments, etc) can be described declaratively, and I’m fine with writing to lua strings for the rest.