Neovim lua configuration

Hi, I want to use lua to configure neovim:

  1. make a derivation, that contains all the lua configure files
/nix/store/...lua-config/{init.lua, keymap.lua ...}
  1. custom neovim config directory to the above derivaion, I don’t know how to write this, currently my neovim configuration is as follows:
nvim = neovim.override {                        
  withNodeJs = true;                            
  configure = {                                 
    packages.mypack = with vimPlugins; {        
      start = [                                 
        lightline-vim                                                
      ];                                        
    };                                          
    # customRC = builtins.readFile ./init.nvim;                                
  };                                            
};                                              

this will always add -u option to the nvim wrapper, which makes it impossible to customize the config dir, so how to set the neovim config dir?

Thanks.

this will always add -u option to the nvim wrapper

this is not the case anymore, the packpath is now set in the wrapper via a --cmd flag. home-manager now defaults to init.lua for instance

1 Like

I’m not sure I understand what you’re asking, but I don’t think configure supports passing an init.lua, let alone a directory.

If you don’t care about python, node and what not, you can do what I do: skip this wrapper (pkgs.neovim) and make your own (simpler) wrapper around pkgs.neovim-unwrapped.

{ config, lib, pkgs, ... }:

let
  plugins = with pkgs.vimPlugins;
    [ # plugins here
      undotree gitsigns-nvim
      plenary-nvim
    ];

  pack = pkgs.linkFarm "neovim-plugins"
    (map (pkg:
      { name = "pack/${pkg.pname}/start/${pkg.pname}";
        path = toString pkg;
      }) plugins);

  neovim-wrapped = pkgs.runCommand "${pkgs.neovim-unwrapped.name}"
    { nativeBuildInputs = [ pkgs.makeWrapper ]; }
    ''
      mkdir -p "$out"
      makeWrapper '${pkgs.neovim-unwrapped}/bin/nvim' "$out/bin/nvim" \
        --add-flags "-u ${conf}"
    '';

  # If you want write the config here, or
  # specify a path (will be copied into the nix store), or
  # just don't add the -u flag at all.
  conf = pkgs.writeText "init.lua"
  ''
    -- load the plugins
    opt.packpath = '${pack}'

    -- configure neovim here
    opt.hidden = true
    opt.mouse  = 'a'

    --- etc.
  '';
in

{

  nixpkgs.overlays = [(self: super: { neovim = neovim-wrapped; })];

  # nix build -f '<nixpkgs/nixos>' config.programs.neovim.package for testing
  programs.neovim.package = neovim-wrapped;

}
3 Likes

I currently do the following, I have init.vim but it contains a single line calling my lua config:

programs.neovim = {
  configure = {
    customRC = ''
      luafile ${./nvim.lua}
    '';
  };
};
1 Like

Isn’t it a bit strange to create its own wrapper when the manual specifies a way to configure neovim using customRC? Also for more complex setups where a single file is not enough, one can just package it as a usual plugin (that basically follows the structure of ./config/nvim as far as I understand, but I’m not sure if you can load the script without calling it from customRC).

Note also that NixOs and home managers provides modules.

# In shell.nix
{ pkgs ? import <nixpkgs> {} }:
let
  # List of custom packages
  easygrep = pkgs.vimUtils.buildVimPlugin {
    name = "vim-easygrep";
    src = pkgs.fetchFromGitHub {
      owner = "dkprice";
      repo = "vim-easygrep";
      rev = "d0c36a77cc63c22648e792796b1815b44164653a";
      sha256 = "0y2p5mz0d5fhg6n68lhfhl8p4mlwkb82q337c22djs4w5zyzggbc";
    };
  };
  # Use customRC for simple config but if you want your configuration to be in multiple files, you
  # can just package them as a plugin as plugins respect the hierarchie of .config/nvim file
  # as presented for instance here.
  # https://github.com/nanotee/nvim-lua-guide
  myConfig = pkgs.vimUtils.buildVimPlugin {
    name = "my-config";
    # Create in this directory a file like my-neovim-config/lua/init.lua, to load as below in customRC using
    # lua require("init")
    src = ./my-neovim-config;
  };
  
  # The neovim package with your configuration
  myNeovim = pkgs.neovim.override {
    configure = {
      customRC = ''
        " your custom configuration (vim format). If you like to have multiple files or lua config,
        " you can create a plugin easily as shown above.
        :colorscheme evening
        lua require("init")
      '';
      packages.myPlugins = with pkgs.vimPlugins; {
        start = [
          # vim-go # already packaged plugin
          easygrep # custom package not packaged in nixpkgs
          myConfig # my own configuration
        ];
        opt = [];
      };
    };
  };
in
with pkgs;
pkgs.mkShell {
  buildInputs = [
    myNeovim
  ];
}

2 Likes

This should be work, but has an issue, the nvim.lua cannot require other lua file in the same dir.

I tried this taking configuration as plugin, it does work! And I will use this method for my later confiuration.
Thank you!

Custom a wrapper for neovim is a huge and difficult work to me, since I need python, node and I am not very faimilar with nix, I will take the plugin solution, but thanks anyway.