From 353d21e108790a4823c9fae9812dc953e8994399 Mon Sep 17 00:00:00 2001 From: Matthieu Coudron Date: Tue, 23 Aug 2022 22:02:05 +0200 Subject: [PATCH] neovim runtime (#3168) --- modules/programs/neovim.nix | 87 ++++++++++--------- tests/modules/programs/neovim/default.nix | 4 +- tests/modules/programs/neovim/no-init.nix | 1 - .../modules/programs/neovim/plugin-config.nix | 4 +- .../modules/programs/neovim/plugin-config.vim | 6 +- tests/modules/programs/neovim/runtime.nix | 31 +++++++ 6 files changed, 88 insertions(+), 45 deletions(-) create mode 100644 tests/modules/programs/neovim/runtime.nix diff --git a/modules/programs/neovim.nix b/modules/programs/neovim.nix index ced4a39aa..50f30d922 100644 --- a/modules/programs/neovim.nix +++ b/modules/programs/neovim.nix @@ -6,6 +6,11 @@ let cfg = config.programs.neovim; + fileType = (import ../lib/file-type.nix { + inherit (config.home) homeDirectory; + inherit lib pkgs; + }).fileType; + jsonFormat = pkgs.formats.json { }; extraPython3PackageType = mkOptionType { @@ -52,6 +57,19 @@ let type = types.package; description = "vim plugin"; }; + + runtime = mkOption { + default = { }; + # passing actual "${xdg.configHome}/nvim" as basePath was a bit tricky + # due to how fileType.target is implemented + type = fileType "xdg.configHome/nvim" "nvim"; + example = literalExpression '' + { "ftplugin/c.vim".text = "setlocal omnifunc=v:lua.vim.lsp.omnifunc"; } + ''; + description = lib.mdDoc '' + Set of files that have to be linked in nvim config folder. + ''; + }; }; }; @@ -62,18 +80,6 @@ let optional = false; }; - moduleConfigure = { - packages.home-manager = { - start = remove null (map - (x: if x ? plugin && x.optional == true then null else (x.plugin or x)) - allPlugins); - opt = remove null - (map (x: if x ? plugin && x.optional == true then x.plugin else null) - allPlugins); - }; - beforePlugins = ""; - }; - extraMakeWrapperArgs = lib.optionalString (cfg.extraPackages != [ ]) ''--suffix PATH : "${lib.makeBinPath cfg.extraPackages}"''; extraMakeWrapperLuaCArgs = lib.optionalString (cfg.extraLuaPackages != [ ]) '' @@ -188,8 +194,7 @@ in { { viml = ''' " Generated by home-manager - set packpath^=/nix/store/cn8vvv4ymxjf8cfzg7db15b2838nqqib-vim-pack-dir - set runtimepath^=/nix/store/cn8vvv4ymxjf8cfzg7db15b2838nqqib-vim-pack-dir + map , '''; lua = ''' @@ -249,7 +254,6 @@ in { type = types.lines; default = ""; example = '' - set nocompatible set nobackup ''; description = '' @@ -344,23 +348,25 @@ in { }; config = let - # transform all plugins into an attrset - pluginsNormalized = map (x: - if (x ? plugin) then - x - else { - type = x.type or "viml"; - plugin = x; - config = ""; - optional = false; - }) allPlugins; + defaultPlugin = { + type = "viml"; + plugin = null; + config = ""; + optional = false; + runtime = { }; + }; + + # transform all plugins into a standardized attrset + pluginsNormalized = + map (x: defaultPlugin // (if (x ? plugin) then x else { plugin = x; })) + allPlugins; + suppressNotVimlConfig = p: if p.type != "viml" then p // { config = ""; } else p; neovimConfig = pkgs.neovimUtils.makeNeovimConfig { inherit (cfg) extraPython3Packages withPython3 withRuby viAlias vimAlias; withNodeJs = cfg.withNodeJs || cfg.coc.enable; - configure = cfg.configure // moduleConfigure; plugins = map suppressNotVimlConfig pluginsNormalized; customRC = cfg.extraConfig; }; @@ -384,19 +390,22 @@ in { home.packages = [ cfg.finalPackage ]; - xdg.configFile."nvim/init.vim" = mkIf (neovimConfig.neovimRcContent != "") { - text = neovimConfig.neovimRcContent + (optionalString - (hasAttr "lua" config.programs.neovim.generatedConfigs) '' - lua require('init-home-manager') - ''); - }; - xdg.configFile."nvim/lua/init-home-manager.lua" = - mkIf (hasAttr "lua" config.programs.neovim.generatedConfigs) { - text = config.programs.neovim.generatedConfigs.lua; - }; - xdg.configFile."nvim/coc-settings.json" = mkIf cfg.coc.enable { - source = jsonFormat.generate "coc-settings.json" cfg.coc.settings; - }; + 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; + }; + "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/default.nix b/tests/modules/programs/neovim/default.nix index 8a6c316ea..3950dc8b8 100644 --- a/tests/modules/programs/neovim/default.nix +++ b/tests/modules/programs/neovim/default.nix @@ -1,6 +1,8 @@ { neovim-plugin-config = ./plugin-config.nix; neovim-coc-config = ./coc-config.nix; + neovim-runtime = ./runtime.nix; + # waiting for a nixpkgs patch - # neovim-no-init = ./no-init.nix; + neovim-no-init = ./no-init.nix; } diff --git a/tests/modules/programs/neovim/no-init.nix b/tests/modules/programs/neovim/no-init.nix index 1156529d3..3ec9b0670 100644 --- a/tests/modules/programs/neovim/no-init.nix +++ b/tests/modules/programs/neovim/no-init.nix @@ -6,7 +6,6 @@ with lib; config = { programs.neovim = { enable = true; - package = pkgs.neovim-unwrapped; vimAlias = true; withNodeJs = false; withPython3 = true; diff --git a/tests/modules/programs/neovim/plugin-config.nix b/tests/modules/programs/neovim/plugin-config.nix index 5c26abe88..9647fb5a8 100644 --- a/tests/modules/programs/neovim/plugin-config.nix +++ b/tests/modules/programs/neovim/plugin-config.nix @@ -7,14 +7,14 @@ with lib; programs.neovim = { enable = true; extraConfig = '' - " This should be present in vimrc + " This 'extraConfig' should be present in vimrc ''; plugins = with pkgs.vimPlugins; [ vim-nix { plugin = vim-commentary; config = '' - " This should be present too + " plugin-specific config autocmd FileType c setlocal commentstring=//\ %s autocmd FileType c setlocal comments=:// ''; diff --git a/tests/modules/programs/neovim/plugin-config.vim b/tests/modules/programs/neovim/plugin-config.vim index ee45a863d..8b5f35465 100644 --- a/tests/modules/programs/neovim/plugin-config.vim +++ b/tests/modules/programs/neovim/plugin-config.vim @@ -1,4 +1,6 @@ -" This should be present too + +" plugin-specific config autocmd FileType c setlocal commentstring=//\ %s autocmd FileType c setlocal comments=:// -" This should be present in vimrc + +" This 'extraConfig' should be present in vimrc diff --git a/tests/modules/programs/neovim/runtime.nix b/tests/modules/programs/neovim/runtime.nix new file mode 100644 index 000000000..839b2790d --- /dev/null +++ b/tests/modules/programs/neovim/runtime.nix @@ -0,0 +1,31 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + config = { + programs.neovim = { + enable = true; + plugins = with pkgs.vimPlugins; [ + vim-nix + { + plugin = vim-commentary; + runtime = { + "after/ftplugin/c.vim".text = '' + " plugin-specific config + setlocal commentstring=//\ %s + setlocal comments=:// + ''; + }; + } + ]; + + extraPython3Packages = (ps: with ps; [ jedi pynvim ]); + }; + nmt.script = '' + ftplugin="home-files/.config/nvim/after/ftplugin/c.vim" + assertFileExists "$ftplugin" + ''; + }; +} +