Skip to content

Commit ed13ee1

Browse files
committed
modules/performance: add byteCompileLua option
1 parent 199745c commit ed13ee1

File tree

8 files changed

+364
-23
lines changed

8 files changed

+364
-23
lines changed

flake-modules/wrappers.nix

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
inherit (inputs) home-manager;
1616
nixvim = self;
1717
}).activationPackage;
18+
home-manager-extra-files-byte-compiling =
19+
import ../tests/modules/hm-extra-files-byte-compiling.nix
20+
{
21+
inherit pkgs;
22+
inherit (inputs) home-manager;
23+
nixvim = self;
24+
};
1825
}
1926
// pkgs.lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
2027
nixos-module =

lib/builders.nix

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,46 @@
2626
--indent-width 4 \
2727
"$out"
2828
'';
29+
30+
/*
31+
Write a byte compiled lua file to the nix store.
32+
33+
# Type
34+
35+
```
36+
writeByteCompiledLua :: String -> String -> Derivation
37+
```
38+
39+
# Arguments
40+
41+
- [name] The name of the derivation
42+
- [text] The content of the lua file
43+
*/
44+
writeByteCompiledLua =
45+
name: text:
46+
pkgs.runCommandLocal name { inherit text; } ''
47+
echo -n "$text" > "$out"
48+
49+
${lib.getExe' pkgs.luajit "luajit"} -bd -- "$out" "$out"
50+
'';
51+
52+
# Setup hook to byte compile all lua files in output directory
53+
byteCompileLuaHook = pkgs.makeSetupHook { name = "byte-compile-lua-hook"; } (
54+
let
55+
luajit = lib.getExe' pkgs.luajit "luajit";
56+
in
57+
pkgs.writeText "byte-compile-lua-hook.sh" # bash
58+
''
59+
byteCompileLuaPostFixup() {
60+
while IFS= read -r -d "" file; do
61+
tmp=$(mktemp -u "$file.XXXX")
62+
if ${luajit} -bd -- "$file" "$tmp"; then
63+
mv "$tmp" "$file"
64+
fi
65+
done < <(find "$out" -type f,l -name "*.lua" -print0)
66+
}
67+
68+
postFixupHooks+=(byteCompileLuaPostFixup)
69+
''
70+
);
2971
}

modules/performance.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ in
3030
description = "List of plugins (names or packages) to exclude from plugin pack.";
3131
};
3232
};
33+
34+
byteCompileLua = {
35+
enable = lib.mkEnableOption "byte compiling of lua files";
36+
initLua = lib.mkEnableOption "initLua" // {
37+
description = "Whether to byte compile init.lua.";
38+
default = true;
39+
};
40+
configs = lib.mkEnableOption "configs" // {
41+
description = "Whether to byte compile lua configuration files.";
42+
default = true;
43+
};
44+
};
3345
};
3446

3547
config.performance = {

modules/top-level/files/default.nix

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,36 @@ in
7272

7373
# A directory with all the files in it
7474
# Implementation based on NixOS's /etc module
75-
filesPlugin = pkgs.runCommandLocal "nvim-config" { } ''
76-
set -euo pipefail
75+
filesPlugin =
76+
let
77+
needByteCompiling =
78+
config.performance.byteCompileLua.enable && config.performance.byteCompileLua.configs;
79+
in
80+
pkgs.runCommandLocal "nvim-config"
81+
{ nativeBuildInputs = lib.optional needByteCompiling helpers.byteCompileLuaHook; }
82+
''
83+
set -euo pipefail
7784
78-
makeEntry() {
79-
src="$1"
80-
target="$2"
81-
mkdir -p "$out/$(dirname "$target")"
82-
cp "$src" "$out/$target"
83-
}
85+
makeEntry() {
86+
src="$1"
87+
target="$2"
88+
mkdir -p "$out/$(dirname "$target")"
89+
cp "$src" "$out/$target"
90+
}
8491
85-
mkdir -p "$out"
86-
${lib.concatMapStringsSep "\n" (
87-
{ target, source, ... }:
88-
lib.escapeShellArgs [
89-
"makeEntry"
90-
# Force local source paths to be added to the store
91-
"${source}"
92-
target
93-
]
94-
) extraFiles}
95-
'';
92+
mkdir -p "$out"
93+
${lib.concatMapStringsSep "\n" (
94+
{ target, source, ... }:
95+
lib.escapeShellArgs [
96+
"makeEntry"
97+
# Force local source paths to be added to the store
98+
"${source}"
99+
target
100+
]
101+
) extraFiles}
102+
103+
${lib.optionalString needByteCompiling "runHook postFixup"}
104+
'';
96105

97106
# Never combine user files with the rest of the plugins
98107
performance.combinePlugins.standalonePlugins = [ config.filesPlugin ];

modules/top-level/output.nix

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,17 @@ in
213213
'')
214214
+ config.content;
215215

