lib/modules: add applyExtraConfig

Used for
- mkVimPlugin: extraConfig
- mkNeovimPlugin: extraConfig
- mkLsp: extraConfig
- mkLsp: settings
- mkExtension (telecsope): extraConfig
This commit is contained in:
Matt Sturgeon 2024-11-19 13:55:33 +00:00
parent c7b7b6481b
commit 63cfc84abe
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
6 changed files with 69 additions and 14 deletions

View file

@ -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"` | | **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` | | **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` | | **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 | `{}` | | **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 | `[]` | | **extraPackages** | Extra packages to include. | No | `[]` |
| **extraPlugins** | Extra plugins 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 | | **colorscheme** | The name of the colorscheme. | No | `name` parameter |
| **deprecateExtraConfig** | Flag to deprecate extra configuration. | No | `false` | | **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` | | **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 | `{}` | | **extraOptions** | Extra options for the plugin. | No | `{}` |
| **extraPackages** | Extra packages to include. | No | `[]` | | **extraPackages** | Extra packages to include. | No | `[]` |
| **extraPlugins** | Extra plugins to include. | No | `[]` | | **extraPlugins** | Extra plugins to include. | No | `[]` |

View file

@ -36,6 +36,28 @@ in
helpers = self; helpers = self;
} // extraSpecialArgs; } // 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 ( // lib.mapAttrs (
name: msg: name: msg:

View file

@ -63,7 +63,7 @@
}: }:
let let
cfg = config.${namespace}.${name}; cfg = config.${namespace}.${name};
opt = options.${namespace}.${name}; opts = options.${namespace}.${name};
setupCode = '' setupCode = ''
require('${luaName}')${setup}(${ require('${luaName}')${setup}(${
@ -78,7 +78,7 @@
inherit maintainers; inherit maintainers;
nixvimInfo = { nixvimInfo = {
inherit description; inherit description;
url = args.url or opt.package.default.meta.homepage; url = args.url or opts.package.default.meta.homepage;
path = [ path = [
namespace namespace
name name
@ -146,7 +146,11 @@
(lib.optionalAttrs (isColorscheme && (colorscheme != null)) { (lib.optionalAttrs (isColorscheme && (colorscheme != null)) {
colorscheme = lib.mkDefault colorscheme; colorscheme = lib.mkDefault colorscheme;
}) })
(extraConfig cfg) (lib.optionalAttrs (args ? extraConfig) (
lib.nixvim.modules.applyExtraConfig {
inherit extraConfig cfg opts;
}
))
] ]
++ (lib.optionals (!hasConfigAttrs) [ ++ (lib.optionals (!hasConfigAttrs) [
(lib.optionalAttrs callSetup (setLuaConfig setupCode)) (lib.optionalAttrs callSetup (setLuaConfig setupCode))

View file

@ -60,14 +60,14 @@
}: }:
let let
cfg = config.${namespace}.${name}; cfg = config.${namespace}.${name};
opt = options.${namespace}.${name}; opts = options.${namespace}.${name};
in in
{ {
meta = { meta = {
inherit maintainers; inherit maintainers;
nixvimInfo = { nixvimInfo = {
inherit description; inherit description;
url = args.url or opt.package.default.meta.homepage; url = args.url or opts.package.default.meta.homepage;
path = [ path = [
namespace namespace
name name
@ -115,7 +115,11 @@
(lib.optionalAttrs (isColorscheme && (colorscheme != null)) { (lib.optionalAttrs (isColorscheme && (colorscheme != null)) {
colorscheme = lib.mkDefault colorscheme; colorscheme = lib.mkDefault colorscheme;
}) })
(extraConfig cfg) (lib.optionalAttrs (args ? extraConfig) (
lib.nixvim.modules.applyExtraConfig {
inherit extraConfig cfg opts;
}
))
] ]
); );
}; };

View file

@ -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 in
{ {
imports = imports ++ [ imports = imports ++ [

View file

@ -29,14 +29,14 @@
with lib; with lib;
let let
cfg = config.plugins.lsp.servers.${name}; 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; enabled = config.plugins.lsp.enable && cfg.enable;
in in
{ {
meta.nixvimInfo = { meta.nixvimInfo = {
# TODO: description # 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 = [ path = [
"plugins" "plugins"
"lsp" "lsp"
@ -70,7 +70,7 @@ in
type = with types; nullOr (listOf str); type = with types; nullOr (listOf str);
default = default =
# TODO: do we really only want the default `cmd` when `package` is non-null? # 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 null
else if cfg.package == null then else if cfg.package == null then
null null
@ -150,7 +150,12 @@ in
end end
'' ''
); );
settings = settings cfg.settings; settings = lib.nixvim.modules.applyExtraConfig {
extraConfig = settings;
cfg = cfg.settings;
opts = opts.settings;
enabled = true;
};
} // cfg.extraOptions; } // cfg.extraOptions;
} }
]; ];
@ -170,8 +175,17 @@ in
(mkRemovedOptionModule ( (mkRemovedOptionModule (
basePluginPath ++ [ "extraSettings" ] basePluginPath ++ [ "extraSettings" ]
) "You can use `${basePluginPathString}.extraOptions.settings` instead.") ) "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`. # 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 # Note: users may use lspconfig's docs to guess the `plugins.lsp.servers.*` name
++ (optional (name != serverName) ( ++ (optional (name != serverName) (