lua: create a folder for hooks

- moved lua hooks to a specific folder as I foresee to add more
- moved generateLuarocksConfig to lib
- fix getLuaPath
- removed the useless rockspecDir
This commit is contained in:
Matthieu Coudron 2021-09-18 21:56:32 +02:00 committed by Matthieu Coudron
parent 99a1d8ef57
commit abc36451d7
6 changed files with 112 additions and 77 deletions

View file

@ -82,66 +82,25 @@ let
# configured trees)
luarocks_config = "luarocks-config.lua";
luarocks_content = let
extraVariablesStr = lib.concatStringsSep "\n "
(lib.mapAttrsToList (k: v: "${k}='${v}';") extraVariables);
in ''
local_cache = ""
-- To prevent collisions when creating environments, we install the rock
-- files into per-package subdirectories
rocks_subdir = '${rocksSubdir}'
-- Then we need to tell luarocks where to find the rock files per
-- dependency
rocks_trees = {
${lib.concatStringsSep "\n, " rocksTrees}
}
'' + lib.optionalString lua.pkgs.isLuaJIT ''
-- Luajit provides some additional functionality built-in; this exposes
-- that to luarock's dependency system
rocks_provided = {
jit='${lua.luaversion}-1';
ffi='${lua.luaversion}-1';
luaffi='${lua.luaversion}-1';
bit='${lua.luaversion}-1';
}
'' + ''
-- For single-output external dependencies
external_deps_dirs = {
${lib.concatStringsSep "\n, " externalDepsDirs}
}
variables = {
-- Some needed machinery to handle multiple-output external dependencies,
-- as per https://github.com/luarocks/luarocks/issues/766
${lib.optionalString (lib.length depVariables > 0) ''
${lib.concatStringsSep "\n " depVariables}''}
${extraVariablesStr}
}
${extraConfig}
'';
generatedConfig = lua.pkgs.lib.generateLuarocksConfig {
inherit externalDeps;
inherit extraVariables;
inherit rocksSubdir;
inherit requiredLuaRocks;
};
in
''
${generatedConfig}
${extraConfig}
'';
rocksSubdir = "${attrs.pname}-${version}-rocks";
externalDepsDirs = map
(x: "'${builtins.toString x}'")
(lib.filter (lib.isDerivation) externalDeps);
rocksTrees = lib.imap0
(i: dep: "{ name = [[dep-${toString i}]], root = '${dep}', rocks_dir = '${dep}/${dep.rocksSubdir}' }")
requiredLuaRocks;
# Filter out the lua derivation itself from the Lua module dependency
# closure, as it doesn't have a rock tree :)
requiredLuaRocks = lib.filter (d: d ? luaModule)
(lua.pkgs.requiredLuaModules propagatedBuildInputs);
# 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)
depVariables = lib.concatMap ({name, dep}: [
"${name}_INCDIR='${lib.getDev dep}/include';"
"${name}_LIBDIR='${lib.getLib dep}/lib';"
"${name}_BINDIR='${lib.getBin dep}/bin';"
]) externalDeps';
# example externalDeps': [ { name = "CRYPTO"; dep = pkgs.openssl; } ]
externalDeps' = lib.filter (dep: !lib.isDerivation dep) externalDeps;
in

View file

@ -0,0 +1,27 @@
# Hooks for building lua packages.
{ lua
, lib
, makeSetupHook
, findutils
, runCommand
}:
let
callPackage = lua.pkgs.callPackage;
luaInterpreter = lua.interpreter;
in {
lua-setup-hook = LuaPathSearchPaths: LuaCPathSearchPaths:
let
hook = ./setup-hook.sh;
in runCommand "lua-setup-hook.sh" {
# hum doesn't seem to like caps !! BUG ?
luapathsearchpaths=lib.escapeShellArgs LuaPathSearchPaths;
luacpathsearchpaths=lib.escapeShellArgs LuaCPathSearchPaths;
} ''
cp ${hook} hook.sh
substituteAllInPlace hook.sh
mv hook.sh $out
'';
}

View file

