lua: take into propagated-build-inputs when building LUA_PATH

so far we ignored propagated-build-inputs
This commit is contained in:
Matthieu C. 2024-06-12 15:31:04 +02:00
parent 7e1ae5e8bb
commit 665f3f694b
6 changed files with 99 additions and 49 deletions

View File

@ -54,7 +54,7 @@ stdenv.mkDerivation (finalAttrs:
LuaCPathSearchPaths = luaPackages.luaLib.luaCPathList;
setupHook = builtins.toFile "lua-setup-hook" ''
source @out@/nix-support/utils.sh
addEnvHooks "$hostOffset" addToLuaPath
addEnvHooks "$hostOffset" luaEnvHook
'';
nativeBuildInputs = [ makeWrapper ];

View File

@ -96,4 +96,17 @@ in
touch $out
'');
/*
Check that a lua package's propagatedBuildInputs end up in LUA_PATH
*/
checkPropagatedBuildInputs = pkgs.runCommandLocal "test-${lua.name}-setup-hook" ({
# lua-curl is a propagatedBuildInput of rest-nvim has
buildInputs = [ lua.pkgs.rest-nvim ];
}) (''
${lua}/bin/lua -e "require'cURL'"
touch $out
'');
})

View File

@ -1,4 +1,8 @@
#!/bin/sh
#!/bin/bash
declare -gA luaPathsSeen=()
# shellcheck disable=SC2164,SC2041
nix_print() {
if [ ${NIX_DEBUG:-0} -ge $1 ]; then
echo "$2"
@ -33,10 +37,16 @@ addToLuaSearchPathWithCustomDelimiter() {
shopt -u globstar
}
# used in setup Hooks to load LUA_PATH and LUA_CPATH
# luaEnvHook
luaEnvHook() {
_addToLuaPath "$1"
}
addToLuaPath() {
local dir="$1"
if [[ ! -d "$dir" ]]; then
if [ ! -d "$dir" ]; then
nix_debug "$dir not a directory abort"
return 0
fi
@ -52,3 +62,74 @@ addToLuaPath() {
cd - >/dev/null
}
_addToLuaPath() {
local dir="$1"
echo "_addToLuaPath called for dir $dir"
if [[ ! -d "$dir" ]]; then
nix_debug "$dir not a directory abort"
return 0
fi
# set -x
# if [ -n "${pythonPathsSeen[$dir]}" ]; then return; fi
if [[ -n "${luaPathsSeen[$dir]:-}" ]]; then
# if [ -n "${luaPathsSeen[$dir]}" ]; then
echo "$dir already parsed"
return
fi
luaPathsSeen["$dir"]=true
# shellcheck disable=SC2164
cd "$dir"
for pattern in @luapathsearchpaths@; do
addToLuaSearchPathWithCustomDelimiter LUA_PATH "$PWD/$pattern"
done
# LUA_CPATH
for pattern in @luacpathsearchpaths@; do
addToLuaSearchPathWithCustomDelimiter LUA_CPATH "$PWD/$pattern"
done
cd - >/dev/null
addToSearchPath program_PATH "$dir"/bin
# Inspect the propagated inputs (if they exist) and recur on them.
local prop="$dir/nix-support/propagated-build-inputs"
if [ -e "$prop" ]; then
local new_path
for new_path in $(cat $prop); do
echo "newpath: $new_path"
_addToLuaPath "$new_path"
done
fi
}
# Builds environment variables like LUA_PATH and PATH walking through closure
# of dependencies.
buildLuaPath() {
local luaPath="$1"
local path
echo "BUILD_LUA_PATH"
# # set -x
# # Create an empty table of paths (see doc on loadFromPropagatedInputs
# # for how this is used). Build up the program_PATH and program_LUA_PATH
# # variables.
# declare -gA luaPathsSeen=()
# # shellcheck disable=SC2034
program_PATH=
luaPathsSeen["@lua@"]=1
# addToSearchPath program_PATH @lua@/bin
for path in $luaPath; do
_addToLuaPath "$path"
done
}

View File

@ -9,24 +9,6 @@ wrapLuaPrograms() {
wrapLuaProgramsIn "$out/bin" "$out $luaPath"
}
# Builds environment variables like LUA_PATH and PATH walking through closure
# of dependencies.
buildLuaPath() {
local luaPath="$1"
local path
# Create an empty table of paths (see doc on loadFromPropagatedInputs
# for how this is used). Build up the program_PATH and program_LUA_PATH
# variables.
declare -A luaPathsSeen=()
program_PATH=
luaPathsSeen["@lua@"]=1
addToSearchPath program_PATH @lua@/bin
for path in $luaPath; do
addToLuaPath "$path"
done
}
# with an executable shell script which will set some environment variables
# and then call into the original binary (which has been given a .wrapped suffix).
# luaPath is a list of directories
@ -47,7 +29,6 @@ wrapLuaProgramsIn() {
# Find all regular files in the output directory that are executable.
find "$dir" -type f -perm -0100 -print0 | while read -d "" f; do
# Rewrite "#! .../env lua" to "#! /nix/store/.../lua".
# Strip suffix, like "3" or "2.7m" -- we don't have any choice on which
# Lua to use besides one with this hook anyway.
if head -n1 "$f" | grep -q '#!.*/env.*\(lua\)'; then
sed -i "$f" -e "1 s^.*/env[ ]*\(lua\)[^ ]*^#! @executable@^"
@ -73,28 +54,3 @@ wrapLuaProgramsIn() {
done
}
# Adds the lib and bin directories to the LUA_PATH and PATH variables,
# respectively. Recurses on any paths declared in
# `propagated-native-build-inputs`, while avoiding duplicating paths by
# flagging the directories it has visited in `luaPathsSeen`.
loadFromPropagatedInputs() {
local dir="$1"
# Stop if we've already visited here.
if [ -n "${luaPathsSeen[$dir]}" ]; then
return
fi
luaPathsSeen[$dir]=1
addToLuaPath "$dir"
addToSearchPath program_PATH $dir/bin
# Inspect the propagated inputs (if they exist) and recur on them.
local prop="$dir/nix-support/propagated-native-build-inputs"
if [ -e "$prop" ]; then
local new_path
for new_path in $(cat $prop); do
loadFromPropagatedInputs "$new_path"
done
fi
}

View File

@ -30,7 +30,7 @@ let
fi
mkdir -p "$out/bin"
addToLuaPath "$out"
buildLuaPath "$out"
# take every binary from lua packages and put them into the env
for path in ${lib.concatStringsSep " " paths}; do

View File

@ -114,7 +114,7 @@ stdenv.mkDerivation (finalAttrs: {
setupHook = builtins.toFile "lua-setup-hook" ''
source @out@/nix-support/utils.sh
addEnvHooks "$hostOffset" addToLuaPath
addEnvHooks "$hostOffset" luaEnvHook
'';
# copied from python