modules/performance: add ability to byte compile extraLuaPackages

This commit adds `performance.byteCompileLua.luaLib` options. When
enabled it byte-compiles lua packages from extraLuaPackages option.
This commit is contained in:
Stanislav Asunkin 2025-05-03 18:25:49 +03:00
parent fac192c022
commit 404e56066f
3 changed files with 64 additions and 9 deletions

View file

@ -57,6 +57,9 @@ in
nvimRuntime = lib.mkEnableOption "nvimRuntime" // {
description = "Whether to byte compile lua files in Nvim runtime.";
};
luaLib = lib.mkEnableOption "luaLib" // {
description = "Whether to byte compile lua library.";
};
};
combinePlugins = {

View file

@ -171,11 +171,18 @@ in
config =
let
# Optionally byte compile lua library
extraLuaPackages =
if config.performance.byteCompileLua.enable && config.performance.byteCompileLua.luaLib then
ps: map builders.byteCompileLuaDrv (config.extraLuaPackages ps)
else
config.extraLuaPackages;
neovimConfig = pkgs.neovimUtils.makeNeovimConfig (
{
inherit extraLuaPackages;
inherit (config)
extraPython3Packages
extraLuaPackages
viAlias
vimAlias
withRuby

View file

@ -22,16 +22,29 @@ let
end
end
local function test_lualib_file(func, is_compiled)
-- Get the source of the func
local info = debug.getinfo(func, "S")
-- The source returned by debug.getinfo is prefixed with '@'
local file = info.source:sub(2)
assert(vim.uv.fs_stat(file))
if is_compiled then
assert(is_byte_compiled(file), file .. " is expected to be byte compiled, but it's not")
else
assert(not is_byte_compiled(file), file .. " is not expected to be byte compiled, but it is")
end
end
local function assert_g_var(varname, filename)
assert(
vim.g[varname] == true or vim.g[varname] == 1,
string.format(
"expected vim.g.%s to be truthy, got %s. File %s isn't executed?",
varname,
vim.g[varname],
filename
)
assert(
vim.g[varname] == true or vim.g[varname] == 1,
string.format(
"expected vim.g.%s to be truthy, got %s. File %s isn't executed?",
varname,
vim.g[varname],
filename
)
)
end
'';
@ -130,6 +143,8 @@ in
extraPlugins = [ stubDrvPlugin ];
extraLuaPackages = ps: [ ps.say ];
extraConfigLua = ''
-- The test will search for the next string in nixvim-print-init's output: VALIDATING_STRING.
-- Since this is the comment, it won't appear in byte compiled file.
@ -180,6 +195,9 @@ in
-- Nvim runtime isn't byte compiled by default
test_rtp_file("lua/vim/lsp.lua", false)
-- Lua library isn't byte compiled by default
test_lualib_file(require("say").set, false)
'';
};
@ -193,6 +211,8 @@ in
extraPlugins = [ stubDrvPlugin ];
extraLuaPackages = ps: [ ps.say ];
extraConfigLua = ''
${isByteCompiledFun}
@ -208,6 +228,8 @@ in
test_rtp_file("lua/stub_drv_plugin/init.lua", false)
-- Neovim runtime
test_rtp_file("lua/vim/lsp.lua", false)
-- Lua library
test_lualib_file(require("say").set, false)
'';
};
@ -276,6 +298,29 @@ in
vim.cmd.py3("import yaml")
'';
};
lua-lib = {
performance.byteCompileLua = {
enable = true;
luaLib = true;
};
extraLuaPackages =
ps: with ps; [
say
argparse
];
extraConfigLuaPost = ''
${isByteCompiledFun}
-- Lua modules are importable and byte compiled
local say = require("say")
test_lualib_file(say.set, true)
local argparse = require("argparse")
test_lualib_file(argparse("test").parse, true)
'';
};
}
//
# Two equal tests, one with combinePlugins.enable = true