Skip to content

Commit 196762d

Browse files
committed
modules/performance: add ability to byte compile lua plugins
This commit adds `performance.byteCompileLua.plugins` toggle that, if enabled, byte compiles all lua files in plugins
1 parent 9dfc40e commit 196762d

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
@@ -14,6 +14,9 @@ in
1414
description = "Whether to byte compile lua configuration files.";
1515
default = true;
1616
};
17+
plugins = lib.mkEnableOption "plugins" // {
18+
description = "Whether to byte compile lua plugins.";
19+
};
1720
};
1821

1922
combinePlugins = {

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
@@ -178,3 +178,71 @@ in
178178
'';
179179
};
180180
}
181+
//
182+
# Two equal tests, one with combinePlugins.enable = true
183+
pkgs.lib.genAttrs
184+
[
185+
"plugins"
186+
"plugins-combined"
187+
]
188+
(name: {
189+
performance = {
190+
byteCompileLua = {
191+
enable = true;
192+
plugins = true;
193+
};
194+
195+
combinePlugins.enable = pkgs.lib.hasSuffix "combined" name;
196+
};
197+
198+
extraPlugins = with pkgs.vimPlugins; [
199+
nvim-lspconfig
200+
# Depends on plenary-nvim
201+
telescope-nvim
202+
# buildCommand plugin with python3 dependency
203+
((pkgs.writeTextDir "/plugin/test.lua" "vim.opt.tabstop = 2").overrideAttrs {
204+
passthru.python3Dependencies = ps: [ ps.pyyaml ];
205+
})
206+
# Plugin with invalid lua file tests/indent/lua/cond.lua (should be ignored)
207+
nvim-treesitter
208+
];
209+
210+
extraConfigLuaPost = ''
211+
${isByteCompiledFun}
212+
213+
-- Plugins are loadable
214+
require("lspconfig")
215+
require("telescope")
216+
require("plenary")
217+
require("nvim-treesitter")
218+
219+
-- Python modules are importable
220+
vim.cmd.py3("import yaml")
221+
222+
-- nvim-lspconfig
223+
test_rtp_file("lua/lspconfig.lua", true)
224+
test_rtp_file("lua/lspconfig/server_configurations/nixd.lua", true)
225+
test_rtp_file("plugin/lspconfig.lua", true)
226+
test_rtp_file("doc/lspconfig.txt", false)
227+
228+
-- telescope-nvim
229+
test_rtp_file("lua/telescope/init.lua", true)
230+
test_rtp_file("lua/telescope/builtin/init.lua", true)
231+
test_rtp_file("plugin/telescope.lua", true)
232+
test_rtp_file("autoload/health/telescope.vim", false)
233+
test_rtp_file("doc/telescope.txt", false)
234+
235+
-- Dependency of telescope-nvim (plenary-nvim)
236+
test_rtp_file("lua/plenary/init.lua", true)
237+
test_rtp_file("plugin/plenary.vim", false)
238+
239+
-- Test plugin
240+
test_rtp_file("plugin/test.lua", true)
241+
242+
-- nvim-treesitter
243+
test_rtp_file("lua/nvim-treesitter/health.lua", true)
244+
test_rtp_file("lua/nvim-treesitter/install.lua", true)
245+
test_rtp_file("plugin/nvim-treesitter.lua", true)
246+
test_rtp_file("queries/nix/highlights.scm", false)
247+
'';
248+
})

0 commit comments

Comments
 (0)