From bd83eab6220226085c82e637931a7ae3863d9893 Mon Sep 17 00:00:00 2001 From: Matthieu Coudron Date: Thu, 22 Sep 2022 10:39:55 +0200 Subject: [PATCH] programs.neovim: default to init.lua (#3233) We change the current logic: instead of writing an init.vim which loads lua/init-home-manager.lua, we write an init.lua that sources init.vim This commit also avoids writing any of these files if the plugins have no config. --- modules/programs/neovim.nix | 47 +++++++++++-------- tests/modules/programs/neovim/no-init.nix | 8 +++- .../modules/programs/neovim/plugin-config.vim | 1 - 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/modules/programs/neovim.nix b/modules/programs/neovim.nix index 7123f07a4..a5bcc4630 100644 --- a/modules/programs/neovim.nix +++ b/modules/programs/neovim.nix @@ -35,10 +35,10 @@ let pluginWithConfigType = types.submodule { options = { config = mkOption { - type = types.lines; + type = types.nullOr types.lines; description = "Script to configure this plugin. The scripting language should match type."; - default = ""; + default = null; }; type = mkOption { @@ -326,7 +326,7 @@ in { defaultPlugin = { type = "viml"; plugin = null; - config = ""; + config = null; optional = false; runtime = { }; }; @@ -337,7 +337,7 @@ in { allPlugins; suppressNotVimlConfig = p: - if p.type != "viml" then p // { config = ""; } else p; + if p.type != "viml" then p // { config = null; } else p; neovimConfig = pkgs.neovimUtils.makeNeovimConfig { inherit (cfg) extraPython3Packages withPython3 withRuby viAlias vimAlias; @@ -353,26 +353,33 @@ in { programs.neovim.generatedConfigs = let grouped = lib.lists.groupBy (x: x.type) pluginsNormalized; concatConfigs = lib.concatMapStrings (p: p.config); - in mapAttrs (name: vals: concatConfigs vals) grouped; + configsOnly = lib.foldl + (acc: p: if p.config != null then acc ++ [ (p.config) ] else acc) [ ]; + in mapAttrs (name: vals: lib.concatStringsSep "\n" (configsOnly vals)) + grouped; home.packages = [ cfg.finalPackage ]; - xdg.configFile = mkMerge ( - # writes runtime - (map (x: x.runtime) pluginsNormalized) ++ [{ - "nvim/init.vim" = mkIf (neovimConfig.neovimRcContent != "") { - text = neovimConfig.neovimRcContent + lib.optionalString - (hasAttr "lua" config.programs.neovim.generatedConfigs) - "lua require('init-home-manager')"; - }; - "nvim/lua/init-home-manager.lua" = - mkIf (hasAttr "lua" config.programs.neovim.generatedConfigs) { - text = config.programs.neovim.generatedConfigs.lua; + xdg.configFile = + let hasLuaConfig = hasAttr "lua" config.programs.neovim.generatedConfigs; + in mkMerge ( + # writes runtime + (map (x: x.runtime) pluginsNormalized) ++ [{ + "nvim/init.vim" = mkIf (neovimConfig.neovimRcContent != "") { + text = neovimConfig.neovimRcContent; }; - "nvim/coc-settings.json" = mkIf cfg.coc.enable { - source = jsonFormat.generate "coc-settings.json" cfg.coc.settings; - }; - }]); + "nvim/init.lua" = let + luaRcContent = + lib.optionalString (neovimConfig.neovimRcContent != "") + "vim.cmd.source ${config.xdg.configHome}/nvim/init.vim" + + lib.optionalString hasLuaConfig + config.programs.neovim.generatedConfigs.lua; + in mkIf (luaRcContent != "") { text = luaRcContent; }; + + "nvim/coc-settings.json" = mkIf cfg.coc.enable { + source = jsonFormat.generate "coc-settings.json" cfg.coc.settings; + }; + }]); programs.neovim.finalPackage = pkgs.wrapNeovimUnstable cfg.package (neovimConfig // { diff --git a/tests/modules/programs/neovim/no-init.nix b/tests/modules/programs/neovim/no-init.nix index 3ec9b0670..02dd34a08 100644 --- a/tests/modules/programs/neovim/no-init.nix +++ b/tests/modules/programs/neovim/no-init.nix @@ -12,10 +12,14 @@ with lib; withRuby = false; extraPython3Packages = (ps: with ps; [ jedi pynvim ]); + + # plugins without associated config should not trigger the creation of init.vim + plugins = with pkgs.vimPlugins; [ fugitive ({ plugin = vim-sensible; }) ]; }; nmt.script = '' - vimrc="home-files/.config/nvim/init.vim" - assertPathNotExists "$vimrc" + nvimFolder="home-files/.config/nvim" + assertPathNotExists "$nvimFolder/init.vim" + assertPathNotExists "$nvimFolder/init.lua" ''; }; } diff --git a/tests/modules/programs/neovim/plugin-config.vim b/tests/modules/programs/neovim/plugin-config.vim index 8b5f35465..8f2e10621 100644 --- a/tests/modules/programs/neovim/plugin-config.vim +++ b/tests/modules/programs/neovim/plugin-config.vim @@ -1,4 +1,3 @@ - " plugin-specific config autocmd FileType c setlocal commentstring=//\ %s autocmd FileType c setlocal comments=://