Neovim Lua config via home-manager

Hello,

I’m trying to set up my existing Lua config for Neovim through home-manager.

I am using the programs.neovim.extraLuaConfig for the basic config, then programs.neovim.plugins to manage the plugins. For each plugin that has a specific config, I create a separate Lua file, and use builtins.readFile to access it in the home-manager config.

For example, with mason-lspconfig.nvim:

  • In home.nix:
programs.neovim.plugins = with pkgs.vimPlugins [
    [...]
    nvim-lspconfig
    {
        plugin = mason-nvim;
        type = "lua";
        config = builtins.readFile(./nvim/plugins/mason.lua)
    }
    {
        plugin = mason-lspconfig-nvim;
        type = "lua";
        config = builtins.readFile(./nvim/plugins/mason-lspconfig.lua)
    }
    [...]
];
  • In mason.lua:
require("mason").setup()
  • In mason-lspconfig.lua:
require("mason-lspconfig").setup() {
    ensure_installed = {
        "lua_ls", 
        "clangd",
        "marksman",
        "nil_ls",
        "jedi_language_server",
        "svelte",
        "taplo",
        "yamlls",
        "bashls",
    },
}
  • In turn generating the following lines in init.lua:
[...]
require("mason").setup()

require("mason-lspconfig").setup() {
    ensure_installed = {
        "lua_ls", 
        "clangd",
        "marksman",
        "nil_ls",
        "jedi_language_server",
        "svelte",
        "taplo",
        "yamlls",
        "bashls",
    },
}

However, for some unknown reason, the line require("mason-lspconfig").setup() throws out an error, saying attempt to call a nil value, as if the package was not installed.

Thank you for any help provided !

Well, it turns out it wasn’t an installation problem, but rather a syntax error.

For some reason, the above syntax worked when I used nvim on Fedora and backed up the init.lua directly. However, on NixOS, I had to change it to look like this:

require("mason-lspconfig").setup({
    ensure_installed = {
        "lua_ls", 
        "clangd",
        "marksman",
        "nil_ls",
        "jedi_language_server",
        "svelte",
        "taplo",
        "yamlls",
        "bashls",
    },
})

The only difference with the above being that the {} block is passed as a parameter to setup(), which has me wonder how it could work in the original config, since it shouldn’t be valid syntax. Most likely, the bump in the nvim version is the cause of the problem.