Skip to content

Commit efb24d7

Browse files
committed
modules: refactor plugins code in top-level
1 parent d81f372 commit efb24d7

File tree

7 files changed

+221
-122
lines changed

7 files changed

+221
-122
lines changed

modules/top-level/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
./files
1010
./nixpkgs.nix
1111
./output.nix
12+
./plugins
1213
./readonly-renames.nix
1314
./test.nix
1415
];

modules/top-level/output.nix

Lines changed: 2 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -171,126 +171,6 @@ in
171171

172172
config =
173173
let
174-
# Plugin normalization
175-
normalize =
176-
p:
177-
let
178-
defaultPlugin = {
179-
plugin = null;
180-
config = null;
181-
optional = false;
182-
};
183-
in
184-
defaultPlugin // (if p ? plugin then p else { plugin = p; });
185-
normalizePluginList = plugins: map normalize plugins;
186-
187-
# Byte compiling of normalized plugin list
188-
byteCompilePlugins =
189-
plugins:
190-
let
191-
byteCompile =
192-
p:
193-
(builders.byteCompileLuaDrv p).overrideAttrs (
194-
prev: lib.optionalAttrs (prev ? dependencies) { dependencies = map byteCompile prev.dependencies; }
195-
);
196-
in
197-
map (p: p // { plugin = byteCompile p.plugin; }) plugins;
198-
199-
# Normalized and optionally byte compiled plugin list
200-
normalizedPlugins =
201-
let
202-
normalized = normalizePluginList config.extraPlugins;
203-
in
204-
if config.performance.byteCompileLua.enable && config.performance.byteCompileLua.plugins then
205-
byteCompilePlugins normalized
206-
else
207-
normalized;
208-
209-
# Plugin list extended with dependencies
210-
allPlugins =
211-
let
212-
pluginWithItsDeps =
213-
p: [ p ] ++ builtins.concatMap pluginWithItsDeps (normalizePluginList p.plugin.dependencies or [ ]);
214-
in
215-
lib.unique (builtins.concatMap pluginWithItsDeps normalizedPlugins);
216-
217-
# Remove dependencies from all plugins in a list
218-
removeDependencies = ps: map (p: p // { plugin = removeAttrs p.plugin [ "dependencies" ]; }) ps;
219-
220-
# Separated start and opt plugins
221-
partitionedOptStartPlugins = builtins.partition (p: p.optional) allPlugins;
222-
startPlugins = partitionedOptStartPlugins.wrong;
223-
# Remove opt plugin dependencies since they are already available in start plugins
224-
optPlugins = removeDependencies partitionedOptStartPlugins.right;
225-
226-
# Test if plugin shouldn't be included in plugin pack
227-
isStandalone =
228-
p:
229-
builtins.elem p.plugin config.performance.combinePlugins.standalonePlugins
230-
|| builtins.elem (lib.getName p.plugin) config.performance.combinePlugins.standalonePlugins;
231-
232-
# Separated standalone and combined start plugins
233-
partitionedStandaloneStartPlugins = builtins.partition isStandalone startPlugins;
234-
toCombinePlugins = partitionedStandaloneStartPlugins.wrong;
235-
# Remove standalone plugin dependencies since they are already available in start plugins
236-
standaloneStartPlugins = removeDependencies partitionedStandaloneStartPlugins.right;
237-
238-
# Combine start plugins into a single pack
239-
pluginPack =
240-
let
241-
# Every plugin has its own generated help tags (doc/tags)
242-
# Remove them to avoid collisions, new help tags
243-
# will be generate for the entire pack later on
244-
overriddenPlugins = map (
245-
plugin:
246-
plugin.plugin.overrideAttrs (prev: {
247-
nativeBuildInputs = lib.remove pkgs.vimUtils.vimGenDocHook prev.nativeBuildInputs or [ ];
248-
configurePhase = ''
249-
${prev.configurePhase or ""}
250-
rm -vf doc/tags'';
251-
})
252-
) toCombinePlugins;
253-
254-
# Python3 dependencies
255-
python3Dependencies =
256-
let
257-
deps = map (p: p.plugin.python3Dependencies or (_: [ ])) toCombinePlugins;
258-
in
259-
ps: builtins.concatMap (f: f ps) deps;
260-
261-
# Combined plugin
262-
combinedPlugin = pkgs.vimUtils.toVimPlugin (
263-
pkgs.buildEnv {
264-
name = "plugin-pack";
265-
paths = overriddenPlugins;
266-
inherit (config.performance.combinePlugins) pathsToLink;
267-
# Remove empty directories and activate vimGenDocHook
268-
postBuild = ''
269-
find $out -type d -empty -delete
270-
runHook preFixup
271-
'';
272-
passthru = {
273-
inherit python3Dependencies;
274-
};
275-
}
276-
);
277-
278-
# Combined plugin configs
279-
combinedConfig = builtins.concatStringsSep "\n" (
280-
builtins.concatMap (x: lib.optional (x.config != null && x.config != "") x.config) toCombinePlugins
281-
);
282-
in
283-
normalize {
284-
plugin = combinedPlugin;
285-
config = combinedConfig;
286-
};
287-
288-
# Combined plugins
289-
combinedPlugins = [ pluginPack ] ++ standaloneStartPlugins ++ optPlugins;
290-
291-
# Plugins to use in build.package
292-
plugins = if config.performance.combinePlugins.enable then combinedPlugins else normalizedPlugins;
293-
294174
neovimConfig = pkgs.neovimUtils.makeNeovimConfig (
295175
{
296176
inherit (config)
@@ -304,15 +184,15 @@ in
304184
withPython3
305185
;
306186
# inherit customRC;
307-
inherit plugins;
187+
inherit (config.build) plugins;
308188
}
309189
# Necessary to make sure the runtime path is set properly in NixOS 22.05,
310190
# or more generally before the commit:
311191
# cda1f8ae468 - neovim: pass packpath via the wrapper
312192
// optionalAttrs (lib.functionArgs pkgs.neovimUtils.makeNeovimConfig ? configure) {
313193
configure.packages = {
314194
nixvim = {
315-
start = map (x: x.plugin) plugins;
195+
start = map (x: x.plugin) config.build.plugins;
316196
opt = [ ];
317197
};
318198
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Byte compiling of normalized plugin list
3+
4+
Inputs: List of normalized plugins
5+
Outputs: List of normalized (compiled) plugins
6+
*/
7+
{ lib, pkgs }:
8+
let
9+
builders = lib.nixvim.builders.withPkgs pkgs;
10+
11+
byteCompile =
12+
p:
13+
(builders.byteCompileLuaDrv p).overrideAttrs (
14+
prev: lib.optionalAttrs (prev ? dependencies) { dependencies = map byteCompile prev.dependencies; }
15+
);
16+
in
17+
map (p: p // { plugin = byteCompile p.plugin; })
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
lib,
3+
pkgs,
4+
pathsToLink,
5+
standalonePlugins,
6+
}:
7+
let
8+
inherit (import ./utils.nix lib)
9+
getAndNormalizeDeps
10+
removeDeps
11+
;
12+
mkPluginPack = import ./mk-plugin-pack.nix { inherit lib pkgs; };
13+
14+
in
15+
/*
16+
*combinePlugins* function
17+
18+
Take a list of combined plugins, combine the relevant ones and return the resulting list of plugins
19+
*/
20+
normalizedPlugins:
21+
let
22+
# Plugin list extended with dependencies
23+
allPlugins =
24+
let
25+
pluginWithItsDeps = p: [ p ] ++ builtins.concatMap pluginWithItsDeps (getAndNormalizeDeps p);
26+
in
27+
lib.unique (builtins.concatMap pluginWithItsDeps normalizedPlugins);
28+
29+
# Separated start and opt plugins
30+
partitionedOptStartPlugins = builtins.partition (p: p.optional) allPlugins;
31+
startPlugins = partitionedOptStartPlugins.wrong;
32+
# Remove opt plugin dependencies since they are already available in start plugins
33+
optPlugins = removeDeps partitionedOptStartPlugins.right;
34+
35+
# Test if plugin shouldn't be included in plugin pack
36+
isStandalone =
37+
p:
38+
builtins.elem p.plugin standalonePlugins || builtins.elem (lib.getName p.plugin) standalonePlugins;
39+
40+
# Separated standalone and combined start plugins
41+
partitionedStandaloneStartPlugins = builtins.partition isStandalone startPlugins;
42+
pluginsToCombine = partitionedStandaloneStartPlugins.wrong;
43+
# Remove standalone plugin dependencies since they are already available in start plugins
44+
standaloneStartPlugins = removeDeps partitionedStandaloneStartPlugins.right;
45+
46+
# Combine start plugins into a single pack
47+
pluginPack = mkPluginPack { inherit pluginsToCombine pathsToLink; };
48+
in
49+
# Combined plugins
50+
[ pluginPack ] ++ standaloneStartPlugins ++ optPlugins

modules/top-level/plugins/default.nix

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
lib,
3+
config,
4+
pkgs,
5+
...
6+
}:
7+
let
8+
inherit (import ./utils.nix lib)
9+
normalizedPluginType
10+
normalizePlugins
11+
;
12+
byteCompileCfg = config.performance.byteCompileLua;
13+
in
14+
{
15+
options = {
16+
build.plugins = lib.mkOption {
17+
visible = false;
18+
internal = true;
19+
readOnly = true;
20+
type = lib.types.listOf normalizedPluginType;
21+
description = ''
22+
Final list of (normalized) plugins that will be passed to the wrapper.
23+
24+
It notably implements:
25+
- byte-compilation (performance.byteCompileLua) -> ./byte-compile-plugins.nix
26+
- plugins combining (performance.combinePlugins) -> ./combine-plugins.nix
27+
'';
28+
};
29+
};
30+
31+
config = {
32+
build.plugins =
33+
let
34+
shouldCompilePlugins = byteCompileCfg.enable && byteCompileCfg.plugins;
35+
byteCompilePlugins = import ./byte-compile-plugins.nix { inherit lib pkgs; };
36+
37+
shouldCombinePlugins = config.performance.combinePlugins.enable;
38+
combinePlugins = import ./combine-plugins.nix {
39+
inherit lib pkgs;
40+
inherit (config.performance.combinePlugins)
41+
standalonePlugins
42+
pathsToLink
43+
;
44+
};
45+
in
46+
47+
lib.pipe config.extraPlugins (
48+
[ normalizePlugins ]
49+
++ lib.optionals shouldCompilePlugins [ byteCompilePlugins ]
50+
++ lib.optionals shouldCombinePlugins [ combinePlugins ]
51+
);
52+
};
53+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{ lib, pkgs }:
2+
let
3+
inherit (import ./utils.nix lib) normalizePlugin;
4+
in
5+
{ pluginsToCombine, pathsToLink }:
6+
let
7+
8+
overridePlugin =
9+
plugin:
10+
plugin.plugin.overrideAttrs (prev: {
11+
nativeBuildInputs = lib.remove pkgs.vimUtils.vimGenDocHook prev.nativeBuildInputs or [ ];
12+
configurePhase = ''
13+
${prev.configurePhase or ""}
14+
rm -vf doc/tags'';
15+
});
16+
17+
# Every plugin has its own generated help tags (doc/tags)
18+
# Remove them to avoid collisions, new help tags
19+
# will be generate for the entire pack later on
20+
overriddenPlugins = map overridePlugin pluginsToCombine;
21+
22+
# Gather python 3 dependencies from every plugins
23+
python3Dependencies =
24+
ps:
25+
lib.pipe pluginsToCombine [
26+
(builtins.catAttrs "plugin")
27+
(builtins.catAttrs "python3Dependencies")
28+
(builtins.concatMap (f: f ps))
29+
];
30+
31+
# Combined plugin
32+
combinedPlugin = pkgs.vimUtils.toVimPlugin (
33+
pkgs.buildEnv {
34+
name = "plugin-pack";
35+
paths = overriddenPlugins;
36+
inherit pathsToLink;
37+
38+
# Remove empty directories and activate vimGenDocHook
39+
# TODO: figure out why we are running the `preFixup` hook in `postBuild`
40+
postBuild = ''
41+
find $out -type d -empty -delete
42+
runHook preFixup
43+
'';
44+
passthru = {
45+
inherit python3Dependencies;
46+
};
47+
}
48+
);
49+
50+
# Combined plugin configs
51+
combinedConfig = lib.pipe pluginsToCombine [
52+
(builtins.catAttrs "config")
53+
(builtins.filter (config: config != null && config != ""))
54+
(builtins.concatStringsSep "\n")
55+
];
56+
in
57+
normalizePlugin {
58+
plugin = combinedPlugin;
59+
config = combinedConfig;
60+
}

modules/top-level/plugins/utils.nix

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
lib:
2+
lib.fix (self: {
3+
normalizedPluginType = lib.types.submodule {
4+
options = {
5+
plugin = lib.mkOption {
6+
type = lib.types.package;
7+
};
8+
9+
config = lib.mkOption {
10+
type = with lib.types; nullOr str;
11+
};
12+
13+
optional = lib.mkOption {
14+
type = lib.types.bool;
15+
};
16+
};
17+
};
18+
19+
# Normalize a plugin in a standard { plugin, config, optional } attrs
20+
normalizePlugin =
21+
p:
22+
let
23+
defaultPlugin = {
24+
plugin = null;
25+
config = null;
26+
optional = false;
27+
};
28+
in
29+
defaultPlugin // (if p ? plugin then p else { plugin = p; });
30+
31+
# Normalize a list of plugins
32+
normalizePlugins = builtins.map self.normalizePlugin;
33+
34+
getAndNormalizeDeps = p: self.normalizePlugins (p.plugin.dependencies or [ ]);
35+
36+
# Remove dependencies from all plugins in a list
37+
removeDeps = map (p: p // { plugin = removeAttrs p.plugin [ "dependencies" ]; });
38+
})

0 commit comments

Comments
 (0)