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

Hi,

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 [
    [...]
    nvim-lspconfig
    {
        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.

Hi,

I’m trying to do the same thing as you, managing plugins configuration in lua. In the folder where I store my configurations (your plugins folder), i have a init.lua where I use require("plugin-config") for all my plugins. I import this file using extraLuaConfig in my default.nix.

My issue is that in ~/.config/nvim/plugins, I only have the init.lua file with the symlink, not the configuration files.

I assume this is because nix doesn’t recognize them as dependencies so they don’t get copied in the store. I’m quite new to NixOS so this could be related to something else.
Did you import your plugins folder somewhere in nix ?

Thanks in advance.

See

Nixcats : GitHub - BirdeeHub/nixCats-nvim: A framework for configuring neovim like neovim via nix, and having multiple config profiles. (with example config(s) and in-editor help)
&
NV : GitHub - NicoElbers/nv: A patcher that takes in a regular lua neovim config and makes it nix compatible

1 Like

I think the issue will be with importing your lua files in extraLuaConfig. I don’t think you can “require” other lua modules if you do it this way because the required lua files won’t be in the same directory in the nix store.

You can see my configuration here.

To manage your configuration entirely in lua, I’d suggest installing plugins with nix, configuring them in lua, and symlinking your entire lua configuration to your home directory. This way you can still import lua modules with “require”. You can symlink your configuration to your home with the snippet I posted above.

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

Because you no longer have a plugin manager to load all you plugin configurations, you can make use of neovim’s runtimepath instead. Neovim will automatically load all of the modules in ~/.config/nvim/plugin and ~/.config/nvim/after/plugin (loaded afterwards) at startup, so you can put your plugin configurations in there.

Each plugin configuration should be equivalent to the config field in a Lazy plugin specification.

If you do this you would no longer need the require("plugin_config") because all your plugins would get automatically loaded at startup.

Hope that solves your problem!

I managed to get it working using @holden-c repo. I had to restructure everything because I didn’t understand how nvim works.

I was already symlinking my nvim direcory like in your snippet but I was getting some read-only files errors due to my previous configuration. I had to delete my current ~/.config/nvim directory for home-manager to actually apply the symlink, and now, it seems to be working fine.

Thanks for your help !

1 Like