Difference between Nix-configured neovim and normal neovim

This config file should set my theme to gruvbox, and set trailing whitespace to be highlighted in red. And, in fact, it does in a standard neovim setup. But when trying to set this up in nix, the whitespace highlighting doesn’t get set up unless I go to the init.vim being used and source it manually.

colorscheme gruvbox

augroup vimrc
        " highlight trailing whitespace
        autocmd BufRead,SourcePre,WinNew * match trailingwhitespace /\s\+$/
augroup end

highlight trailingWhitespace ctermbg=Red guibg=red

how do you configure your neovim ? nixos wraps the binary with its config via -u thus ignoring your dotfiles. You can check out :scriptnames to see what is loaded

The issue here is that if I configure it with nix, the highlighting doesn’t work unless I manually source the config file (the one I can find be looking at readlink $(which nvim)). Whereas, if I configure it without nix, the highlighting works just fine.

As Matthieu said. If configured through nix, xVim will not load the regular init.vim.

I’m am aware, the init.vim it loads is /nix/store/jz8pd9pqch2q3bv79dv4h67c306bmccj-init.vim and it looks like this

" configuration generated by NIX
set nocompatible

set packpath^=/nix/store/lhydx4d1i9x2w09wfldm9ifsmcwmr123-vim-pack-dir
set runtimepath^=/nix/store/lhydx4d1i9x2w09wfldm9ifsmcwmr123-vim-pack-dir

colorscheme gruvbox

augroup vimrc
        " highlight trailing whitespace
        autocmd BufRead,SourcePre,WinNew * match trailingwhitespace /\s\+$/
augroup end

highlight trailingWhitespace ctermbg=Red guibg=red

edit: Using this with normally configured neovim does in fact reproduce the same behaviour.

But using this with vim-plug works as expected, trailing spaces are highlighted without needing to source the init.vim manually.

set nocompatible

call plug#begin()
Plug 'morhetz/gruvbox'
call plug#end()

colorscheme gruvbox

augroup vimrc
        " highlight trailing whitespace
        autocmd BufRead,SourcePre,WinNew * match trailingwhitespace /\s\+$/
augroup end

highlight trailingWhitespace ctermbg=Red guibg=red

try to see if gruvbox is available in the “start” subdirectory of

/nix/store/lhydx4d1i9x2w09wfldm9ifsmcwmr123-vim-pack-dir

could you link/share the nix expression you use to configure neovim ?

Yes it is in the start directory, and gruvbox itself loads just fine. It the highlight the breaks because of gruvbox. I can share a minimal example, sure. However, I did find a workaround.

So given that it works with normal neovim configuration using plug, I decided to see if it would work using plug in nix (an option I have always wondered why it existed). And sure enough, moving gruvbox from packages.myPackage.start to plug.plugins made the issue go away.

I’m interested in this as well, just out of curiosity. Could you maybe share a *.nix file that I can just use instead of my own neovim.nix so I can check if I can reproduce the issue?

Here are derivations you can use to try this for yourself. bad will not highlight trailing whitespace until you manually source the init.vim (/nix/store/hlq6ywnyxmn9b0gryp9qdwmqk8l6ip2g-init.vim)

with builtins;
let
  nixpkgs =
    fetchTarball
      { url = "https://github.com/NixOS/nixpkgs/archive/8d8a28b47b7c41aeb4ad01a2bd8b7d26986c3512.tar.gz";
        sha256 = "1s29nc3ppsjdq8kgbh8pc26xislkv01yph58xv2vjklkvsmz5pzm";
      };

  pkgs =
    import nixpkgs
      { overlays =
          [ (_: super:
               let
                 customRC =
                   ''
                   colorscheme gruvbox

                   augroup vimrc
                     " highlight trailing whitespace
                     autocmd BufRead,SourcePre,WinNew * match trailingwhitespace /\s\+$/
                   augroup end

                   highlight trailingWhitespace ctermbg=Red guibg=red
                   '';

                 inherit (super.vimPlugins) gruvbox;
               in
               { neovim-good =
                   super.neovim.override
                     { configure =
                         { inherit customRC;
                           plug.plugins = [ gruvbox ];
                         };
                     };

                 neovim-bad =
                   super.neovim.override
                     { configure =
                         { inherit customRC;
                           packages.gruvbox.start = [ gruvbox ];
                         };
                     };
               }
            )
          ];
      };
