From 63cfc84abeec5f8995945098e30e2d8101133d18 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Tue, 19 Nov 2024 13:55:33 +0000 Subject: [PATCH] lib/modules: add `applyExtraConfig` Used for - mkVimPlugin: extraConfig - mkNeovimPlugin: extraConfig - mkLsp: extraConfig - mkLsp: settings - mkExtension (telecsope): extraConfig --- CONTRIBUTING.md | 4 ++-- lib/modules.nix | 22 +++++++++++++++++ lib/neovim-plugin.nix | 10 +++++--- lib/vim-plugin.nix | 10 +++++--- .../telescope/extensions/_mk-extension.nix | 13 +++++++++- plugins/lsp/language-servers/_mk-lsp.nix | 24 +++++++++++++++---- 6 files changed, 69 insertions(+), 14 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d7599150..1f89843a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -65,7 +65,7 @@ A template plugin can be found in (plugins/TEMPLATE.nix)[https://github.com/nix- | **configLocation** | The location for the Lua configuration. | No | `"extraConfigLuaPre"` if `isColorscheme` then `extraConfigLuaPre`, otherwise `"extraConfigLua"` | | **deprecateExtraOptions** | Indicating whether to deprecate the `extraOptions` attribute. Mainly used for old plugins. | No | `false` | | **description** | A brief description of the plugin. Can also be used for non-normative documentation, warnings, tips and tricks. | No | `null` | -| **extraConfig** | Additional configuration for the plugin. | No | `{}` | +| **extraConfig** | Additional configuration for the plugin. Either an attrset, a function accepting `cfg`, or a function accepting `cfg` and `opts`. | No | `{}` | | **extraOptions** | Module options for the plugin, to be added _outside_ of the `settings` option. These should be Nixvim-specific options. | No | `{}` | | **extraPackages** | Extra packages to include. | No | `[]` | | **extraPlugins** | Extra plugins to include. | No | `[]` | @@ -126,7 +126,7 @@ Such plugins are usually configured via vim globals, but often have no configura | **colorscheme** | The name of the colorscheme. | No | `name` parameter | | **deprecateExtraConfig** | Flag to deprecate extra configuration. | No | `false` | | **description** | A description of the plugin. Can also be used for non-normative documentation, warnings, tips and tricks. | No | `null` | -| **extraConfig** | Extra configuration for the plugin. | No | `cfg: {}` | +| **extraConfig** | Extra configuration for the plugin. Either an attrset, a function accepting `cfg`, or a function accepting `cfg` and `opts`. | No | `{}` | | **extraOptions** | Extra options for the plugin. | No | `{}` | | **extraPackages** | Extra packages to include. | No | `[]` | | **extraPlugins** | Extra plugins to include. | No | `[]` | diff --git a/lib/modules.nix b/lib/modules.nix index 1da6f337..bd492c0b 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -36,6 +36,28 @@ in helpers = self; } // extraSpecialArgs; }; + + /** + Apply an `extraConfig` definition, which should produce a `config` definition. + As used by `mkVimPlugin` and `mkNeovimPlugin`. + + The result will be wrapped using a `mkIf` definition. + */ + applyExtraConfig = + { + extraConfig, + cfg, + opts, + enabled ? cfg.enable or (throw "`enabled` argument not provided and no `cfg.enable` option found."), + }: + let + maybeApply = x: maybeFn: if builtins.isFunction maybeFn then maybeFn x else maybeFn; + in + lib.pipe extraConfig [ + (maybeApply cfg) + (maybeApply opts) + (lib.mkIf enabled) + ]; } // lib.mapAttrs ( name: msg: diff --git a/lib/neovim-plugin.nix b/lib/neovim-plugin.nix index 1b6fc36d..bbb5324b 100644 --- a/lib/neovim-plugin.nix +++ b/lib/neovim-plugin.nix @@ -63,7 +63,7 @@ }: let cfg = config.${namespace}.${name}; - opt = options.${namespace}.${name}; + opts = options.${namespace}.${name}; setupCode = '' require('${luaName}')${setup}(${ @@ -78,7 +78,7 @@ inherit maintainers; nixvimInfo = { inherit description; - url = args.url or opt.package.default.meta.homepage; + url = args.url or opts.package.default.meta.homepage; path = [ namespace name @@ -146,7 +146,11 @@ (lib.optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = lib.mkDefault colorscheme; }) - (extraConfig cfg) + (lib.optionalAttrs (args ? extraConfig) ( + lib.nixvim.modules.applyExtraConfig { + inherit extraConfig cfg opts; + } + )) ] ++ (lib.optionals (!hasConfigAttrs) [ (lib.optionalAttrs callSetup (setLuaConfig setupCode)) diff --git a/lib/vim-plugin.nix b/lib/vim-plugin.nix index eff0e1e0..0b17f8bf 100644 --- a/lib/vim-plugin.nix +++ b/lib/vim-plugin.nix @@ -60,14 +60,14 @@ }: let cfg = config.${namespace}.${name}; - opt = options.${namespace}.${name}; + opts = options.${namespace}.${name}; in { meta = { inherit maintainers; nixvimInfo = { inherit description; - url = args.url or opt.package.default.meta.homepage; + url = args.url or opts.package.default.meta.homepage; path = [ namespace name @@ -115,7 +115,11 @@ (lib.optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = lib.mkDefault colorscheme; }) - (extraConfig cfg) + (lib.optionalAttrs (args ? extraConfig) ( + lib.nixvim.modules.applyExtraConfig { + inherit extraConfig cfg opts; + } + )) ] ); }; diff --git a/plugins/by-name/telescope/extensions/_mk-extension.nix b/plugins/by-name/telescope/extensions/_mk-extension.nix index 834f8454..019d8446 100644 --- a/plugins/by-name/telescope/extensions/_mk-extension.nix +++ b/plugins/by-name/telescope/extensions/_mk-extension.nix @@ -74,7 +74,18 @@ let }; }; - extraConfigModule = { config, ... }: extraConfig (getPluginAttr config); + extraConfigModule = + { + lib, + config, + options, + ... + }: + lib.nixvim.modules.applyExtraConfig { + inherit extraConfig; + cfg = getPluginAttr config; + opts = getPluginAttr options; + }; in { imports = imports ++ [ diff --git a/plugins/lsp/language-servers/_mk-lsp.nix b/plugins/lsp/language-servers/_mk-lsp.nix index 389c2758..59595be3 100644 --- a/plugins/lsp/language-servers/_mk-lsp.nix +++ b/plugins/lsp/language-servers/_mk-lsp.nix @@ -29,14 +29,14 @@ with lib; let cfg = config.plugins.lsp.servers.${name}; - opt = options.plugins.lsp.servers.${name}; + opts = options.plugins.lsp.servers.${name}; enabled = config.plugins.lsp.enable && cfg.enable; in { meta.nixvimInfo = { # TODO: description - url = args.url or opt.package.default.meta.homepage or null; + url = args.url or opts.package.default.meta.homepage or null; path = [ "plugins" "lsp" @@ -70,7 +70,7 @@ in type = with types; nullOr (listOf str); default = # TODO: do we really only want the default `cmd` when `package` is non-null? - if !(opt.package.isDefined or false) then + if !(opts.package.isDefined or false) then null else if cfg.package == null then null @@ -150,7 +150,12 @@ in end '' ); - settings = settings cfg.settings; + settings = lib.nixvim.modules.applyExtraConfig { + extraConfig = settings; + cfg = cfg.settings; + opts = opts.settings; + enabled = true; + }; } // cfg.extraOptions; } ]; @@ -170,8 +175,17 @@ in (mkRemovedOptionModule ( basePluginPath ++ [ "extraSettings" ] ) "You can use `${basePluginPathString}.extraOptions.settings` instead.") - (lib.mkIf enabled (extraConfig cfg)) ] + ++ lib.optional (args ? extraConfig) ( + lib.nixvim.modules.applyExtraConfig { + inherit + extraConfig + cfg + opts + enabled + ; + } + ) # Add an alias (with warning) for the lspconfig server name, if different to `name`. # Note: users may use lspconfig's docs to guess the `plugins.lsp.servers.*` name ++ (optional (name != serverName) (