Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at 23.05 98 lines 3.0 kB view raw
1# Inspired by python/wrapper.nix 2# Wrapper around wrapLuaProgramsIn, below. The $luaPath 3# variable is passed in from the buildLuarocksPackage function. 4set -e 5 6wrapLuaPrograms() { 7 wrapLuaProgramsIn "$out/bin" "$out $luaPath" 8} 9 10# Builds environment variables like LUA_PATH and PATH walking through closure 11# of dependencies. 12buildLuaPath() { 13 local luaPath="$1" 14 local path 15 16 # Create an empty table of paths (see doc on loadFromPropagatedInputs 17 # for how this is used). Build up the program_PATH and program_LUA_PATH 18 # variables. 19 declare -A luaPathsSeen=() 20 program_PATH= 21 luaPathsSeen["@lua@"]=1 22 addToSearchPath program_PATH @lua@/bin 23 for path in $luaPath; do 24 addToLuaPath "$path" 25 done 26} 27 28# with an executable shell script which will set some environment variables 29# and then call into the original binary (which has been given a .wrapped suffix). 30# luaPath is a list of directories 31wrapLuaProgramsIn() { 32 local dir="$1" 33 local luaPath="$2" 34 local f 35 36 buildLuaPath "$luaPath" 37 38 if [ ! -d "$dir" ]; then 39 nix_debug "$dir not a directory" 40 return 41 fi 42 43 nix_debug "wrapping programs in [$dir]" 44 45 # Find all regular files in the output directory that are executable. 46 find "$dir" -type f -perm -0100 -print0 | while read -d "" f; do 47 # Rewrite "#! .../env lua" to "#! /nix/store/.../lua". 48 # Strip suffix, like "3" or "2.7m" -- we don't have any choice on which 49 # Lua to use besides one with this hook anyway. 50 if head -n1 "$f" | grep -q '#!.*/env.*\(lua\)'; then 51 sed -i "$f" -e "1 s^.*/env[ ]*\(lua\)[^ ]*^#! @executable@^" 52 fi 53 54 # wrapProgram creates the executable shell script described 55 # above. The script will set LUA_(C)PATH and PATH variables! 56 # (see pkgs/build-support/setup-hooks/make-wrapper.sh) 57 local -a wrap_args=("$f" 58 --prefix PATH ':' "$program_PATH" 59 --prefix LUA_PATH ';' "$LUA_PATH" 60 --prefix LUA_CPATH ';' "$LUA_CPATH" 61 ) 62 63 # Add any additional arguments provided by makeWrapperArgs 64 # argument to buildLuaPackage. 65 # makeWrapperArgs 66 local -a user_args="($makeWrapperArgs)" 67 local -a wrapProgramArgs=("${wrap_args[@]}" "${user_args[@]}") 68 69 # see setup-hooks/make-wrapper.sh 70 wrapProgram "${wrapProgramArgs[@]}" 71 72 done 73} 74 75# Adds the lib and bin directories to the LUA_PATH and PATH variables, 76# respectively. Recurses on any paths declared in 77# `propagated-native-build-inputs`, while avoiding duplicating paths by 78# flagging the directories it has visited in `luaPathsSeen`. 79loadFromPropagatedInputs() { 80 local dir="$1" 81 # Stop if we've already visited here. 82 if [ -n "${luaPathsSeen[$dir]}" ]; then 83 return 84 fi 85 luaPathsSeen[$dir]=1 86 87 addToLuaPath "$dir" 88 addToSearchPath program_PATH $dir/bin 89 90 # Inspect the propagated inputs (if they exist) and recur on them. 91 local prop="$dir/nix-support/propagated-native-build-inputs" 92 if [ -e "$prop" ]; then 93 local new_path 94 for new_path in $(cat $prop); do 95 loadFromPropagatedInputs "$new_path" 96 done 97 fi 98}