How to "require" other lua files in my Neovim plugin config?


I want to maintain my plugin setup functions in lua, but manage their install using nix. I don’t really want to translate/embed my lua to/in nix, because I want LSP support and formatting when I configure my Neovim config.

I am aiming to source a lua config file per plugin in my default.nix, a bit like this:

programs.neovim.plugins = with pkgs.vimPlugins [
        plugin = telescoep-nvim;
        type = "lua";
        config = builtins.readFile(./nvim/plugins/telescope.lua)
        plugin = oil-nvim;
        type = "lua";
        config = builtins.readFile(./nvim/plugins/oil.lua)

The issue, is that I would like to “require” other lua files in my plugin config files, so that I can maintain a modular structure (e.g. import from mappings.lua, autocommands.lua etc…), however the plugin config file ends up in the nix store and so importing the other lua modules doesn’t work.

Does anyone have a solution for this problem?

I could probably do some lua wizardry to import modules with an absolute path, but it seems a bit hacky.

Many thanks for any help

1 Like

I came up with a solution that works for me in the end:

I install my plugins using nix, without any reference to their config files:

programs.neovim.plugins = with pkgs.vimPlugins [telescope.nvim oil-nvm ...];

Then I place my plugin configuration files (these contain the equivalent of the ‘config’ function for a Lazy plugin install) in nvim/plugin, where nvim is my neovim lua directory. It gets symlinked to the correct location in default.nix with:

home.file."./config/nvim" = {
    source = ./nvim;
    recursive = true;

nvim/plugin is automatically run by neovim on startup as part of its runtimepath, so all my plugins config files are run when I start neovim.
In this way I can maintain all the configurability of a regular neovim installation, but install my plugins with nix.