Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions modules/programs/neovim.nix
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ in
'';
};

sideloadInitLua = mkOption {
type = types.bool;
default = false;
description = ''
Whether to sideload the content of {var}`initLua` without generating a {file}`init.lua`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reusing sideload here might seem tautological: if the name of the option was not clear, it wont help.

I would suggest (feel free to change/improve I am not native):


Enable to avoid writing the config to the default location {file}`$XDG_CONFIG_HOME/nvim/init.lua`.
This is useful if you want to manage your own init.lua imperatively.

Instead the home-manager neovim configuration will be pre-loaded via arguments in the neovim wrapper,

'';
};

initLua = mkOption {
type = types.lines;
default = "";
Expand Down Expand Up @@ -495,13 +503,6 @@ in
opt = [ ];
};
};

# This is a hack to avoid breaking config for users that dont want an init.lua to get generated
# See https://github.com/nix-community/home-manager/pull/8734
# we basically check if the generated wrapper lua config has any user-set config
# if not HM avoids creating an init.lua
# this makes the logic harder to understand and maintain so hopefully we can find a way out
wrapperHasUserConfig = wrappedNeovim'.luaRcContent != wrappedNeovim'.providerLuaRc;
in
{
programs.neovim = {
Expand Down Expand Up @@ -530,9 +531,9 @@ in

programs.neovim.extraPackages = mkIf cfg.autowrapRuntimeDeps vimPackageInfo.runtimeDeps;

programs.neovim.extraWrapperArgs = mkIf (!wrapperHasUserConfig) [
programs.neovim.extraWrapperArgs = mkIf (cfg.sideloadInitLua && cfg.initLua != "") [
"--add-flags"
''--cmd 'lua dofile("${pkgs.writeText "wrapper-init-lua" wrappedNeovim'.luaRcContent}")' ''
''--cmd 'lua dofile("${pkgs.writeText "wrapper-init-lua" cfg.initLua}")' ''
];

programs.neovim.initLua =
Expand All @@ -550,7 +551,7 @@ in
null;
in
lib.mkMerge [
(lib.mkIf wrapperHasUserConfig (
(lib.mkIf (wrappedNeovim'.luaRcContent != "") (
# we want it to appear rather early
lib.mkOrder 200 wrappedNeovim'.luaRcContent
))
Expand All @@ -577,6 +578,7 @@ in
++ [
{
"nvim/init.lua" = mkIf (cfg.initLua != "") {
enable = !cfg.sideloadInitLua;
text = cfg.initLua;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just add enable = !cfg.wrapGeneratedConfigs or in your case (builtins.elem cfg.wrapGeneratedConfigs [ false "user" ] . This way the user can still see the generated config and use it somewhere else but the actual init.lua wont be created.

};

Expand Down
5 changes: 3 additions & 2 deletions tests/modules/programs/neovim/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
neovim-runtime = ./runtime.nix;
neovim-wrapper-args = ./wrapper-args.nix;

# waiting for a nixpkgs patch
neovim-no-init-vim = ./no-init.nix;
neovim-no-init-vim = ./no-init-vim.nix;
neovim-extra-lua-init = ./extra-lua-init.nix;
neovim-extra-lua-default = ./extra-lua-default.nix;
neovim-extra-lua-empty-plugin = ./extra-lua-empty-plugin.nix;

neovim-sideload-init-lua = ./sideload-init-lua.nix;
}
10 changes: 9 additions & 1 deletion tests/modules/programs/neovim/extra-lua-default.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
{ lib, ... }:
{
imports = [ ./stubs.nix ];

programs.neovim.enable = true;

nmt.script = ''
nvimFolder="home-files/.config/nvim"
assertPathNotExists "$nvimFolder/init.lua"
initLua="home-files/.config/nvim/init.lua"
initLuaNormalized="$(normalizeStorePaths "$initLua")"

assertFileContent "$initLuaNormalized" ${builtins.toFile "init.lua-expected" (
lib.trim ''
vim.g.loaded_node_provider=0;vim.g.loaded_perl_provider=0;vim.g.ruby_host_prog='/nix/store/00000000000000000000000000000000-neovim-ruby-env/bin/neovim-ruby-host';vim.g.python3_host_prog='/nix/store/00000000000000000000000000000000-nvim-host-python3/bin/nvim-python3'
''
)}
'';
}
11 changes: 9 additions & 2 deletions tests/modules/programs/neovim/extra-lua-empty-plugin.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ pkgs, ... }:
{ pkgs, lib, ... }:
{
imports = [ ./stubs.nix ];

Expand All @@ -13,7 +13,14 @@
};

nmt.script = ''
nvimFolder="home-files/.config/nvim"
initLua="home-files/.config/nvim/init.lua"
assertPathNotExists "$initLua"
initLuaNormalized="$(normalizeStorePaths "$initLua")"

assertFileContent "$initLuaNormalized" ${builtins.toFile "init.lua-expected" (
lib.trim ''
vim.g.loaded_node_provider=0;vim.g.loaded_perl_provider=0;vim.g.ruby_host_prog='/nix/store/00000000000000000000000000000000-neovim-ruby-env/bin/neovim-ruby-host';vim.g.python3_host_prog='/nix/store/00000000000000000000000000000000-nvim-host-python3/bin/nvim-python3'
''
)}
'';
}
1 change: 1 addition & 0 deletions tests/modules/programs/neovim/extra-lua-init.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
initLuaNormalized="$(normalizeStorePaths "$initLua")"

assertFileContent "$initLuaNormalized" ${builtins.toFile "init.lua-expected" ''
vim.g.loaded_node_provider=0;vim.g.loaded_perl_provider=0;vim.g.ruby_host_prog='/nix/store/00000000000000000000000000000000-neovim-ruby-env/bin/neovim-ruby-host';vim.g.python3_host_prog='/nix/store/00000000000000000000000000000000-nvim-host-python3/bin/nvim-python3'
-- initLua
''}
'';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@
nmt.script = ''
nvimFolder="home-files/.config/nvim"
assertPathNotExists "$nvimFolder/init.vim"
assertPathNotExists "$nvimFolder/init.lua"
'';
}
2 changes: 1 addition & 1 deletion tests/modules/programs/neovim/plugin-config.expected
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package.path = "/nix/store/00000000000000000000000000000000-luajit/share/lua/5.1/?.lua;/nix/store/00000000000000000000000000000000-luajit/share/lua/5.1/?/init.lua".. ";" .. package.path
package.cpath = "/nix/store/00000000000000000000000000000000-luajit/lib/lua/5.1/?.so".. ";" .. package.cpath

vim.g.loaded_node_provider=0;vim.g.loaded_perl_provider=0;vim.g.loaded_ruby_provider=0;vim.g.python3_host_prog='/nix/store/00000000000000000000000000000000-nvim-host-python3/bin/nvim-python3'
vim.g.loaded_node_provider=0;vim.g.loaded_perl_provider=0;vim.g.ruby_host_prog='/nix/store/00000000000000000000000000000000-neovim-ruby-env/bin/neovim-ruby-host';vim.g.python3_host_prog='/nix/store/00000000000000000000000000000000-nvim-host-python3/bin/nvim-python3'
vim.cmd.source "/nix/store/00000000000000000000000000000000-init.vim"

vim.g.Unicode_data_directory="/nix/store/00000000000000000000000000000000-vimplugin-unicode.vim/autoload/unicode"
Expand Down
81 changes: 81 additions & 0 deletions tests/modules/programs/neovim/sideload-init-lua.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{ pkgs, realPkgs, ... }:

{
tests.stubs.wl-clipboard = { };

programs.neovim = {
enable = true;
package = realPkgs.neovim-unwrapped;
vimAlias = true;
withNodeJs = false;
withPython3 = true;
withRuby = false;

sideloadInitLua = true;

extraPython3Packages = (
ps: with ps; [
jedi
pynvim
]
);

# plugins without associated config should not trigger the creation of init.vim
plugins = with pkgs.vimPlugins; [
{
plugin = vim-fugitive;
type = "viml";
config = ''
let g:hmPluginVimlConfig = 'HM_PLUGIN_VIML_CONFIG'
'';
}
{
plugin = vim-sensible;
type = "lua";
config = ''
vim.g.hmPluginLuaConfig = 'HM_PLUGIN_LUA_CONFIG'
'';
}
];

extraConfig = ''
let g:hmExtraConfig = 'HM_EXTRA_CONFIG'
'';

initLua = ''
vim.g.hmInitLua = 'HM_INIT_LUA'
'';
};
nmt.script = ''
nvimFolder="home-files/.config/nvim"
nvimBin="home-path/bin/nvim"
export PATH="$TESTED/home-path/bin:$PATH"
export HOME="$TMPDIR/hm-user"
export XDG_CONFIG_HOME="$TESTED/home-files/.config"

assertNeovimExpr() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to duplicate an existing function. I would rather see the same function used across tests even if you have to modify it. Move it to a neovim.sh that you can source across tests if that helps.

local expr="$1"
local expected_pattern="$2"
local output=$(nvim -i NONE --headless -c "$expr" +q! 2>&1)
local exit_code=$?

if [ $exit_code -ne 0 ]; then
echo "neovim command failed with code: $exit_code and output:"
fail "$output"
elif ! grep "$expected_pattern" <(echo -n "$output") ; then
echo "Expression '$expr' doesn't match expected pattern '$expected_pattern'"
echo "Output:"
fail "$output"

echo "'$output'"
fi
}

assertPathNotExists "$nvimFolder/init.lua"

assertNeovimExpr "echo g:hmPluginVimlConfig" "HM_PLUGIN_VIML_CONFIG"
assertNeovimExpr "echo g:hmExtraConfig" "HM_EXTRA_CONFIG"
assertNeovimExpr "echo g:hmExtraConfig" "HM_EXTRA_CONFIG"
assertNeovimExpr "echo g:hmInitLua" "HM_INIT_LUA"
'';
}
5 changes: 4 additions & 1 deletion tests/modules/programs/neovim/wrapper-args.nix
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ in
nvimBin="home-path/bin/nvim"
export PATH="$TESTED/home-path/bin:$PATH"
export HOME="$TMPDIR/hm-user"
# Load init.lua from $XDG_CONFIG_HOME/nvim/init.lua
export XDG_CONFIG_HOME="$TESTED/home-files/.config"

assertBinaryContains() {
local file="$TESTED/$1"
Expand All @@ -55,7 +57,8 @@ in
assertNeovimExpr() {
local var_name="$1"
local expected_pattern="$2"
local output=$(nvim -i NONE --headless --cmd "echo $var_name" +q! 2>&1)
# Use -c to evaluate the expression after init.lua is loaded
local output=$(nvim -i NONE --headless -c "echo $var_name" +q! 2>&1)
local exit_code=$?

if [ $exit_code -ne 0 ]; then
Expand Down