lua: smarter/more correct patching

In order to have the 'reset' LUA_PATH (aka `;;`) work, and for purity
reasons(removing /usr paths) we(I) decided to patch the lua interpreters default LUA_PATH.
Turns out the interpreters have different defaults and the patch was too
coarse.
There is smarter patching that can be done via LUA_ROOT.

Also luajit doesn't need patching at all since LUA_ROOT is set via the
build system.
This commit is contained in:
Matthieu Coudron 2024-03-06 22:01:08 +01:00
parent b00700a4e4
commit 640cdcb814
3 changed files with 14 additions and 28 deletions

View File

@ -8,7 +8,6 @@
let let
callPackage = lua.pkgs.callPackage; callPackage = lua.pkgs.callPackage;
luaInterpreter = lua.interpreter;
in { in {
lua-setup-hook = LuaPathSearchPaths: LuaCPathSearchPaths: lua-setup-hook = LuaPathSearchPaths: LuaCPathSearchPaths:

View File

@ -22,7 +22,7 @@ stdenv.mkDerivation (finalAttrs:
let let
luaPackages = self.pkgs; luaPackages = self.pkgs;
luaversion = lib.versions.majorMinor version; luaversion = lib.versions.majorMinor finalAttrs.version;
plat = if (stdenv.isLinux && lib.versionOlder self.luaversion "5.4") then "linux" plat = if (stdenv.isLinux && lib.versionOlder self.luaversion "5.4") then "linux"
else if (stdenv.isLinux && lib.versionAtLeast self.luaversion "5.4") then "linux-readline" else if (stdenv.isLinux && lib.versionAtLeast self.luaversion "5.4") then "linux-readline"
@ -45,7 +45,7 @@ stdenv.mkDerivation (finalAttrs:
outputs = [ "out" "doc" ]; outputs = [ "out" "doc" ];
src = fetchurl { src = fetchurl {
url = "https://www.lua.org/ftp/${finalAttrs.pname}-${finalAttrs.version}.tar.gz"; url = "https://www.lua.org/ftp/lua-${finalAttrs.version}.tar.gz";
sha256 = hash; sha256 = hash;
}; };
@ -60,16 +60,11 @@ stdenv.mkDerivation (finalAttrs:
inherit patches; inherit patches;
# we can't pass flags to the lua makefile because for portability, everything is hardcoded
postPatch = '' postPatch = ''
{ sed -i "s@#define LUA_ROOT[[:space:]]*\"/usr/local/\"@#define LUA_ROOT \"$out/\"@g" src/luaconf.h
echo -e '
#undef LUA_PATH_DEFAULT # abort if patching didn't work
#define LUA_PATH_DEFAULT "./share/lua/${luaversion}/?.lua;./?.lua;./?/init.lua" grep $out src/luaconf.h
#undef LUA_CPATH_DEFAULT
#define LUA_CPATH_DEFAULT "./lib/lua/${luaversion}/?.so;./?.so;./lib/lua/${luaversion}/loadall.so"
'
} >> src/luaconf.h
'' + lib.optionalString (!stdenv.isDarwin && !staticOnly) '' '' + lib.optionalString (!stdenv.isDarwin && !staticOnly) ''
# Add a target for a shared library to the Makefile. # Add a target for a shared library to the Makefile.
sed -e '1s/^/LUA_SO = liblua.so/' \ sed -e '1s/^/LUA_SO = liblua.so/' \
@ -102,8 +97,8 @@ stdenv.mkDerivation (finalAttrs:
makeFlagsArray+=(${lib.optionalString stdenv.isDarwin "CC=\"$CC\""}${lib.optionalString (stdenv.buildPlatform != stdenv.hostPlatform) " 'AR=${stdenv.cc.targetPrefix}ar rcu'"}) makeFlagsArray+=(${lib.optionalString stdenv.isDarwin "CC=\"$CC\""}${lib.optionalString (stdenv.buildPlatform != stdenv.hostPlatform) " 'AR=${stdenv.cc.targetPrefix}ar rcu'"})
installFlagsArray=( TO_BIN="lua luac" INSTALL_DATA='cp -d' \ installFlagsArray=( TO_BIN="lua luac" INSTALL_DATA='cp -d' \
TO_LIB="${if stdenv.isDarwin then "liblua.${version}.dylib" TO_LIB="${if stdenv.isDarwin then "liblua.${finalAttrs.version}.dylib"
else ("liblua.a" + lib.optionalString (!staticOnly) " liblua.so liblua.so.${luaversion} liblua.so.${version}" )}" ) else ("liblua.a" + lib.optionalString (!staticOnly) " liblua.so liblua.so.${luaversion} liblua.so.${finalAttrs.version}" )}" )
runHook postConfigure runHook postConfigure
''; '';
@ -128,7 +123,7 @@ stdenv.mkDerivation (finalAttrs:
Name: Lua Name: Lua
Description: An Extensible Extension Language Description: An Extensible Extension Language
Version: ${version} Version: ${finalAttrs.version}
Requires: Requires:
Libs: -L$out/lib -llua Libs: -L$out/lib -llua
Cflags: -I$out/include Cflags: -I$out/include
@ -138,7 +133,7 @@ stdenv.mkDerivation (finalAttrs:
ln -s "$out/lib/pkgconfig/lua.pc" "$out/lib/pkgconfig/lua${lib.replaceStrings [ "." ] [ "" ] luaversion}.pc" ln -s "$out/lib/pkgconfig/lua.pc" "$out/lib/pkgconfig/lua${lib.replaceStrings [ "." ] [ "" ] luaversion}.pc"
# Make documentation outputs of different versions co-installable. # Make documentation outputs of different versions co-installable.
mv $out/share/doc/lua $out/share/doc/lua-${version} mv $out/share/doc/lua $out/share/doc/lua-${finalAttrs.version}
''; '';
# copied from python # copied from python

View File

@ -62,7 +62,7 @@ let
else buildPackages.stdenv; else buildPackages.stdenv;
in in
stdenv.mkDerivation rec { stdenv.mkDerivation (finalAttrs: {
pname = "luajit"; pname = "luajit";
inherit version src; inherit version src;
@ -75,15 +75,6 @@ stdenv.mkDerivation rec {
# passed by nixpkgs CC wrapper is insufficient on its own # passed by nixpkgs CC wrapper is insufficient on its own
substituteInPlace src/Makefile --replace "#CCDEBUG= -g" "CCDEBUG= -g" substituteInPlace src/Makefile --replace "#CCDEBUG= -g" "CCDEBUG= -g"
fi fi
{
echo -e '
#undef LUA_PATH_DEFAULT
#define LUA_PATH_DEFAULT "./share/lua/${luaversion}/?.lua;./?.lua;./?/init.lua"
#undef LUA_CPATH_DEFAULT
#define LUA_CPATH_DEFAULT "./lib/lua/${luaversion}/?.so;./?.so;./lib/lua/${luaversion}/loadall.so"
'
} >> src/luaconf.h
''; '';
dontConfigure = true; dontConfigure = true;
@ -122,7 +113,8 @@ stdenv.mkDerivation rec {
inputs' = lib.filterAttrs (n: v: ! lib.isDerivation v && n != "passthruFun") inputs; inputs' = lib.filterAttrs (n: v: ! lib.isDerivation v && n != "passthruFun") inputs;
override = attr: let lua = attr.override (inputs' // { self = lua; }); in lua; override = attr: let lua = attr.override (inputs' // { self = lua; }); in lua;
in passthruFun rec { in passthruFun rec {
inherit self luaversion packageOverrides luaAttr; inherit self packageOverrides luaAttr;
inherit (finalAttrs) luaversion;
executable = "lua"; executable = "lua";
luaOnBuildForBuild = override pkgsBuildBuild.${luaAttr}; luaOnBuildForBuild = override pkgsBuildBuild.${luaAttr};
luaOnBuildForHost = override pkgsBuildHost.${luaAttr}; luaOnBuildForHost = override pkgsBuildHost.${luaAttr};
@ -142,4 +134,4 @@ stdenv.mkDerivation rec {
]; ];
maintainers = with maintainers; [ thoughtpolice smironov vcunat lblasc ]; maintainers = with maintainers; [ thoughtpolice smironov vcunat lblasc ];
} // extraMeta; } // extraMeta;
} })