Skip to content

Commit

Permalink
modules/performance: add support for byte compiling lua plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
stasjok committed Jul 18, 2024
1 parent ed13ee1 commit 90a5725
Show file tree
Hide file tree
Showing 3 changed files with 102 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 @@ -41,6 +41,9 @@ in
description = "Whether to byte compile lua configuration files.";
default = true;
};
plugins = lib.mkEnableOption "plugins" // {
description = "Whether to byte compile lua plugins.";
};
};
};

Expand Down
33 changes: 31 additions & 2 deletions modules/top-level/output.nix
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,37 @@ 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:
p.overrideAttrs (
prev:
{
nativeBuildInputs = prev.nativeBuildInputs or [ ] ++ [ helpers.byteCompileLuaHook ];
}
// lib.optionalAttrs (prev ? buildCommand) {
buildCommand = ''
${prev.buildCommand}
runHook postFixup
'';
}
// 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 @@ -141,3 +141,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 90a5725

Please sign in to comment.