modules/performance: add ability to byte compile lua configuration files

This commit adds support for byte compiling lua configuration files.
It's enabled by default (if byte compiling is enabled at all) and can be
disabled with `performance.byteCompileLua.configs` toggle.

To implement this feature `extraFiles.<name>.finalSource` internal
read-only option is introduced. `source` option cannot be used because
it's user configurable. In order to access the values of the
`performance.byteCompileLua` options, parent config is added to
specialArgs of extraFiles submodule. Then the usages of `source` option
changed to `finalSource` in all relevant places (filesPlugin and
wrappers).

Added more helpers for various cases of byte compiling:

* `byteCompileLuaFile` byte compiles lua file
* `byteCompileLuaHook` is a setup hook that byte compiles all lua files
* `byteCompileLuaDrv` overrides derivation by adding byteCompileLuaHook
  to it

Added tests to validate that extraFiles specified by various methods are
handled correctly. Added a separate home-manager test, that is intended
to validate that extraFiles propagated to wrapper modules are correctly
byte compiled.
This commit is contained in:
Stanislav Asunkin 2024-07-25 18:09:16 +03:00 committed by GaetanLepage
parent 17e8904992
commit 44849233e0
10 changed files with 292 additions and 14 deletions

View file

@ -2,14 +2,16 @@
lib,
helpers,
pkgs,
config,
...
}:
let
fileType = lib.types.submodule (
fileTypeModule =
{
name,
config,
options,
topConfig,
...
}:
{
@ -41,6 +43,14 @@ let
type = lib.types.path;
description = "Path of the source file.";
};
finalSource = lib.mkOption {
type = lib.types.path;
description = "Path to the final source file.";
readOnly = true;
visible = false;
internal = true;
};
};
config =
@ -54,9 +64,29 @@ let
# This means our `source` definition has the same priority as `text`.
lib.mkDerivedConfig options.text (pkgs.writeText derivationName)
);
finalSource =
# Byte compile lua files if performance.byteCompileLua option is enabled
if
lib.hasSuffix ".lua" config.target
&& topConfig.performance.byteCompileLua.enable
&& topConfig.performance.byteCompileLua.configs
then
if lib.isDerivation config.source then
# Source is a derivation
helpers.byteCompileLuaDrv config.source
else
# Source is a path or string
helpers.byteCompileLuaFile derivationName config.source
else
config.source;
};
}
);
};
fileType = lib.types.submoduleWith {
shorthandOnlyDefinesConfig = true;
modules = [ fileTypeModule ];
specialArgs.topConfig = config;
};
# TODO: Added 2024-07-07, remove after 24.11
# Before we had a fileType, we used types.str.