2021-09-11 22:01:49 +00:00
|
|
|
{ pkgs, lib, lua }:
|
|
|
|
let
|
2023-04-23 00:43:37 +00:00
|
|
|
inherit (lib.generators) toLua;
|
2021-09-11 22:01:49 +00:00
|
|
|
requiredLuaModules = drvs: with lib; let
|
|
|
|
modules = filter hasLuaModule drvs;
|
|
|
|
in unique ([lua] ++ modules ++ concatLists (catAttrs "requiredLuaModules" modules));
|
|
|
|
# Check whether a derivation provides a lua module.
|
|
|
|
hasLuaModule = drv: drv ? luaModule;
|
2021-10-03 22:10:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Use this to override the arguments passed to buildLuarocksPackage
|
|
|
|
*/
|
|
|
|
overrideLuarocks = drv: f: (drv.override (args: args // {
|
|
|
|
buildLuarocksPackage = drv: (args.buildLuarocksPackage drv).override f;
|
|
|
|
})) // {
|
|
|
|
overrideScope = scope: overrideLuarocks (drv.overrideScope scope) f;
|
|
|
|
};
|
|
|
|
|
2021-09-11 22:01:49 +00:00
|
|
|
in
|
|
|
|
rec {
|
2021-10-03 22:10:46 +00:00
|
|
|
inherit overrideLuarocks;
|
2021-09-11 22:01:49 +00:00
|
|
|
inherit hasLuaModule requiredLuaModules;
|
|
|
|
|
|
|
|
luaPathList = [
|
|
|
|
"share/lua/${lua.luaversion}/?.lua"
|
|
|
|
"share/lua/${lua.luaversion}/?/init.lua"
|
|
|
|
];
|
|
|
|
luaCPathList = [
|
|
|
|
"lib/lua/${lua.luaversion}/?.so"
|
|
|
|
];
|
|
|
|
|
|
|
|
/* generate paths without a prefix
|
|
|
|
*/
|
|
|
|
luaPathRelStr = lib.concatStringsSep ";" luaPathList;
|
|
|
|
luaCPathRelStr = lib.concatStringsSep ";" luaCPathList;
|
|
|
|
|
|
|
|
/* generate LUA_(C)PATH value for a specific derivation, i.e., with absolute paths
|
|
|
|
*/
|
|
|
|
genLuaPathAbsStr = drv: lib.concatMapStringsSep ";" (x: "${drv}/${x}") luaPathList;
|
|
|
|
genLuaCPathAbsStr = drv: lib.concatMapStringsSep ";" (x: "${drv}/${x}") luaCPathList;
|
|
|
|
|
|
|
|
/* Generate a LUA_PATH with absolute paths
|
|
|
|
*/
|
|
|
|
# genLuaPathAbs = drv:
|
|
|
|
# lib.concatStringsSep ";" (map (x: "${drv}/x") luaPathList);
|
|
|
|
|
|
|
|
luaAtLeast = lib.versionAtLeast lua.luaversion;
|
|
|
|
luaOlder = lib.versionOlder lua.luaversion;
|
|
|
|
isLua51 = (lib.versions.majorMinor lua.version) == "5.1";
|
|
|
|
isLua52 = (lib.versions.majorMinor lua.version) == "5.2";
|
|
|
|
isLua53 = lua.luaversion == "5.3";
|
|
|
|
isLuaJIT = lib.getName lua == "luajit";
|
|
|
|
|
|
|
|
/* generates the relative path towards the folder where
|
|
|
|
seems stable even when using lua_modules_path = ""
|
|
|
|
|
|
|
|
Example:
|
|
|
|
getDataFolder luaPackages.stdlib
|
|
|
|
=> stdlib-41.2.2-1-rocks/stdlib/41.2.2-1/doc
|
|
|
|
*/
|
|
|
|
getDataFolder = drv:
|
|
|
|
"${drv.pname}-${drv.version}-rocks/${drv.pname}/${drv.version}";
|
|
|
|
|
|
|
|
/* Convert derivation to a lua module.
|
|
|
|
so that luaRequireModules can be run later
|
|
|
|
*/
|
|
|
|
toLuaModule = drv:
|
2023-03-30 13:43:19 +00:00
|
|
|
drv.overrideAttrs(oldAttrs: {
|
2021-09-11 22:01:49 +00:00
|
|
|
# Use passthru in order to prevent rebuilds when possible.
|
2021-09-11 23:20:30 +00:00
|
|
|
passthru = (oldAttrs.passthru or {}) // {
|
2021-09-11 22:01:49 +00:00
|
|
|
luaModule = lua;
|
|
|
|
requiredLuaModules = requiredLuaModules drv.propagatedBuildInputs;
|
|
|
|
};
|
|
|
|
});
|
2021-09-18 19:56:32 +00:00
|
|
|
|
|
|
|
/* generate luarocks config
|
|
|
|
|
|
|
|
generateLuarocksConfig {
|
|
|
|
externalDeps = [ { name = "CRYPTO"; dep = pkgs.openssl; } ];
|
|
|
|
rocksSubdir = "subdir";
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
generateLuarocksConfig = {
|
2023-07-09 19:47:13 +00:00
|
|
|
externalDeps ? []
|
2022-03-29 01:48:20 +00:00
|
|
|
# a list of lua derivations
|
2023-07-09 19:47:13 +00:00
|
|
|
, requiredLuaRocks ? []
|
2021-09-18 19:56:32 +00:00
|
|
|
, extraVariables ? {}
|
2023-07-09 19:47:13 +00:00
|
|
|
, rocksSubdir ? "rocks-subdir"
|
2021-09-18 19:56:32 +00:00
|
|
|
}: let
|
|
|
|
rocksTrees = lib.imap0
|
2023-07-07 14:26:00 +00:00
|
|
|
(i: dep: {
|
|
|
|
name = "dep-${toString i}";
|
|
|
|
root = "${dep}";
|
|
|
|
# packages built by buildLuaPackage or luarocks doesn't contain rocksSubdir
|
|
|
|
# hence a default here
|
|
|
|
rocks_dir = if dep ? rocksSubdir then "${dep}/${dep.rocksSubdir}" else "${dep.pname}-${dep.version}-rocks";
|
|
|
|
})
|
2021-09-18 19:56:32 +00:00
|
|
|
requiredLuaRocks;
|
|
|
|
|
|
|
|
# Explicitly point luarocks to the relevant locations for multiple-output
|
|
|
|
# derivations that are external dependencies, to work around an issue it has
|
|
|
|
# (https://github.com/luarocks/luarocks/issues/766)
|
2023-04-23 00:43:37 +00:00
|
|
|
depVariables = zipAttrsWithLast (lib.lists.map ({name, dep}: {
|
|
|
|
"${name}_INCDIR" = "${lib.getDev dep}/include";
|
|
|
|
"${name}_LIBDIR" = "${lib.getLib dep}/lib";
|
|
|
|
"${name}_BINDIR" = "${lib.getBin dep}/bin";
|
|
|
|
}) externalDeps');
|
|
|
|
zipAttrsWithLast = lib.attrsets.zipAttrsWith (name: lib.lists.last);
|
2021-09-18 19:56:32 +00:00
|
|
|
|
|
|
|
# example externalDeps': [ { name = "CRYPTO"; dep = pkgs.openssl; } ]
|
|
|
|
externalDeps' = lib.filter (dep: !lib.isDerivation dep) externalDeps;
|
|
|
|
|
|
|
|
externalDepsDirs = map
|
2023-04-23 00:43:37 +00:00
|
|
|
(x: builtins.toString x)
|
2021-09-18 19:56:32 +00:00
|
|
|
(lib.filter (lib.isDerivation) externalDeps);
|
2023-04-29 16:37:42 +00:00
|
|
|
in toLua { asBindings = true; } ({
|
|
|
|
local_cache = "";
|
|
|
|
# To prevent collisions when creating environments, we install the rock
|
|
|
|
# files into per-package subdirectories
|
|
|
|
rocks_subdir = rocksSubdir;
|
|
|
|
# first tree is the default target where new rocks are installed,
|
|
|
|
# any other trees in the list are treated as additional sources of installed rocks for matching dependencies.
|
|
|
|
rocks_trees = (
|
2023-04-23 00:43:37 +00:00
|
|
|
[{name = "current"; root = "${placeholder "out"}"; rocks_dir = "current"; }] ++
|
|
|
|
rocksTrees
|
2023-04-29 16:37:42 +00:00
|
|
|
);
|
|
|
|
} // lib.optionalAttrs lua.pkgs.isLuaJIT {
|
|
|
|
# Luajit provides some additional functionality built-in; this exposes
|
|
|
|
# that to luarock's dependency system
|
2021-09-18 19:56:32 +00:00
|
|
|
rocks_provided = {
|
2023-04-23 00:43:37 +00:00
|
|
|
jit = "${lua.luaversion}-1";
|
|
|
|
ffi = "${lua.luaversion}-1";
|
|
|
|
luaffi = "${lua.luaversion}-1";
|
|
|
|
bit = "${lua.luaversion}-1";
|
2023-04-29 16:37:42 +00:00
|
|
|
};
|
|
|
|
} // {
|
|
|
|
# For single-output external dependencies
|
|
|
|
external_deps_dirs = externalDepsDirs;
|
|
|
|
# Some needed machinery to handle multiple-output external dependencies,
|
|
|
|
# as per https://github.com/luarocks/luarocks/issues/766
|
|
|
|
variables = (depVariables // extraVariables);
|
|
|
|
});
|
2021-09-11 22:01:49 +00:00
|
|
|
}
|