Skip to content

Commit

Permalink
modules/performance: add ability to byte compile lua plugins
Browse files Browse the repository at this point in the history
This commit adds `performance.byteCompileLua.plugins` toggle that, if
enabled, byte compiles all lua files in plugins
  • Loading branch information
stasjok committed Jul 26, 2024
1 parent ee96a1b commit ef04f61
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
3 changes: 3 additions & 0 deletions modules/performance.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ in
default = true;
example = false;
};
plugins = lib.mkEnableOption "plugins" // {
description = "Whether to byte compile lua plugins.";
};
};

combinePlugins = {
Expand Down
23 changes: 21 additions & 2 deletions modules/top-level/output.nix
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,27 @@ in
defaultPlugin // (if p ? plugin then p else { plugin = p; });
normalizePluginList = plugins: map normalize plugins;

# Normalized plugin list
normalizedPlugins = normalizePluginList config.extraPlugins;
# Byte compiling of normalized plugin list
byteCompilePlugins =
plugins:
let
byteCompile =
p:
(helpers.byteCompileLuaDrv p).overrideAttrs (
prev: lib.optionalAttrs (prev ? dependencies) { dependencies = map byteCompile prev.dependencies; }
);
in
map (p: p // { plugin = byteCompile p.plugin; }) plugins;

# Normalized and optionally byte compiled plugin list
normalizedPlugins =
let
normalized = normalizePluginList config.extraPlugins;
in
if config.performance.byteCompileLua.enable && config.performance.byteCompileLua.plugins then
byteCompilePlugins normalized
else
normalized;

# Plugin list extended with dependencies
allPlugins =
Expand Down
68 changes: 68 additions & 0 deletions tests/test-sources/modules/performance/byte-compile-lua.nix
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,71 @@ in
'';
};
}
//
# Two equal tests, one with combinePlugins.enable = true
pkgs.lib.genAttrs
[
"plugins"
"plugins-combined"
]
(name: {
performance = {
byteCompileLua = {
enable = true;
plugins = true;
};

combinePlugins.enable = pkgs.lib.hasSuffix "combined" name;
};

extraPlugins = with pkgs.vimPlugins; [
nvim-lspconfig
# Depends on plenary-nvim
telescope-nvim
# buildCommand plugin with python3 dependency
((pkgs.writeTextDir "/plugin/test.lua" "vim.opt.tabstop = 2").overrideAttrs {
passthru.python3Dependencies = ps: [ ps.pyyaml ];
})
# Plugin with invalid lua file tests/indent/lua/cond.lua (should be ignored)
nvim-treesitter
];

extraConfigLuaPost = ''
${isByteCompiledFun}
-- Plugins are loadable
require("lspconfig")
require("telescope")
require("plenary")
require("nvim-treesitter")
-- Python modules are importable
vim.cmd.py3("import yaml")
-- nvim-lspconfig
test_rtp_file("lua/lspconfig.lua", true)
test_rtp_file("lua/lspconfig/server_configurations/nixd.lua", true)
test_rtp_file("plugin/lspconfig.lua", true)
test_rtp_file("doc/lspconfig.txt", false)
-- telescope-nvim
test_rtp_file("lua/telescope/init.lua", true)
test_rtp_file("lua/telescope/builtin/init.lua", true)
test_rtp_file("plugin/telescope.lua", true)
test_rtp_file("autoload/health/telescope.vim", false)
test_rtp_file("doc/telescope.txt", false)
-- Dependency of telescope-nvim (plenary-nvim)
test_rtp_file("lua/plenary/init.lua", true)
test_rtp_file("plugin/plenary.vim", false)
-- Test plugin
test_rtp_file("plugin/test.lua", true)
-- nvim-treesitter
test_rtp_file("lua/nvim-treesitter/health.lua", true)
test_rtp_file("lua/nvim-treesitter/install.lua", true)
test_rtp_file("plugin/nvim-treesitter.lua", true)
test_rtp_file("queries/nix/highlights.scm", false)
'';
})

0 comments on commit ef04f61

Please sign in to comment.