Skip to content

Commit 2589416

Browse files
committed
plugins/lazy.nvim: switch to mkNeovimPlugin
1 parent 5241c9f commit 2589416

File tree

2 files changed

+187
-172
lines changed

2 files changed

+187
-172
lines changed

plugins/pluginmanagers/lazy.nix

Lines changed: 179 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -36,188 +36,195 @@ let
3636
processedPlugins = builtins.concatLists (builtins.map processPlugin lazyPlugins);
3737
lazyPath = pkgs.linkFarm "lazy-plugins" processedPlugins;
3838
in
39-
{
40-
options = {
41-
plugins.lazy = {
42-
enable = mkEnableOption "lazy.nvim";
43-
44-
gitPackage = helpers.mkPackageOption {
45-
name = "git";
46-
default = pkgs.git;
47-
};
39+
nixvim.neovim-plugin.mkNeovimPlugin config {
40+
name = "lazy";
41+
maintainers = [ maintainers.MattSturgeon ];
42+
defaultPackage = pkgs.vimPlugins.lazy-nvim;
4843

49-
plugins =
44+
extraOptions =
45+
let
46+
lazyPluginType =
5047
with types;
51-
let
52-
pluginType = either package (submodule {
53-
options = {
54-
dir = helpers.mkNullOrOption str "A directory pointing to a local plugin";
55-
56-
pkg = mkOption {
57-
type = package;
58-
description = "Vim plugin to install";
59-
};
60-
61-
name = helpers.mkNullOrOption str "Name of the plugin to install";
62-
63-
dev = helpers.defaultNullOpts.mkBool false ''
64-
When true, a local plugin directory will be used instead.
65-
See config.dev
66-
'';
67-
68-
lazy = helpers.defaultNullOpts.mkBool true ''
69-
When true, the plugin will only be loaded when needed.
70-
Lazy-loaded plugins are automatically loaded when their Lua modules are required,
71-
or when one of the lazy-loading handlers triggers
72-
'';
73-
74-
enabled = helpers.defaultNullOpts.mkStrLuaFnOr types.bool "`true`" ''
75-
When false then this plugin will not be included in the spec. (accepts fun():boolean)
76-
'';
77-
78-
cond = helpers.defaultNullOpts.mkStrLuaFnOr types.bool "`true`" ''
79-
When false, or if the function returns false,
80-
then this plugin will not be loaded. Useful to disable some plugins in vscode,
81-
or firenvim for example. (accepts fun(LazyPlugin):boolean)
82-
'';
83-
84-
dependencies = helpers.mkNullOrOption (helpers.nixvimTypes.eitherRecursive str listOfPlugins) "Plugin dependencies";
85-
86-
init = helpers.mkNullOrLuaFn "init functions are always executed during startup";
87-
88-
config = helpers.mkNullOrStrLuaFnOr (types.enum [ true ]) ''
89-
config is executed when the plugin loads.
90-
The default implementation will automatically run require(MAIN).setup(opts).
91-
Lazy uses several heuristics to determine the plugin's MAIN module automatically based on the plugin's name.
92-
See also opts. To use the default implementation without opts set config to true.
93-
'';
94-
95-
main = helpers.mkNullOrOption str ''
96-
You can specify the main module to use for config() and opts(),
97-
in case it can not be determined automatically. See config()
98-
'';
48+
either package (submodule {
49+
freeformType = types.attrsOf types.anything;
50+
options = {
51+
dir = helpers.mkNullOrOption str "A directory pointing to a local plugin";
52+
53+
pkg = mkOption {
54+
type = package;
55+
description = "Vim plugin to install";
56+
};
9957

100-
submodules = helpers.defaultNullOpts.mkBool true ''
101-
When false, git submodules will not be fetched.
102-
Defaults to true
58+
name = helpers.mkNullOrOption str "Name of the plugin to install";
59+
60+
dev = helpers.defaultNullOpts.mkBool false ''
61+
When true, a local plugin directory will be used instead.
62+
See config.dev
63+
'';
64+
65+
lazy = helpers.defaultNullOpts.mkBool true ''
66+
When true, the plugin will only be loaded when needed.
67+
Lazy-loaded plugins are automatically loaded when their Lua modules are required,
68+
or when one of the lazy-loading handlers triggers
69+
'';
70+
71+
enabled = helpers.defaultNullOpts.mkStrLuaFnOr types.bool "`true`" ''
72+
When false then this plugin will not be included in the spec. (accepts fun():boolean)
73+
'';
74+
75+
cond = helpers.defaultNullOpts.mkStrLuaFnOr types.bool "`true`" ''
76+
When false, or if the function returns false,
77+
then this plugin will not be loaded. Useful to disable some plugins in vscode,
78+
or firenvim for example. (accepts fun(LazyPlugin):boolean)
79+
'';
80+
81+
dependencies = helpers.mkNullOrOption (helpers.nixvimTypes.eitherRecursive str listOfPlugins) "Plugin dependencies";
82+
83+
init = helpers.mkNullOrLuaFn "init functions are always executed during startup";
84+
85+
config = helpers.mkNullOrStrLuaFnOr (types.enum [ true ]) ''
86+
config is executed when the plugin loads.
87+
The default implementation will automatically run require(MAIN).setup(opts).
88+
Lazy uses several heuristics to determine the plugin's MAIN module automatically based on the plugin's name.
89+
See also opts. To use the default implementation without opts set config to true.
90+
'';
91+
92+
main = helpers.mkNullOrOption str ''
93+
You can specify the main module to use for config() and opts(),
94+
in case it can not be determined automatically. See config()
95+
'';
96+
97+
submodules = helpers.defaultNullOpts.mkBool true ''
98+
When false, git submodules will not be fetched.
99+
Defaults to true
100+
'';
101+
102+
event =
103+
with helpers.nixvimTypes;
104+
helpers.mkNullOrOption (maybeRaw (either str (listOf str))) "Lazy-load on event. Events can be specified as BufEnter or with a pattern like BufEnter *.lua";
105+
106+
cmd =
107+
with helpers.nixvimTypes;
108+
helpers.mkNullOrOption (maybeRaw (either str (listOf str))) "Lazy-load on command";
109+
110+
ft =
111+
with helpers.nixvimTypes;
112+
helpers.mkNullOrOption (maybeRaw (either str (listOf str))) "Lazy-load on filetype";
113+
114+
keys =
115+
with helpers.nixvimTypes;
116+
helpers.mkNullOrOption (maybeRaw (either str (listOf str))) "Lazy-load on key mapping";
117+
118+
module = helpers.mkNullOrOption (enum [ false ]) ''
119+
Do not automatically load this Lua module when it's required somewhere
120+
'';
121+
122+
priority = helpers.mkNullOrOption number ''
123+
Only useful for start plugins (lazy=false) to force loading certain plugins first.
124+
Default priority is 50. It's recommended to set this to a high number for colorschemes.
125+
'';
126+
127+
optional = helpers.defaultNullOpts.mkBool false ''
128+
When a spec is tagged optional, it will only be included in the final spec,
129+
when the same plugin has been specified at least once somewhere else without optional.
130+
This is mainly useful for Neovim distros, to allow setting options on plugins that may/may not be part
131+
of the user's plugins
132+
'';
133+
134+
opts =
135+
with helpers.nixvimTypes;
136+
helpers.mkNullOrOption (maybeRaw (attrsOf anything)) ''
137+
opts should be a table (will be merged with parent specs),
138+
return a table (replaces parent specs) or should change a table.
139+
The table will be passed to the Plugin.config() function.
140+
Setting this value will imply Plugin.config()
103141
'';
142+
};
143+
});
104144

105-
event =
106-
with helpers.nixvimTypes;
107-
helpers.mkNullOrOption (maybeRaw (either str (listOf str))) "Lazy-load on event. Events can be specified as BufEnter or with a pattern like BufEnter *.lua";
108-
109-
cmd =
110-
with helpers.nixvimTypes;
111-
helpers.mkNullOrOption (maybeRaw (either str (listOf str))) "Lazy-load on command";
112-
113-
ft =
114-
with helpers.nixvimTypes;
115-
helpers.mkNullOrOption (maybeRaw (either str (listOf str))) "Lazy-load on filetype";
145+
listOfPlugins = types.listOf lazyPluginType;
146+
in
147+
{
148+
plugins = mkOption {
149+
type = listOfPlugins;
150+
default = [ ];
151+
description = "List of plugins";
152+
};
153+
};
116154

117-
keys =
118-
with helpers.nixvimTypes;
119-
helpers.mkNullOrOption (maybeRaw (either str (listOf str))) "Lazy-load on key mapping";
155+
extraConfig =
156+
cfg:
157+
let
158+
setupOptions =
159+
cfg.settings
160+
// optionalAttrs (cfg.settings.dev == null) {
161+
dev = {
162+
path = "${lazyPath}";
163+
pattern = [ "." ];
164+
fallback = false;
165+
};
166+
};
120167

121-
module = helpers.mkNullOrOption (enum [ false ]) ''
122-
Do not automatically load this Lua module when it's required somewhere
123-
'';
168+
pluginToLua =
169+
plugin:
170+
let
171+
keyExists = keyToCheck: attrSet: lib.elem keyToCheck (lib.attrNames attrSet);
172+
converted =
173+
if isDerivation plugin then
174+
{ dir = "${lazyPath}/${lib.getName plugin}"; }
175+
else
176+
let
177+
handledPluginOptions = {
178+
"__unkeyed" = plugin.name;
179+
180+
inherit (plugin)
181+
cmd
182+
cond
183+
config
184+
dev
185+
enabled
186+
event
187+
ft
188+
init
189+
keys
190+
lazy
191+
main
192+
module
193+
name
194+
optional
195+
opts
196+
priority
197+
submodules
198+
;
199+
200+
dependencies = helpers.ifNonNull' plugin.dependencies (
201+
if isList plugin.dependencies then (pluginListToLua plugin.dependencies) else plugin.dependencies
202+
);
203+
204+
dir =
205+
if plugin ? dir && plugin.dir != null then plugin.dir else "${lazyPath}/${lib.getName plugin.pkg}";
206+
};
207+
freeformPluginOptions = lib.removeAttrs plugin ((lib.attrNames handledPluginOptions) ++ [ "pkg" ]);
208+
combinedPluginOptions = freeformPluginOptions // handledPluginOptions;
209+
in
210+
combinedPluginOptions;
211+
in
212+
converted;
124213

125-
priority = helpers.mkNullOrOption number ''
126-
Only useful for start plugins (lazy=false) to force loading certain plugins first.
127-
Default priority is 50. It's recommended to set this to a high number for colorschemes.
128-
'';
214+
pluginListToLua = map pluginToLua;
129215

130-
optional = helpers.defaultNullOpts.mkBool false ''
131-
When a spec is tagged optional, it will only be included in the final spec,
132-
when the same plugin has been specified at least once somewhere else without optional.
133-
This is mainly useful for Neovim distros, to allow setting options on plugins that may/may not be part
134-
of the user's plugins
135-
'';
216+
plugins = pluginListToLua cfg.plugins;
136217

137-
opts =
138-
with helpers.nixvimTypes;
139-
helpers.mkNullOrOption (maybeRaw (attrsOf anything)) ''
140-
opts should be a table (will be merged with parent specs),
141-
return a table (replaces parent specs) or should change a table.
142-
The table will be passed to the Plugin.config() function.
143-
Setting this value will imply Plugin.config()
144-
'';
145-
};
146-
});
147-
148-
listOfPlugins = types.listOf pluginType;
149-
in
150-
mkOption {
151-
type = listOfPlugins;
152-
default = [ ];
153-
description = "List of plugins";
218+
packedPlugins = if length plugins == 1 then head plugins else plugins;
219+
in
220+
{
221+
plugins.lazy.settings = {
222+
dev = {
223+
path = "${lazyPath}";
224+
patterns = [ "." ];
225+
fallback = false;
154226
};
227+
spec = packedPlugins;
228+
};
155229
};
156-
};
157-
158-
config = mkIf cfg.enable {
159-
extraPlugins = [ pkgs.vimPlugins.lazy-nvim ];
160-
161-
extraPackages = [ cfg.gitPackage ];
162-
163-
extraConfigLua =
164-
let
165-
pluginToLua =
166-
plugin:
167-
let
168-
keyExists = keyToCheck: attrSet: lib.elem keyToCheck (lib.attrNames attrSet);
169-
in
170-
if isDerivation plugin then
171-
{ dir = "${lazyPath}/${lib.getName plugin}"; }
172-
else
173-
{
174-
"__unkeyed" = plugin.name;
175-
176-
inherit (plugin)
177-
cmd
178-
cond
179-
config
180-
dev
181-
enabled
182-
event
183-
ft
184-
init
185-
keys
186-
lazy
187-
main
188-
module
189-
name
190-
optional
191-
opts
192-
priority
193-
submodules
194-
;
195-
196-
dependencies = helpers.ifNonNull' plugin.dependencies (
197-
if isList plugin.dependencies then (pluginListToLua plugin.dependencies) else plugin.dependencies
198-
);
199-
200-
dir =
201-
if plugin ? dir && plugin.dir != null then plugin.dir else "${lazyPath}/${lib.getName plugin.pkg}";
202-
};
203-
204-
pluginListToLua = map pluginToLua;
205-
206-
plugins = pluginListToLua cfg.plugins;
207-
208-
packedPlugins = if length plugins == 1 then head plugins else plugins;
209-
in
210-
mkIf (cfg.plugins != [ ]) ''
211-
require('lazy').setup(
212-
{
213-
dev = {
214-
path = "${lazyPath}",
215-
patterns = {"."},
216-
fallback = false
217-
},
218-
spec = ${helpers.toLuaObject packedPlugins}
219-
}
220-
)
221-
'';
222-
};
223230
}

tests/test-sources/plugins/pluginmanagers/lazy.nix

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
plugins = [
1313
vim-closer
1414

15+
# Test freeform
16+
{
17+
pkg = vim-dispatch;
18+
# The below is not actually a property in the `lazy.nvim` plugin spec
19+
# but is purely to test freeform capabilities of the `pluginType`.
20+
blah = "test";
21+
}
22+
1523
# Load on specific commands
1624
{
1725
pkg = vim-dispatch;

0 commit comments

Comments
 (0)