@ -1,15 +0,0 @@
{ runCommand, lib, }:
LuaPathSearchPaths: LuaCPathSearchPaths:
let
hook = ./setup-hook.sh;
in runCommand "lua-setup-hook.sh" {
# hum doesn't seem to like caps !! BUG ?
luapathsearchpaths=lib.escapeShellArgs LuaPathSearchPaths;
luacpathsearchpaths=lib.escapeShellArgs LuaCPathSearchPaths;
} ''
cp ${hook} hook.sh
substituteAllInPlace hook.sh
mv hook.sh $out
''

View file

@ -60,4 +60,72 @@ rec {
requiredLuaModules = requiredLuaModules drv.propagatedBuildInputs;
};
});
/* generate luarocks config
generateLuarocksConfig {
externalDeps = [ { name = "CRYPTO"; dep = pkgs.openssl; } ];
rocksSubdir = "subdir";
};
*/
generateLuarocksConfig = {
externalDeps
, requiredLuaRocks
, extraVariables ? {}
, rocksSubdir
}: let
rocksTrees = lib.imap0
(i: dep: "{ name = [[dep-${toString i}]], root = '${dep}', rocks_dir = '${dep}/${dep.rocksSubdir}' }")
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)
depVariables = lib.concatMap ({name, dep}: [
"${name}_INCDIR='${lib.getDev dep}/include';"
"${name}_LIBDIR='${lib.getLib dep}/lib';"
"${name}_BINDIR='${lib.getBin dep}/bin';"
]) externalDeps';
# example externalDeps': [ { name = "CRYPTO"; dep = pkgs.openssl; } ]
externalDeps' = lib.filter (dep: !lib.isDerivation dep) externalDeps;
externalDepsDirs = map
(x: "'${builtins.toString x}'")
(lib.filter (lib.isDerivation) externalDeps);
extraVariablesStr = lib.concatStringsSep "\n "
(lib.mapAttrsToList (k: v: "${k}='${v}';") extraVariables);
in ''
local_cache = ""
-- To prevent collisions when creating environments, we install the rock
-- files into per-package subdirectories
rocks_subdir = '${rocksSubdir}'
-- Then we need to tell luarocks where to find the rock files per
-- dependency
rocks_trees = {
${lib.concatStringsSep "\n, " rocksTrees}
}
'' + lib.optionalString lua.pkgs.isLuaJIT ''
-- Luajit provides some additional functionality built-in; this exposes
-- that to luarock's dependency system
rocks_provided = {
jit='${lua.luaversion}-1';
ffi='${lua.luaversion}-1';
luaffi='${lua.luaversion}-1';
bit='${lua.luaversion}-1';
}
'' + ''
-- For single-output external dependencies
external_deps_dirs = {
${lib.concatStringsSep "\n, " externalDepsDirs}
}
variables = {
-- Some needed machinery to handle multiple-output external dependencies,
-- as per https://github.com/luarocks/luarocks/issues/766
${lib.optionalString (lib.length depVariables > 0) ''
${lib.concatStringsSep "\n " depVariables}''}
${extraVariablesStr}
}
'';
}

View file

@ -12,18 +12,12 @@
, fetchFromGitHub, which, writeText
, pkgs
, lib
}:
}@args:
let
packages = ( self:
let
# a function of lua_path / lua_cpath
lua-setup-hook = callPackage ../development/interpreters/lua-5/setup-hook.nix {
inherit lib;
};
callPackage = pkgs.newScope self;
buildLuaApplication = args: buildLuarocksPackage ({namePrefix="";} // args );
@ -52,11 +46,13 @@ in
# helper functions for dealing with LUA_PATH and LUA_CPATH
lib = luaLib;
getLuaPath = drv: luaLib.getPath drv (luaLib.luaPathList lua.luaversion) ;
getLuaCPath = drv: luaLib.getPath drv (luaLib.luaCPathList lua.luaversion) ;
getLuaPath = drv: getPath drv (luaLib.luaPathList lua.luaversion) ;
getLuaCPath = drv: getPath drv (luaLib.luaCPathList lua.luaversion) ;
inherit (callPackage ../development/interpreters/lua-5/hooks { inherit (args) lib;})
lua-setup-hook;
inherit lua lua-setup-hook callPackage;
inherit lua callPackage;
inherit buildLuaPackage buildLuarocksPackage buildLuaApplication;
inherit (luaLib) luaOlder luaAtLeast isLua51 isLua52 isLua53 isLuaJIT
requiredLuaModules toLuaModule hasLuaModule;