Skip to content

Commit d9ba37a

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 65b71cb commit d9ba37a

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

modules/performance.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ in
1818
default = true;
1919
example = false;
2020
};
21+
plugins = lib.mkEnableOption "plugins" // {
22+
description = "Whether to byte compile lua plugins.";
23+
};
2124
};
2225

2326
combinePlugins = {

modules/top-level/output.nix

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,27 @@ 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+
(helpers.byteCompileLuaDrv p).overrideAttrs (
97+
prev: lib.optionalAttrs (prev ? dependencies) { dependencies = map byteCompile prev.dependencies; }
98+
);
99+
in
100+
map (p: p // { plugin = byteCompile p.plugin; }) plugins;
101+
102+
# Normalized and optionally byte compiled plugin list
103+
normalizedPlugins =
104+
let
105+
normalized = normalizePluginList config.extraPlugins;
106+
in
107+
if config.performance.byteCompileLua.enable && config.performance.byteCompileLua.plugins then
108+
byteCompilePlugins normalized
109+
else
110+
normalized;
92111

93112
# Plugin list extended with dependencies
94113
allPlugins =

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

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

0 commit comments

Comments
 (0)