From 8d3fe1366b8b6c4748a847d1e3b41a73269caaee Mon Sep 17 00:00:00 2001 From: Matthieu Coudron Date: Thu, 27 Jan 2022 16:27:35 +0100 Subject: [PATCH] neovim: support different configuration languages (#2637) Plugins now accept a "type" element describing the language (viml, lua , teal, fennel, ...) in which they are configured. The configuration of the different plugins is aggregated per language and made available as a key in the attribute set `programs.neovim.generatedConfigs` For instance if you want to configure a lua package: ``` programs.neovim.plugins = [ { plugin = packer-nvim; type = "lua"; config = '' require('packer').init({ luarocks = { python_cmd = 'python' -- Set the python command to use for running hererocks }, }) ''; } ] ``` and you can save the generated lua config to a file via ``` xdg.configFile = { "nvim/init.generated.lua".text = config.programs.neovim.generatedConfigs.lua; }; ``` --- modules/programs/neovim.nix | 56 +++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/modules/programs/neovim.nix b/modules/programs/neovim.nix index 24da73c91..97e84ba88 100644 --- a/modules/programs/neovim.nix +++ b/modules/programs/neovim.nix @@ -20,10 +20,19 @@ let options = { config = mkOption { type = types.lines; - description = "vimscript for this plugin to be placed in init.vim"; + description = + "Script to configure this plugin. The scripting language should match type."; default = ""; }; + type = mkOption { + type = + types.either (types.enum [ "lua" "viml" "teal" "fennel" ]) types.str; + description = + "Language used in config. Configurations are aggregated per-language."; + default = "viml"; + }; + optional = mkEnableOption "optional" // { description = "Don't load by default (load with :packadd)"; }; @@ -141,6 +150,28 @@ in { ''; }; + generatedConfigs = mkOption { + type = types.attrsOf types.lines; + visible = true; + readOnly = true; + example = literalExpression '' + { + viml = ''' + " Generated by home-manager + set packpath^=/nix/store/cn8vvv4ymxjf8cfzg7db15b2838nqqib-vim-pack-dir + set runtimepath^=/nix/store/cn8vvv4ymxjf8cfzg7db15b2838nqqib-vim-pack-dir + '''; + + lua = ''' + -- Generated by home-manager + vim.opt.background = "dark" + '''; + }''; + description = '' + Generated configurations with as key their language (set via type). + ''; + }; + package = mkOption { type = types.package; default = pkgs.neovim-unwrapped; @@ -270,12 +301,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; + }) cfg.plugins; + suppressNotVimlConfig = p: + if p.type != "viml" then p // { config = ""; } else p; + neovimConfig = pkgs.neovimUtils.makeNeovimConfig { inherit (cfg) extraPython3Packages withPython3 withNodeJs withRuby viAlias vimAlias; configure = cfg.configure // moduleConfigure; - plugins = cfg.plugins - ++ optionals cfg.coc.enable [ pkgs.vimPlugins.coc-nvim ]; + plugins = (map suppressNotVimlConfig pluginsNormalized) + ++ optionals cfg.coc.enable [{ plugin = pkgs.vimPlugins.coc-nvim; }]; customRC = cfg.extraConfig; }; @@ -291,6 +335,12 @@ in { programs.neovim.generatedConfigViml = neovimConfig.neovimRcContent; + programs.neovim.generatedConfigs = let + grouped = lib.lists.groupBy (x: x.type) pluginsNormalized; + concatConfigs = + lib.concatMapStrings (p: builtins.trace p.plugin.name p.config); + in mapAttrs (name: vals: concatConfigs vals) grouped; + home.packages = [ cfg.finalPackage ]; xdg.configFile."nvim/init.vim" = mkIf (neovimConfig.neovimRcContent != "") {