216-
init = helpers.writeLua "init.lua" customRC;
216+
textInit = helpers.writeLua "init.lua" customRC;
217+
byteCompiledInit = helpers.writeByteCompiledLua "init.lua" customRC;
218+
init =
219+
if
220+
config.type == "lua"
221+
&& config.performance.byteCompileLua.enable
222+
&& config.performance.byteCompileLua.initLua
223+
then
224+
byteCompiledInit
225+
else
226+
textInit;
217227

218228
extraWrapperArgs = builtins.concatStringsSep " " (
219229
(optional (
@@ -239,7 +249,7 @@ in
239249
name = "nixvim-print-init";
240250
runtimeInputs = [ pkgs.bat ];
241251
text = ''
242-
bat --language=lua "${init}"
252+
bat --language=lua "${textInit}"
243253
'';
244254
};
245255

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
nixvim,
3+
pkgs,
4+
home-manager,
5+
}:
6+
let
7+
config = {
8+
home = {
9+
username = "nixvim";
10+
homeDirectory = "/invalid/dir";
11+
stateVersion = "24.05";
12+
};
13+
14+
programs.nixvim = {
15+
enable = true;
16+
17+
performance.byteCompileLua.enable = true;
18+
19+
extraFiles = {
20+
"extra-files/test1.lua".text = "vim.opt.tabstop = 2";
21+
"extra-files/test2.lua".source = builtins.toFile "file_source.lua" "vim.opt.tabstop = 2";
22+
"extra-files/test.vim".text = "set tabstop=2";
23+
"extra-files/test.json".text = builtins.toJSON { a = 1; };
24+
};
25+
26+
files = {
27+
"files/test.lua".opts.tabstop = 2;
28+
"files/test.vim".opts.tabstop = 2;
29+
};
30+
};
31+
};
32+
33+
homeFilesByteCompilingEnabled =
34+
(home-manager.lib.homeManagerConfiguration {
35+
inherit pkgs;
36+
37+
modules = [
38+
nixvim.homeManagerModules.nixvim
39+
config
40+
{ programs.nixvim.performance.byteCompileLua.configs = true; }
41+
];
42+
}).config.home-files;
43+
44+
homeFilesByteCompilingDisabled =
45+
(home-manager.lib.homeManagerConfiguration {
46+
inherit pkgs;
47+
48+
modules = [
49+
nixvim.homeManagerModules.nixvim
50+
config
51+
{ programs.nixvim.performance.byteCompileLua.configs = false; }
52+
];
53+
}).config.home-files;
54+
in
55+
pkgs.runCommand "home-manager-extra-files-byte-compiling" { } ''
56+
is_binary() {
57+
! grep -qI . "$1"
58+
}
59+
test_byte_compiled() {
60+
if ! is_binary "$home_files/.config/nvim/$1"; then
61+
echo "File $1 is expected to be byte compiled, but it's not"
62+
exit 1
63+
fi
64+
}
65+
test_not_byte_compiled() {
66+
if is_binary "$home_files/.config/nvim/$1"; then
67+
echo "File $1 is not expected to be byte compiled, but it is"
68+
exit 1
69+
fi
70+
}
71+
72+
# Test directory with extraFiles byte compiling enabled
73+
home_files="${homeFilesByteCompilingEnabled}"
74+
75+
echo "Testing home-files with extraFiles byte compiling enabled"
76+
77+
# extraFiles
78+
test_byte_compiled extra-files/test1.lua
79+
test_byte_compiled extra-files/test2.lua
80+
test_not_byte_compiled extra-files/test.vim
81+
test_not_byte_compiled extra-files/test.json
82+
# files
83+
test_byte_compiled files/test.lua
84+
test_not_byte_compiled files/test.vim
85+
86+
# Test directory with extraFiles byte compiling disabled
87+
home_files="${homeFilesByteCompilingDisabled}"
88+
89+
echo "Testing home-files with extraFiles byte compiling disabled"
90+
91+
# extraFiles
92+
test_not_byte_compiled extra-files/test1.lua
93+
test_not_byte_compiled extra-files/test2.lua
94+
test_not_byte_compiled extra-files/test.vim
95+
test_not_byte_compiled extra-files/test.json
96+
# files
97+
test_not_byte_compiled files/test.lua
98+
test_not_byte_compiled files/test.vim
99+
100+
touch $out
101+
''

0 commit comments

Comments
 (0)