Skip to content

Commit 90a5725

Browse files
committed
modules/performance: add support for byte compiling lua plugins
1 parent ed13ee1 commit 90a5725

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed

modules/performance.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ in
4141
description = "Whether to byte compile lua configuration files.";
4242
default = true;
4343
};
44+
plugins = lib.mkEnableOption "plugins" // {
45+
description = "Whether to byte compile lua plugins.";
46+
};
4447
};
4548
};
4649

modules/top-level/output.nix

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,37 @@ in
8787
defaultPlugin // (if p ? plugin then p else { plugin = p; });
8888
normalizePluginList = plugins: map normalize plugins;
8989

90-
# Normalized plugin list
91-
normalizedPlugins = normalizePluginList config.extraPlugins;
90+
# Byte compiling of normalized plugin list
91+
byteCompilePlugins =
92+
plugins:
93+
let
94+
byteCompile =
95+
p:
96+
p.overrideAttrs (
97+
prev:
98+
{
99+
nativeBuildInputs = prev.nativeBuildInputs or [ ] ++ [ helpers.byteCompileLuaHook ];
100+
}
101+
// lib.optionalAttrs (prev ? buildCommand) {
102+
buildCommand = ''
103+
${prev.buildCommand}
104+
runHook postFixup
105+
'';
106+
}
107+
// lib.optionalAttrs (prev ? dependencies) { dependencies = map byteCompile prev.dependencies; }
108+
);
109+
in
110+
map (p: p // { plugin = byteCompile p.plugin; }) plugins;
111+
112+
# Normalized and optionally byte compiled plugin list
113+
normalizedPlugins =
114+
let
115+
normalized = normalizePluginList config.extraPlugins;
116+
in
117+
if config.performance.byteCompileLua.enable && config.performance.byteCompileLua.plugins then
118+
byteCompilePlugins normalized
119+
else
120+
normalized;
92121

93122
# Plugin list extended with dependencies
94123
allPlugins =

tests/test-sources/modules/performance/byte-compile-lua.nix

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,71 @@ in
141141
'';
142142
};
143143
}
144+
//
145+
# Two equal tests, one with combinePlugins.enable = true
146+
pkgs.lib.genAttrs
147+
[
148+
"plugins"
149+
"plugins-combined"
150+
]
151+
(name: {
152+
performance = {
153+
byteCompileLua = {
154+
enable = true;
155+
plugins = true;
156+
};
157+
158+
combinePlugins.enable = pkgs.lib.hasSuffix "combined" name;
159+
};
160+
161+
extraPlugins = with pkgs.vimPlugins; [
162+
nvim-lspconfig
163+
# Depends on plenary-nvim
164+
telescope-nvim
165+
# buildCommand plugin with python3 dependency
166+
((pkgs.writeTextDir "/plugin/test.lua" "vim.opt.tabstop = 2").overrideAttrs {
167+
passthru.python3Dependencies = ps: [ ps.pyyaml ];
168+
})
169+
# Plugin with invalid lua file tests/indent/lua/cond.lua (should be ignored)
170+
nvim-treesitter
171+
];
172+
173+
extraConfigLuaPost = ''
174+
${isByteCompiledFun}
175+
176+
-- Plugins are loadable
177+
require("lspconfig")
178+
require("telescope")
179+
require("plenary")
180+
require("nvim-treesitter")
181+
182+
-- Python modules are importable
183+
vim.cmd.py3("import yaml")
184+
185+
-- nvim-lspconfig
186+
test_rtp_file("lua/lspconfig.lua", true)
187+
test_rtp_file("lua/lspconfig/server_configurations/nixd.lua", true)
188+
test_rtp_file("plugin/lspconfig.lua", true)
189+
test_rtp_file("doc/lspconfig.txt", false)
190+
191+
-- telescope-nvim
192+
test_rtp_file("lua/telescope/init.lua", true)
193+
test_rtp_file("lua/telescope/builtin/init.lua", true)
194+
test_rtp_file("plugin/telescope.lua", true)
195+
test_rtp_file("autoload/health/telescope.vim", false)
196+
test_rtp_file("doc/telescope.txt", false)
197+
198+
-- Dependency of telescope-nvim (plenary-nvim)
199+
test_rtp_file("lua/plenary/init.lua", true)
200+
test_rtp_file("plugin/plenary.vim", false)
201+
202+
-- Test plugin
203+
test_rtp_file("plugin/test.lua", true)
204+
205+
-- nvim-treesitter
206+
test_rtp_file("lua/nvim-treesitter/health.lua", true)
207+
test_rtp_file("lua/nvim-treesitter/install.lua", true)
208+
test_rtp_file("plugin/nvim-treesitter.lua", true)
209+
test_rtp_file("queries/nix/highlights.scm", false)
210+
'';
211+
})

0 commit comments

Comments
 (0)