Skip to content

Commit

Permalink
lib/modules: add applyExtraConfig
Browse files Browse the repository at this point in the history
Used for
- mkVimPlugin: extraConfig
- mkNeovimPlugin: extraConfig
- mkLsp: extraConfig
- mkLsp: settings
- mkExtension (telecsope): extraConfig
  • Loading branch information
MattSturgeon committed Nov 20, 2024
1 parent c7b7b64 commit 63cfc84
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 14 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | `[]` |
Expand Down Expand Up @@ -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 | `[]` |
Expand Down
22 changes: 22 additions & 0 deletions lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions lib/neovim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
}:
let
cfg = config.${namespace}.${name};
opt = options.${namespace}.${name};
opts = options.${namespace}.${name};

setupCode = ''
require('${luaName}')${setup}(${
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down
10 changes: 7 additions & 3 deletions lib/vim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
))
]
);
};
Expand Down
13 changes: 12 additions & 1 deletion plugins/by-name/telescope/extensions/_mk-extension.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 ++ [
Expand Down
24 changes: 19 additions & 5 deletions plugins/lsp/language-servers/_mk-lsp.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
];
Expand All @@ -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) (
Expand Down

0 comments on commit 63cfc84

Please sign in to comment.