I’m configuring my system with NixOS for the first time, and struggling a bit to set up my nixvim config correctly. I’m using flakes for my system config and Home manager. I’m also using the 23.11 sources for the system pkgs, home manager and nixvim.
The problem I’m having is specifically with the luasnip nvim plugin, which I’m trying to use along with cmp. Enabling luasnip via the nixvim exposed option or adding it to the extra packages and manually intializing it with lua config both fail to make luasnip available to be called in my cmp keybind. I’ve inspected the produced init.lua
file to verify that both approaches do in fact reference luasnip with require("luasnip")
, however running the defined function throws the following error: attempt to index global 'luasnip' (a nil value)
.
I have limited experience with nvim, lua and nix, but similar configs on github seem successful just by enabling luasnip through the nixvim option and calling it in the same fashion as I do. What am I missing here?
My nixvim config:
{ inputs, pkgs, ... }: {
imports = [
inputs.nixvim.homeManagerModules.nixvim
];
programs.nixvim = {
enable = true;
colorschemes.tokyonight.enable = true;
options = {
relativenumber = true;
shiftwidth = 2;
};
extraConfigLua = ''
require("luasnip").setup({})
'';
# Set leader key to space
globals.mapleader = " ";
############### Plugins ###############
plugins = {
# Language service providers
lsp = {
enable = true;
servers = {
# Nix
nil_ls.enable = true;
};
};
# Handy code snippets
# luasnip.enable = true;
# Completion engine
cmp_luasnip.enable = true;
cmp-nvim-lsp.enable = true;
nvim-cmp = {
enable = true;
autoEnableSources = true;
# snippet.expand = "luasnip";
sources = [
{name = "nvim_lsp";}
{name = "path";}
{name = "buffer";}
{name = "luasnip";}
];
mapping = {
"<CR>" = "cmp.mapping.confirm({ select = true })";
"<Tab>" = {
action = ''
function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expandable() then
luasnip.expand()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end
'';
modes = [ "i" "s" ];
};
};
};
};
# Plugins that aren't exposed through nixvim
extraPlugins = builtins.attrValues {
inherit (pkgs.vimPlugins)
luasnip;
};
};
}