in
{ good = pkgs.neovim-good;
  bad = pkgs.neovim-bad;
}
2 Likes

This is puzzling. I’ll share what I found so far but I also don’t know what causes this.

I believe I know why it works with vim-plug, the explanation is at the bottom of the post

When using the packages option the highlight group is cleared at some point. Running :hi trai followed by trying to autocomplete the highlight group name yields no results. Dumping some highlight group related commands into a buffer with :put=execute('verbose highlight') shows, among many other things:

jsonString     xxx links to GruvboxFg1
	Last set from /nix/store/89m1lc0dy0ayxlbz35y53qyqxy3vyizp-vimplugin-gruvbox-2020-07-03/share/vim-plugins/gruvbox/colors/gruvbox.vim line 1400
trailingWhitespace xxx cleared
Ignore         xxx ctermfg=0 guifg=bg
	Last set from /nix/store/j00jh9z55wlcldb1qy8xv66j30lv4qy0-neovim-unwrapped-0.5.0/share/nvim/runtime/syntax/syncolor.vim line 42

I vaguely remembered that color schemes interfere with custom highlight groups, and indeed this gist explains it in a bit more detail. Additionally, the Gruvbox source, like most color scheme plugins, clears the existing highlight groups.

Of course none of this explains the issue since the :highlight command happens after the colorscheme command. On the other hand, the plugin file is sourced after the init.vim in both Neovim configurations.

Bad

  1: /nix/store/avfqw388zpaw3kxrp4al5iwmwyc4njqk-init.vim
  2: /nix/store/89m1lc0dy0ayxlbz35y53qyqxy3vyizp-vimplugin-gruvbox-2020-07-03/share/vim-plugins/gruvbox/colors/gruvbox.vim
  ...

Good



  1: /nix/store/752v4pldlg9syn9m74h6jnrkbi9iqcmf-init.vim
  2: /nix/store/99k9mgg1x3hvwavqgmby9jqsa5w7b4zr-vimplugin-vim-plug-2021-04-30/share/vim-plugins/vim-plug/plug.vim
  3: /nix/store/j00jh9z55wlcldb1qy8xv66j30lv4qy0-neovim-unwrapped-0.5.0/share/nvim/runtime/filetype.vim
  4: /nix/store/j00jh9z55wlcldb1qy8xv66j30lv4qy0-neovim-unwrapped-0.5.0/share/nvim/runtime/ftplugin.vim
  5: /nix/store/j00jh9z55wlcldb1qy8xv66j30lv4qy0-neovim-unwrapped-0.5.0/share/nvim/runtime/indent.vim
  6: /nix/store/j00jh9z55wlcldb1qy8xv66j30lv4qy0-neovim-unwrapped-0.5.0/share/nvim/runtime/syntax/syntax.vim
  7: /nix/store/j00jh9z55wlcldb1qy8xv66j30lv4qy0-neovim-unwrapped-0.5.0/share/nvim/runtime/syntax/synload.vim
  8: /nix/store/j00jh9z55wlcldb1qy8xv66j30lv4qy0-neovim-unwrapped-0.5.0/share/nvim/runtime/syntax/syncolor.vim
  9: /nix/store/89m1lc0dy0ayxlbz35y53qyqxy3vyizp-vimplugin-gruvbox-2020-07-03/share/vim-plugins/gruvbox/colors/gruvbox.vim
...

The only thing I find interesting is that there are some syntax related runtime files that are sourced before Gruvbox loads in the working configuration.

Other than that, the next step would probably be to look at what vim-plug actually does. Clearly the order in which files are sourced seems to matter here.

Just when I was about to give up and post this, I took another look at this PR . So I added:

colorscheme gruvbox
syntax enable
highlight trailingWhitespace ctermbg=Red guibg=red

and what do you know, :hi trai now works.

Here’s what help says

The `:syntax enable` command will keep most of your current color settings.
This allows using `:highlight` commands to set your preferred colors before or
after using this command.  If you want Vim to overrule your settings with the
defaults, use: >

Not sure how this now relates to the following highlight group settings and the interactions with the color scheme but I think this at least seems to explain why it works with vim-plug since it seems to include additional syntax enable calls. I could imagine that if packages would source the plugins after loading the syntax runtime files it might also work. I don’t know if they automatically enable syntax in the same way that the command does it, but it’d be worth a shot.

1 Like