buildLuarocksPackage: accept structured luarocks config

There is an arbitrary mapping being done right now between
nixpkgs lua infrastructre and luarocks config schema.
This is confusing if you use lua so let's make it possible to use the
lua names in the nixpkgs, thanks to the lib.generators.toLua convertor.

The only nixpkgs thing to remember should be to put the config into `luarocksConfig`

`buildLuarocksPackage.extraVariables` should become `buildLuarocksPackage.luarocksConfig.variables`
This commit is contained in:
Matthieu Coudron 2024-02-11 19:36:56 +01:00
parent bc9b32647a
commit 50e877ed89
5 changed files with 77 additions and 50 deletions

View File

@ -215,6 +215,11 @@ install the package
environment variable and add dependent libraries to script's `LUA_PATH` and environment variable and add dependent libraries to script's `LUA_PATH` and
`LUA_CPATH`. `LUA_CPATH`.
It accepts as arguments:
* 'luarocksConfig': a nix value that directly maps to the luarocks config used during
the installation
By default `meta.platforms` is set to the same value as the interpreter unless overridden otherwise. By default `meta.platforms` is set to the same value as the interpreter unless overridden otherwise.
#### `buildLuaApplication` function {#buildluaapplication-function} #### `buildLuaApplication` function {#buildluaapplication-function}

View File

@ -51,9 +51,10 @@
# Appended to the generated luarocks config # Appended to the generated luarocks config
, extraConfig ? "" , extraConfig ? ""
# Inserted into the generated luarocks config in the "variables" table
, extraVariables ? {} # transparent mapping nix <-> lua used as LUAROCKS_CONFIG
# The two above arguments have access to builder variables -- e.g. to $out # Refer to https://github.com/luarocks/luarocks/wiki/Config-file-format for specs
, luarocksConfig ? {}
# relative to srcRoot, path to the rockspec to use when using rocks # relative to srcRoot, path to the rockspec to use when using rocks
, rockspecFilename ? null , rockspecFilename ? null
@ -92,7 +93,7 @@ let
luarocks luarocks
]; ];
inherit doCheck extraConfig extraVariables rockspecFilename knownRockspec externalDeps nativeCheckInputs; inherit doCheck extraConfig rockspecFilename knownRockspec externalDeps nativeCheckInputs;
buildInputs = let buildInputs = let
# example externalDeps': [ { name = "CRYPTO"; dep = pkgs.openssl; } ] # example externalDeps': [ { name = "CRYPTO"; dep = pkgs.openssl; } ]
@ -116,9 +117,18 @@ let
text = self.luarocks_content; text = self.luarocks_content;
}; };
luarocks_content = let luarocks_content =
externalDepsGenerated = lib.filter (drv: !drv ? luaModule) (lib.generators.toLua { asBindings = true; } self.luarocksConfig) +
(self.nativeBuildInputs ++ self.propagatedBuildInputs ++ self.buildInputs); ''
${self.extraConfig}
'';
# TODO make it the default variable
luarocksConfig = let
externalDepsGenerated = lib.filter (drv: !drv ? luaModule)
(self.nativeBuildInputs ++ self.propagatedBuildInputs ++ self.buildInputs);
generatedConfig = luaLib.generateLuarocksConfig { generatedConfig = luaLib.generateLuarocksConfig {
externalDeps = lib.unique (self.externalDeps ++ externalDepsGenerated); externalDeps = lib.unique (self.externalDeps ++ externalDepsGenerated);
# Filter out the lua derivation itself from the Lua module dependency # Filter out the lua derivation itself from the Lua module dependency
@ -126,13 +136,17 @@ let
# luaLib.hasLuaModule # luaLib.hasLuaModule
requiredLuaRocks = lib.filter luaLib.hasLuaModule requiredLuaRocks = lib.filter luaLib.hasLuaModule
(lua.pkgs.requiredLuaModules (self.nativeBuildInputs ++ self.propagatedBuildInputs)); (lua.pkgs.requiredLuaModules (self.nativeBuildInputs ++ self.propagatedBuildInputs));
inherit (self) extraVariables rocksSubdir; inherit (self) rocksSubdir;
}; };
in
'' luarocksConfig' = lib.recursiveUpdate luarocksConfig
${generatedConfig} (lib.optionalAttrs (attrs ? extraVariables) (lib.warn "extraVariables in buildLuarocksPackage is deprecated, use luarocksConfig instead"
${self.extraConfig} {
''; variables = attrs.extraVariables;
}))
;
in lib.recursiveUpdate generatedConfig luarocksConfig';
configurePhase = '' configurePhase = ''
runHook preConfigure runHook preConfigure

