Skip to content

Commit da17a2e

Browse files
committed
modules/performance: add support for byte compiling lua plugins
1 parent 6ea8189 commit da17a2e

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
@@ -159,3 +159,71 @@ in
159159
'';
160160
};
161161
}
162+
//
163+
# Two equal tests, one with combinePlugins.enable = true
164+
pkgs.lib.genAttrs
165+
[
166+
"plugins"
167+
"plugins-combined"
168+
]
169+
(name: {
170+
performance = {
171+
byteCompileLua = {
172+
enable = true;
173+
plugins = true;
174+
};
175+
176+
combinePlugins.enable = pkgs.lib.hasSuffix "combined" name;
177+
};
178+
179+
extraPlugins = with pkgs.vimPlugins; [
180+
nvim-lspconfig
181+
# Depends on plenary-nvim
182+
telescope-nvim
183+
# buildCommand plugin with python3 dependency
184+
((pkgs.writeTextDir "/plugin/test.lua" "vim.opt.tabstop = 2").overrideAttrs {
185+
passthru.python3Dependencies = ps: [ ps.pyyaml ];
186+
})
187+
# Plugin with invalid lua file tests/indent/lua/cond.lua (should be ignored)
188+
nvim-treesitter
189+
];
190+
191+
extraConfigLuaPost = ''
192+
${isByteCompiledFun}
193+
194+
-- Plugins are loadable
195+
require("lspconfig")
196+
require("telescope")
197+
require("plenary")
198+
require("nvim-treesitter")
199+
200+
-- Python modules are importable
201+
vim.cmd.py3("import yaml")
202+
203+
-- nvim-lspconfig
204+
test_rtp_file("lua/lspconfig.lua", true)
205+
test_rtp_file("lua/lspconfig/server_configurations/nixd.lua", true)
206+
test_rtp_file("plugin/lspconfig.lua", true)
207+
test_rtp_file("doc/lspconfig.txt", false)
208+
209+
-- telescope-nvim
210+
test_rtp_file("lua/telescope/init.lua", true)
211+
test_rtp_file("lua/telescope/builtin/init.lua", true)
212+
test_rtp_file("plugin/telescope.lua", true)
213+
test_rtp_file("autoload/health/telescope.vim", false)
214+
test_rtp_file("doc/telescope.txt", false)
215+
216+
-- Dependency of telescope-nvim (plenary-nvim)
217+
test_rtp_file("lua/plenary/init.lua", true)
218+
test_rtp_file("plugin/plenary.vim", false)
219+
220+
-- Test plugin
221+
test_rtp_file("plugin/test.lua", true)
222+
223+
-- nvim-treesitter
224+
test_rtp_file("lua/nvim-treesitter/health.lua", true)
225+
test_rtp_file("lua/nvim-treesitter/install.lua", true)
226+
test_rtp_file("plugin/nvim-treesitter.lua", true)
227+
test_rtp_file("queries/nix/highlights.scm", false)
228+
'';
229+
})

0 commit comments

Comments
 (0)