2024-09-12 14:56:36 +01:00
|
|
|
{ pkgs, ... }:
|
2024-07-25 13:25:13 +03:00
|
|
|
let
|
2025-04-29 17:22:26 +03:00
|
|
|
isByteCompiledFun = # lua
|
|
|
|
''
|
|
|
|
-- LuaJIT bytecode header is: ESC L J version
|
|
|
|
-- https://github.com/LuaJIT/LuaJIT/blob/v2.1/src/lj_bcdump.h
|
|
|
|
-- We are comparing the first three bytes of the file (until version)
|
|
|
|
local expected_header = string.char(0x1b, 0x4c, 0x4a)
|
|
|
|
local function is_byte_compiled(filename)
|
|
|
|
local f = assert(io.open(filename, "rb"))
|
|
|
|
local data = assert(f:read(3))
|
|
|
|
f:close()
|
|
|
|
return data == expected_header
|
2024-07-25 13:25:13 +03:00
|
|
|
end
|
|
|
|
|
2025-04-29 17:22:26 +03:00
|
|
|
local function test_rtp_file(name, is_compiled)
|
|
|
|
local file = assert(vim.api.nvim_get_runtime_file(name, false)[1], "file " .. name .. " not found in runtime")
|
|
|
|
if is_compiled then
|
|
|
|
assert(is_byte_compiled(file), name .. " is expected to be byte compiled, but it's not")
|
|
|
|
else
|
|
|
|
assert(not is_byte_compiled(file), name .. " is not expected to be byte compiled, but it is")
|
|
|
|
end
|
2024-07-25 13:25:13 +03:00
|
|
|
end
|
2025-04-29 17:22:26 +03:00
|
|
|
'';
|
2025-04-30 21:34:01 +03:00
|
|
|
|
|
|
|
# Stub plugin built with mkDerivation
|
|
|
|
stubDrvPlugin = pkgs.stdenvNoCC.mkDerivation {
|
|
|
|
name = "stub_drv_plugin";
|
|
|
|
src = pkgs.emptyDirectory;
|
|
|
|
buildPhase = ''
|
|
|
|
mkdir -p "$out/lua/$name"
|
|
|
|
echo "return '$name'" >"$out/lua/$name/init.lua"
|
|
|
|
mkdir $out/plugin
|
|
|
|
echo "_G['$name'] = true" >"$out/plugin/$name.lua"
|
|
|
|
echo "let g:$name = 1" >"$out/plugin/$name.vim"
|
|
|
|
'';
|
|
|
|
dependencies = [ stubBuildCommandPlugin ];
|
|
|
|
};
|
|
|
|
# Stub plugin built with buildCommand with python dependency
|
|
|
|
stubBuildCommandPlugin = pkgs.writeTextFile {
|
|
|
|
name = "stub_build_command_plugin";
|
|
|
|
text = ''
|
|
|
|
return "stub_build_command_plugin"
|
|
|
|
'';
|
|
|
|
destination = "/lua/stub_build_command_plugin/init.lua";
|
|
|
|
derivationArgs = {
|
|
|
|
dependencies = [ stubDependentPlugin ];
|
|
|
|
passthru.python3Dependencies = ps: [ ps.pyyaml ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
# Dependent stub plugin
|
|
|
|
stubDependentPlugin = pkgs.writeTextFile {
|
|
|
|
name = "stub_dependent_plugin";
|
|
|
|
text = ''
|
|
|
|
return "stub_dependent_plugin"
|
|
|
|
'';
|
|
|
|
destination = "/lua/stub_dependent_plugin/init.lua";
|
|
|
|
};
|
|
|
|
# Stub plugin with an invalid lua file
|
|
|
|
stubInvalidFilePlugin = pkgs.runCommand "stub_invalid_file_plugin" { } ''
|
|
|
|
mkdir -p "$out/lua/$name"
|
|
|
|
echo "return '$name'" >"$out/lua/$name/init.lua"
|
|
|
|
mkdir "$out/ftplugin"
|
|
|
|
echo "if true then" >"$out/ftplugin/invalid.lua"
|
|
|
|
'';
|
2024-07-25 13:25:13 +03:00
|
|
|
in
|
|
|
|
{
|
2024-08-20 00:54:50 +01:00
|
|
|
default =
|
2024-09-12 14:56:36 +01:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
writeLua = lib.nixvim.builders.writeLuaWith pkgs;
|
|
|
|
in
|
2024-07-25 13:25:13 +03:00
|
|
|
{
|
|
|
|
performance.byteCompileLua.enable = true;
|
|
|
|
|
|
|
|
extraFiles = {
|
2024-07-25 18:09:16 +03:00
|
|
|
# By text
|
2024-07-25 13:25:13 +03:00
|
|
|
"plugin/file_text.lua".text = "vim.opt.tabstop = 2";
|
2024-07-25 18:09:16 +03:00
|
|
|
# By simple source derivation using buildCommand
|
2024-09-12 14:56:36 +01:00
|
|
|
"plugin/file_source.lua".source = writeLua "file_source.lua" "vim.opt.tabstop = 2";
|
2024-07-25 18:09:16 +03:00
|
|
|
# By standard derivation, it needs to execute fixupPhase
|
|
|
|
"plugin/file_drv.lua".source = pkgs.stdenvNoCC.mkDerivation {
|
|
|
|
name = "file_drv.lua";
|
|
|
|
src = pkgs.emptyDirectory;
|
|
|
|
buildPhase = ''
|
|
|
|
echo "vim.opt.tabstop = 2" > $out
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
# By path
|
|
|
|
"plugin/file_path.lua".source = ./files/file.lua;
|
|
|
|
# By string
|
|
|
|
"plugin/file_string.lua".source = builtins.toFile "file_path.lua" "vim.opt.tabstop = 2";
|
|
|
|
# By derivation converted to string
|
|
|
|
"plugin/file_drv_string.lua".source = toString (
|
2024-09-12 14:56:36 +01:00
|
|
|
writeLua "file_drv_string.lua" "vim.opt.tabstop = 2"
|
2024-07-25 18:09:16 +03:00
|
|
|
);
|
|
|
|
# Non-lua files
|
2024-07-25 13:25:13 +03:00
|
|
|
"plugin/test.vim".text = "set tabstop=2";
|
|
|
|
"plugin/test.json".text = builtins.toJSON { a = 1; };
|
2024-07-25 18:09:16 +03:00
|
|
|
# Lua file with txt extension won't be byte compiled
|
2024-09-12 14:56:36 +01:00
|
|
|
"test.txt".source = writeLua "test.txt" "vim.opt.tabstop = 2";
|
2024-07-25 13:25:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
files = {
|
|
|
|
"plugin/file.lua" = {
|
|
|
|
opts.tabstop = 2;
|
|
|
|
};
|
|
|
|
"plugin/file.vim" = {
|
|
|
|
opts.tabstop = 2;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2025-04-30 21:34:01 +03:00
|
|
|
extraPlugins = [ stubDrvPlugin ];
|
2024-07-25 13:25:13 +03:00
|
|
|
|
|
|
|
extraConfigLua = ''
|
|
|
|
-- The test will search for this string in nixvim-print-init output: VALIDATING_STRING.
|
|
|
|
-- Since this is the comment, it won't appear in byte compiled file.
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Using plugin for the test code to avoid infinite recursion
|
2025-04-30 21:34:01 +03:00
|
|
|
extraFiles."plugin/test.lua".text =
|
|
|
|
# lua
|
|
|
|
''
|
|
|
|
${isByteCompiledFun}
|
|
|
|
|
|
|
|
-- vimrc is byte compiled
|
|
|
|
local init = vim.env.MYVIMRC or vim.fn.getscriptinfo({name = "init.lua"})[1].name
|
|
|
|
assert(is_byte_compiled(init), "MYVIMRC is expected to be byte compiled, but it's not")
|
|
|
|
|
|
|
|
-- nixvim-print-init prints text
|
|
|
|
local init_content = vim.fn.system("${lib.getExe config.build.printInitPackage}")
|
|
|
|
assert(init_content:find("VALIDATING_STRING"), "nixvim-print-init's output is byte compiled")
|
|
|
|
|
|
|
|
-- lua extraFiles are byte compiled
|
|
|
|
test_rtp_file("plugin/file_text.lua", true)
|
|
|
|
test_rtp_file("plugin/file_source.lua", true)
|
|
|
|
test_rtp_file("plugin/file_drv.lua", true)
|
|
|
|
test_rtp_file("plugin/file_path.lua", true)
|
|
|
|
test_rtp_file("plugin/file_string.lua", true)
|
|
|
|
test_rtp_file("plugin/file_drv_string.lua", true)
|
|
|
|
test_rtp_file("plugin/test.vim", false)
|
|
|
|
test_rtp_file("plugin/test.json", false)
|
|
|
|
test_rtp_file("test.txt", false)
|
|
|
|
|
|
|
|
-- lua files are byte compiled
|
|
|
|
test_rtp_file("plugin/file.lua", true)
|
|
|
|
test_rtp_file("plugin/file.vim", false)
|
|
|
|
|
|
|
|
-- Plugins and neovim runtime aren't byte compiled by default
|
|
|
|
test_rtp_file("lua/vim/lsp.lua", false)
|
|
|
|
test_rtp_file("lua/stub_drv_plugin/init.lua", false)
|
|
|
|
'';
|
2024-07-25 13:25:13 +03:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2024-08-20 00:54:50 +01:00
|
|
|
disabled =
|
2024-09-24 07:02:22 +01:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}:
|
2024-07-25 13:25:13 +03:00
|
|
|
{
|
|
|
|
performance.byteCompileLua.enable = false;
|
|
|
|
|
|
|
|
extraFiles."plugin/test1.lua".text = "vim.opt.tabstop = 2";
|
|
|
|
|
|
|
|
files."plugin/test2.lua".opts.tabstop = 2;
|
|
|
|
|
2025-04-30 21:34:01 +03:00
|
|
|
extraPlugins = [ stubDrvPlugin ];
|
2024-07-25 13:25:13 +03:00
|
|
|
|
|
|
|
extraConfigLua = ''
|
|
|
|
-- The test will search for this string in nixvim-print-init output: VALIDATING_STRING.
|
|
|
|
-- Since this is the comment, it won't appear in byte compiled file.
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Using plugin for the test code to avoid infinite recursion
|
2025-04-30 21:34:01 +03:00
|
|
|
extraFiles."plugin/test.lua".text =
|
|
|
|
# lua
|
|
|
|
''
|
|
|
|
${isByteCompiledFun}
|
|
|
|
|
|
|
|
-- vimrc
|
|
|
|
local init = vim.env.MYVIMRC or vim.fn.getscriptinfo({name = "init.lua"})[1].name
|
|
|
|
assert(not is_byte_compiled(init), "MYVIMRC is not expected to be byte compiled, but it is")
|
|
|
|
|
|
|
|
-- nixvim-print-init prints text
|
|
|
|
local init_content = vim.fn.system("${lib.getExe config.build.printInitPackage}")
|
|
|
|
assert(init_content:find("VALIDATING_STRING"), "nixvim-print-init's output is byte compiled")
|
|
|
|
|
|
|
|
-- Nothing is byte compiled
|
|
|
|
-- extraFiles
|
|
|
|
test_rtp_file("plugin/test1.lua", false)
|
|
|
|
-- files
|
|
|
|
test_rtp_file("plugin/test2.lua", false)
|
|
|
|
-- Plugins
|
|
|
|
test_rtp_file("lua/stub_drv_plugin/init.lua", false)
|
|
|
|
-- Neovim runtime
|
|
|
|
test_rtp_file("lua/vim/lsp.lua", false)
|
|
|
|
'';
|
2024-07-25 13:25:13 +03:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
init-lua-disabled = {
|
|
|
|
performance.byteCompileLua = {
|
|
|
|
enable = true;
|
|
|
|
initLua = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfigLuaPost = ''
|
|
|
|
${isByteCompiledFun}
|
|
|
|
|
|
|
|
-- vimrc is not byte compiled
|
|
|
|
local init = vim.env.MYVIMRC or vim.fn.getscriptinfo({name = "init.lua"})[1].name
|
|
|
|
assert(not is_byte_compiled(init), "MYVIMRC is not expected to be byte compiled, but it is")
|
|
|
|
'';
|
|
|
|
};
|
2024-07-25 18:09:16 +03:00
|
|
|
|
|
|
|
configs-disabled = {
|
|
|
|
performance.byteCompileLua = {
|
|
|
|
enable = true;
|
|
|
|
configs = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
extraFiles."plugin/test1.lua".text = "vim.opt.tabstop = 2";
|
|
|
|
|
|
|
|
files."plugin/test2.lua".opts.tabstop = 2;
|
|
|
|
|
|
|
|
extraConfigLuaPost = ''
|
|
|
|
${isByteCompiledFun}
|
|
|
|
|
|
|
|
-- extraFiles
|
|
|
|
test_rtp_file("plugin/test1.lua", false)
|
|
|
|
-- files
|
|
|
|
test_rtp_file("plugin/test2.lua", false)
|
|
|
|
'';
|
|
|
|
};
|
2024-07-17 22:26:06 +03:00
|
|
|
|
|
|
|
nvim-runtime = {
|
|
|
|
performance.byteCompileLua = {
|
|
|
|
enable = true;
|
|
|
|
nvimRuntime = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
extraPlugins = [
|
2025-04-30 21:34:01 +03:00
|
|
|
stubBuildCommandPlugin
|
2024-07-17 22:26:06 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
extraConfigLuaPost = ''
|
|
|
|
${isByteCompiledFun}
|
|
|
|
|
|
|
|
-- vim namespace is working
|
|
|
|
vim.opt.tabstop = 2
|
|
|
|
vim.api.nvim_get_runtime_file("init.lua", false)
|
|
|
|
vim.lsp.get_clients()
|
|
|
|
vim.treesitter.language.get_filetypes("nix")
|
|
|
|
vim.iter({})
|
|
|
|
|
|
|
|
test_rtp_file("lua/vim/lsp.lua", true)
|
|
|
|
test_rtp_file("lua/vim/iter.lua", true)
|
|
|
|
test_rtp_file("lua/vim/treesitter/query.lua", true)
|
|
|
|
test_rtp_file("lua/vim/lsp/buf.lua", true)
|
|
|
|
test_rtp_file("plugin/editorconfig.lua", true)
|
|
|
|
test_rtp_file("plugin/tutor.vim", false)
|
|
|
|
test_rtp_file("ftplugin/vim.vim", false)
|
|
|
|
|
|
|
|
-- Python3 packages are importable
|
|
|
|
vim.cmd.py3("import yaml")
|
|
|
|
'';
|
|
|
|
};
|
2024-07-25 13:25:13 +03:00
|
|
|
}
|
2024-07-17 21:06:30 +03:00
|
|
|
//
|
|
|
|
# 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;
|
|
|
|
};
|
|
|
|
|
2025-04-30 21:34:01 +03:00
|
|
|
extraPlugins = [
|
|
|
|
# Depends on stubBuildCommandPlugin -> stubDependentPlugin
|
|
|
|
stubDrvPlugin
|
|
|
|
# Plugin with invalid
|
|
|
|
stubInvalidFilePlugin
|
2024-07-17 21:06:30 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
extraConfigLuaPost = ''
|
|
|
|
${isByteCompiledFun}
|
|
|
|
|
|
|
|
-- Plugins are loadable
|
2025-04-30 21:34:01 +03:00
|
|
|
require("stub_drv_plugin")
|
|
|
|
require("stub_build_command_plugin")
|
|
|
|
require("stub_dependent_plugin")
|
|
|
|
require("stub_invalid_file_plugin")
|
2024-07-17 21:06:30 +03:00
|
|
|
|
|
|
|
-- Python modules are importable
|
|
|
|
vim.cmd.py3("import yaml")
|
|
|
|
|
2025-04-30 21:34:01 +03:00
|
|
|
-- stubDrvPlugin
|
|
|
|
test_rtp_file("lua/stub_drv_plugin/init.lua", true)
|
|
|
|
test_rtp_file("plugin/stub_drv_plugin.lua", true)
|
|
|
|
test_rtp_file("plugin/stub_drv_plugin.vim", false)
|
2024-07-17 21:06:30 +03:00
|
|
|
|
2025-04-30 21:34:01 +03:00
|
|
|
-- stubBuildCommandPlugin
|
|
|
|
test_rtp_file("lua/stub_build_command_plugin/init.lua", true)
|
2024-07-17 21:06:30 +03:00
|
|
|
|
2025-04-30 21:34:01 +03:00
|
|
|
-- stubDependentPlugin
|
|
|
|
test_rtp_file("lua/stub_dependent_plugin/init.lua", true)
|
2024-07-17 21:06:30 +03:00
|
|
|
|
2025-04-30 21:34:01 +03:00
|
|
|
-- stubInvalidFilePlugin
|
|
|
|
test_rtp_file("lua/stub_invalid_file_plugin/init.lua", true)
|
|
|
|
test_rtp_file("ftplugin/invalid.lua", false)
|
2024-07-17 21:06:30 +03:00
|
|
|
'';
|
|
|
|
})
|