View File

@ -74,7 +74,11 @@ rec {
}; };
}); });
/* generate luarocks config
/* generate a luarocks config conforming to:
https://github.com/luarocks/luarocks/wiki/Config-file-format
The config lists folders where to find lua dependencies
Example: Example:
generateLuarocksConfig { generateLuarocksConfig {
@ -89,7 +93,6 @@ rec {
externalDeps ? [] externalDeps ? []
# a list of lua derivations # a list of lua derivations
, requiredLuaRocks ? [] , requiredLuaRocks ? []
, extraVariables ? {}
, rocksSubdir ? "rocks-subdir" , rocksSubdir ? "rocks-subdir"
, ... , ...
}@args: let }@args: let
@ -119,33 +122,37 @@ rec {
externalDepsDirs = map externalDepsDirs = map
(x: builtins.toString x) (x: builtins.toString x)
(lib.filter (lib.isDerivation) externalDeps); (lib.filter (lib.isDerivation) externalDeps);
in toLua { asBindings = true; } ({
local_cache = ""; generatedConfig = ({
# To prevent collisions when creating environments, we install the rock local_cache = "";
# files into per-package subdirectories
rocks_subdir = rocksSubdir; # To prevent collisions when creating environments, we install the rock
# first tree is the default target where new rocks are installed, # files into per-package subdirectories
# any other trees in the list are treated as additional sources of installed rocks for matching dependencies. rocks_subdir = rocksSubdir;
rocks_trees = (
[{name = "current"; root = "${placeholder "out"}"; rocks_dir = "current"; }] ++ # first tree is the default target where new rocks are installed,
rocksTrees # any other trees in the list are treated as additional sources of installed rocks for matching dependencies.
); rocks_trees = (
} // lib.optionalAttrs lua.pkgs.isLuaJIT { [{name = "current"; root = "${placeholder "out"}"; rocks_dir = "current"; }] ++
# Luajit provides some additional functionality built-in; this exposes rocksTrees
# that to luarock's dependency system );
rocks_provided = { } // lib.optionalAttrs lua.pkgs.isLuaJIT {
jit = "${lua.luaversion}-1"; # Luajit provides some additional functionality built-in; this exposes
ffi = "${lua.luaversion}-1"; # that to luarock's dependency system
luaffi = "${lua.luaversion}-1"; rocks_provided = {
bit = "${lua.luaversion}-1"; jit = "${lua.luaversion}-1";
}; ffi = "${lua.luaversion}-1";
} // { luaffi = "${lua.luaversion}-1";
# For single-output external dependencies bit = "${lua.luaversion}-1";
external_deps_dirs = externalDepsDirs; };
# Some needed machinery to handle multiple-output external dependencies, } // {
# as per https://github.com/luarocks/luarocks/issues/766 # For single-output external dependencies
variables = (depVariables // extraVariables); external_deps_dirs = externalDepsDirs;
} # Some needed machinery to handle multiple-output external dependencies,
// removeAttrs args [ "rocksSubdir" "extraVariables" "requiredLuaRocks" "externalDeps" ] # as per https://github.com/luarocks/luarocks/issues/766
); variables = depVariables;
}
// removeAttrs args [ "requiredLuaRocks" "externalDeps" ]
);
in generatedConfig;
} }

View File

@ -22,7 +22,7 @@ buildLuarocksPackage {
]; ];
knownRockspec = "lua/nfd-scm-1.rockspec"; knownRockspec = "lua/nfd-scm-1.rockspec";
extraVariables.LUA_LIBDIR = "${lua}/lib"; luarocksConfig.LUA_LIBDIR = "${lua}/lib";
nativeBuildInputs = [ pkg-config ]; nativeBuildInputs = [ pkg-config ];
buildInputs = lib.optionals stdenv.isDarwin [ AppKit ]; buildInputs = lib.optionals stdenv.isDarwin [ AppKit ];

View File

@ -164,7 +164,7 @@ with prev;
}); });
ldbus = prev.ldbus.overrideAttrs (oa: { ldbus = prev.ldbus.overrideAttrs (oa: {
extraVariables = { luarocksConfig.variables = {
DBUS_DIR = "${dbus.lib}"; DBUS_DIR = "${dbus.lib}";
DBUS_ARCH_INCDIR = "${dbus.lib}/lib/dbus-1.0/include"; DBUS_ARCH_INCDIR = "${dbus.lib}/lib/dbus-1.0/include";
DBUS_INCDIR = "${dbus.dev}/include/dbus-1.0"; DBUS_INCDIR = "${dbus.dev}/include/dbus-1.0";
@ -309,7 +309,7 @@ with prev;
}); });
luadbi-mysql = prev.luadbi-mysql.overrideAttrs (oa: { luadbi-mysql = prev.luadbi-mysql.overrideAttrs (oa: {
extraVariables = { luarocksConfig.variables = {
# Can't just be /include and /lib, unfortunately needs the trailing 'mysql' # Can't just be /include and /lib, unfortunately needs the trailing 'mysql'
MYSQL_INCDIR = "${libmysqlclient.dev}/include/mysql"; MYSQL_INCDIR = "${libmysqlclient.dev}/include/mysql";
MYSQL_LIBDIR = "${libmysqlclient}/lib/mysql"; MYSQL_LIBDIR = "${libmysqlclient}/lib/mysql";
@ -520,7 +520,7 @@ with prev;
buildInputs = [ libuv ]; buildInputs = [ libuv ];
# Use system libuv instead of building local and statically linking # Use system libuv instead of building local and statically linking
extraVariables = { luarocksConfig.variables = {
WITH_SHARED_LIBUV = "ON"; WITH_SHARED_LIBUV = "ON";
}; };
@ -573,6 +573,7 @@ with prev;
''; '';
}); });
# upstream broken, can't be generated, so moved out from the generated set
readline = final.callPackage({ buildLuarocksPackage, fetchurl, luaAtLeast, luaOlder, lua, luaposix }: readline = final.callPackage({ buildLuarocksPackage, fetchurl, luaAtLeast, luaOlder, lua, luaposix }:
buildLuarocksPackage ({ buildLuarocksPackage ({
pname = "readline"; pname = "readline";
@ -588,7 +589,7 @@ with prev;
sha256 = "1mk9algpsvyqwhnq7jlw4cgmfzj30l7n2r6ak4qxgdxgc39f48k4"; sha256 = "1mk9algpsvyqwhnq7jlw4cgmfzj30l7n2r6ak4qxgdxgc39f48k4";
}; };
extraVariables = rec { luarocksConfig.variables = rec {
READLINE_INCDIR = "${readline.dev}/include"; READLINE_INCDIR = "${readline.dev}/include";
HISTORY_INCDIR = READLINE_INCDIR; HISTORY_INCDIR = READLINE_INCDIR;
}; };
@ -597,7 +598,6 @@ with prev;
tar xf *.tar.gz tar xf *.tar.gz
''; '';
disabled = (luaOlder "5.1") || (luaAtLeast "5.5");
propagatedBuildInputs = [ lua luaposix propagatedBuildInputs = [ lua luaposix
readline.out readline.out
]; ];
@ -606,6 +606,7 @@ with prev;
homepage = "http://pjb.com.au/comp/lua/readline.html"; homepage = "http://pjb.com.au/comp/lua/readline.html";
description = "Interface to the readline library"; description = "Interface to the readline library";
license.fullName = "MIT/X11"; license.fullName = "MIT/X11";
broken = (luaOlder "5.1") || (luaAtLeast "5.5");
}; };
})) {}; })) {};