Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow relative lua modules at runtime #273342

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkgs/development/interpreters/lua-5/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ let
inherit executable luaversion;
luaOnBuild = luaOnBuildForHost.override { inherit packageOverrides; self = luaOnBuild; };

tests = callPackage ./tests { inherit (luaPackages) wrapLua; };
tests = callPackage ./tests { lua = self; inherit (luaPackages) wrapLua; };

inherit luaAttr;
};
Expand Down
16 changes: 13 additions & 3 deletions pkgs/development/interpreters/lua-5/hooks/setup-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ addToLuaSearchPathWithCustomDelimiter() {
local topDir="${absPattern%%\?*}"

# export only if the folder exists else LUA_PATH/LUA_CPATH grow too large
if [[ ! -d "$topDir" ]]; then return; fi
if [[ ! -d "$topDir" ]] && [[ ! "$absPattern" =~ ^\./ ]]; then return; fi

# export only if we haven't already got this dir in the search path
if [[ ${!varName-} == *"$absPattern"* ]]; then return; fi
Expand All @@ -34,12 +34,22 @@ addToLuaPath() {
fi
cd "$dir"
for pattern in @luapathsearchpaths@; do
addToLuaSearchPathWithCustomDelimiter LUA_PATH "$PWD/$pattern"
# handle relative paths at runtime
if [[ "$pattern" =~ ^\./ ]]; then
addToLuaSearchPathWithCustomDelimiter LUA_PATH "$pattern"
else
addToLuaSearchPathWithCustomDelimiter LUA_PATH "$PWD/$pattern"
fi
done

# LUA_CPATH
for pattern in @luacpathsearchpaths@; do
addToLuaSearchPathWithCustomDelimiter LUA_CPATH "$PWD/$pattern"
# handle relative paths at runtime
if [[ "$pattern" =~ ^\./ ]]; then
addToLuaSearchPathWithCustomDelimiter LUA_CPATH "$pattern"
else
addToLuaSearchPathWithCustomDelimiter LUA_CPATH "$PWD/$pattern"
fi
done
cd - >/dev/null
}
Expand Down
15 changes: 3 additions & 12 deletions pkgs/development/interpreters/lua-5/interpreter.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ stdenv.mkDerivation (finalAttrs:
sha256 = hash;
};

LuaPathSearchPaths = luaPackages.luaLib.luaPathList;
LuaCPathSearchPaths = luaPackages.luaLib.luaCPathList;
LuaPathSearchPaths = [ "./share/lua/${self.luaversion}/?.lua" "./?.lua" "./?/init.lua" ] ++ luaPackages.luaLib.luaPathList;
LuaCPathSearchPaths = [ "./lib/lua/${self.luaversion}/?.so" "./?.so" "./lib/lua/${self.luaversion}/loadall.so" ] ++ luaPackages.luaLib.luaCPathList;
setupHook = luaPackages.lua-setup-hook
finalAttrs.LuaPathSearchPaths
finalAttrs.LuaCPathSearchPaths;
Expand All @@ -60,16 +60,7 @@ stdenv.mkDerivation (finalAttrs:
inherit patches;

# we can't pass flags to the lua makefile because for portability, everything is hardcoded
postPatch = ''
{
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
'' + lib.optionalString (!stdenv.isDarwin && !staticOnly) ''
postPatch = lib.optionalString (!stdenv.isDarwin && !staticOnly) ''
# Add a target for a shared library to the Makefile.
sed -e '1s/^/LUA_SO = liblua.so/' \
-e 's/ALL_T *= */&$(LUA_SO) /' \
Expand Down
11 changes: 8 additions & 3 deletions pkgs/development/interpreters/lua-5/tests/assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ function fail() {
}


function assertStringEqual {

function assertStringEqual() {
if ! diff <(echo "$1") <(echo "$2") ; then
fail "Strings differ"
fail "expected \"$1\" to be equal to \"$2\""
fi
}

function assertStringContains() {
if ! echo "$1" | grep -q "$2" ; then
fail "expected \"$1\" to contain \"$2\""
fi
}
25 changes: 22 additions & 3 deletions pkgs/development/interpreters/lua-5/tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
let

runTest = lua: { name, command }:
pkgs.runCommandLocal "test-${lua.name}" ({
pkgs.runCommandLocal "test-${lua.name}-${name}" ({
nativeBuildInputs = [lua];
meta.platforms = lua.meta.platforms;
}) (''
Expand All @@ -27,6 +27,10 @@ let
wrapLuaPrograms
'';
});

luaWithModule = lua.withPackages(ps: [
ps.lua-cjson
]);
in
pkgs.recurseIntoAttrs ({

Expand All @@ -36,15 +40,30 @@ in
generated=$(lua -e 'print(package.path)')
golden_LUA_PATH='./share/lua/${lua.luaversion}/?.lua;./?.lua;./?/init.lua'

assertStringEqual "$generated" "$golden_LUA_PATH"
assertStringContains "$generated" "$golden_LUA_PATH"
Copy link
Contributor Author

@rrrnld rrrnld Dec 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was necessary because of this:

$ nix log /nix/store/irzzqd4j56jn4j6n91r7qd7jmknbgnwj-test-luajit-2.1.1693350652-check-aliases.drv
1c1
< ./share/lua/5.1/?.lua;./?.lua;./?/init.lua;/nix/store/n99wdp44qbkzkgs4q5c7nkgqzfcfdl6c-luajit-2.1.1693350652/share/lua/5.1/?.lua;/nix/store/n99wdp44qbkzkgs4q5c7nkgqzfcfdl6c-luajit-2.1.1693350652/share/lua/5.1/?/init.lua
---
> ./share/lua/5.1/?.lua;./?.lua;./?/init.lua
expected "./share/lua/5.1/?.lua;./?.lua;./?/init.lua;/nix/store/n99wdp44qbkzkgs4q5c7nkgqzfcfdl6c-luajit-2.1.1693350652/share/lua/5.1/?.lua;/nix/store/n99wdp44qbkzkgs4q5c7nkgqzfcfdl6c-luajit-2.1.1693350652/share/lua/5.1/?/init.lua" to be equal to "./share/lua/5.1/?.lua;./?.lua;./?/init.lua"

this surfaced after 68dcfd5, before which the same lua 5.2 interpreter was used for all tests.

i'm not sure if this i an intentional difference between lua / luajit.

'';
};

checkWrapping = pkgs.runCommandLocal "test-${lua.name}" ({
checkWrapping = pkgs.runCommandLocal "test-${lua.name}-wrapping" ({
}) (''
grep -- 'LUA_PATH=' ${wrappedHello}/bin/hello
touch $out
'');

checkRelativeImports = pkgs.runCommandLocal "test-${lua.name}-relative-imports" ({
}) (''
source ${./assert.sh}

lua_vanilla_package_path="$(${lua}/bin/lua -e "print(package.path)")"
lua_with_module_package_path="$(${luaWithModule}/bin/lua -e "print(package.path)")"

assertStringContains "$lua_vanilla_package_path" "./?.lua"
assertStringContains "$lua_vanilla_package_path" "./?/init.lua"

assertStringContains "$lua_with_module_package_path" "./?.lua"
assertStringContains "$lua_with_module_package_path" "./?/init.lua"

touch $out
'');
})

15 changes: 3 additions & 12 deletions pkgs/development/interpreters/luajit/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,6 @@ stdenv.mkDerivation rec {
# passed by nixpkgs CC wrapper is insufficient on its own
substituteInPlace src/Makefile --replace "#CCDEBUG= -g" "CCDEBUG= -g"
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;
Expand Down Expand Up @@ -111,10 +102,10 @@ stdenv.mkDerivation rec {
fi
'';

LuaPathSearchPaths = luaPackages.luaLib.luaPathList;
LuaCPathSearchPaths = luaPackages.luaLib.luaCPathList;
LuaPathSearchPaths = [ "./share/lua/${luaversion}/?.lua" "./?.lua" "./?/init.lua" ] ++ luaPackages.luaLib.luaPathList;
LuaCPathSearchPaths = [ "./lib/lua/${luaversion}/?.so" "./?.so" "./lib/lua/${luaversion}/loadall.so" ] ++ luaPackages.luaLib.luaCPathList;

setupHook = luaPackages.lua-setup-hook luaPackages.luaLib.luaPathList luaPackages.luaLib.luaCPathList;
setupHook = luaPackages.lua-setup-hook LuaPathSearchPaths LuaCPathSearchPaths;

# copied from python
passthru = let
Expand Down