Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{ pkgs, lib, lua }: 2let 3 requiredLuaModules = drvs: with lib; let 4 modules = filter hasLuaModule drvs; 5 in unique ([lua] ++ modules ++ concatLists (catAttrs "requiredLuaModules" modules)); 6 # Check whether a derivation provides a lua module. 7 hasLuaModule = drv: drv ? luaModule; 8 9 10 /* 11 Use this to override the arguments passed to buildLuarocksPackage 12 */ 13 overrideLuarocks = drv: f: (drv.override (args: args // { 14 buildLuarocksPackage = drv: (args.buildLuarocksPackage drv).override f; 15 })) // { 16 overrideScope = scope: overrideLuarocks (drv.overrideScope scope) f; 17 }; 18 19in 20rec { 21 inherit overrideLuarocks; 22 inherit hasLuaModule requiredLuaModules; 23 24 luaPathList = [ 25 "share/lua/${lua.luaversion}/?.lua" 26 "share/lua/${lua.luaversion}/?/init.lua" 27 ]; 28 luaCPathList = [ 29 "lib/lua/${lua.luaversion}/?.so" 30 ]; 31 32 /* generate paths without a prefix 33 */ 34 luaPathRelStr = lib.concatStringsSep ";" luaPathList; 35 luaCPathRelStr = lib.concatStringsSep ";" luaCPathList; 36 37 /* generate LUA_(C)PATH value for a specific derivation, i.e., with absolute paths 38 */ 39 genLuaPathAbsStr = drv: lib.concatMapStringsSep ";" (x: "${drv}/${x}") luaPathList; 40 genLuaCPathAbsStr = drv: lib.concatMapStringsSep ";" (x: "${drv}/${x}") luaCPathList; 41 42 /* Generate a LUA_PATH with absolute paths 43 */ 44 # genLuaPathAbs = drv: 45 # lib.concatStringsSep ";" (map (x: "${drv}/x") luaPathList); 46 47 luaAtLeast = lib.versionAtLeast lua.luaversion; 48 luaOlder = lib.versionOlder lua.luaversion; 49 isLua51 = (lib.versions.majorMinor lua.version) == "5.1"; 50 isLua52 = (lib.versions.majorMinor lua.version) == "5.2"; 51 isLua53 = lua.luaversion == "5.3"; 52 isLuaJIT = lib.getName lua == "luajit"; 53 54 /* generates the relative path towards the folder where 55 seems stable even when using lua_modules_path = "" 56 57 Example: 58 getDataFolder luaPackages.stdlib 59 => stdlib-41.2.2-1-rocks/stdlib/41.2.2-1/doc 60 */ 61 getDataFolder = drv: 62 "${drv.pname}-${drv.version}-rocks/${drv.pname}/${drv.version}"; 63 64 /* Convert derivation to a lua module. 65 so that luaRequireModules can be run later 66 */ 67 toLuaModule = drv: 68 drv.overrideAttrs( oldAttrs: { 69 # Use passthru in order to prevent rebuilds when possible. 70 passthru = (oldAttrs.passthru or {}) // { 71 luaModule = lua; 72 requiredLuaModules = requiredLuaModules drv.propagatedBuildInputs; 73 }; 74 }); 75 76 /* generate luarocks config 77 78 generateLuarocksConfig { 79 externalDeps = [ { name = "CRYPTO"; dep = pkgs.openssl; } ]; 80 rocksSubdir = "subdir"; 81 }; 82 */ 83 generateLuarocksConfig = { 84 externalDeps 85 86 # a list of lua derivations 87 , requiredLuaRocks 88 , extraVariables ? {} 89 , rocksSubdir 90 }: let 91 rocksTrees = lib.imap0 92 (i: dep: "{ name = [[dep-${toString i}]], root = '${dep}', rocks_dir = '${dep}/${dep.rocksSubdir}' }") 93 requiredLuaRocks; 94 95 # Explicitly point luarocks to the relevant locations for multiple-output 96 # derivations that are external dependencies, to work around an issue it has 97 # (https://github.com/luarocks/luarocks/issues/766) 98 depVariables = lib.concatMap ({name, dep}: [ 99 "${name}_INCDIR='${lib.getDev dep}/include';" 100 "${name}_LIBDIR='${lib.getLib dep}/lib';" 101 "${name}_BINDIR='${lib.getBin dep}/bin';" 102 ]) externalDeps'; 103 104 # example externalDeps': [ { name = "CRYPTO"; dep = pkgs.openssl; } ] 105 externalDeps' = lib.filter (dep: !lib.isDerivation dep) externalDeps; 106 107 externalDepsDirs = map 108 (x: "'${builtins.toString x}'") 109 (lib.filter (lib.isDerivation) externalDeps); 110 111 extraVariablesStr = lib.concatStringsSep "\n " 112 (lib.mapAttrsToList (k: v: "${k}='${v}';") extraVariables); 113 in '' 114 local_cache = "" 115 -- To prevent collisions when creating environments, we install the rock 116 -- files into per-package subdirectories 117 rocks_subdir = '${rocksSubdir}' 118 -- first tree is the default target where new rocks are installed, 119 -- any other trees in the list are treated as additional sources of installed rocks for matching dependencies. 120 rocks_trees = { 121 {name = "current", root = '${placeholder "out"}', rocks_dir = "current" }, 122 ${lib.concatStringsSep "\n, " rocksTrees} 123 } 124 '' + lib.optionalString lua.pkgs.isLuaJIT '' 125 -- Luajit provides some additional functionality built-in; this exposes 126 -- that to luarock's dependency system 127 rocks_provided = { 128 jit='${lua.luaversion}-1'; 129 ffi='${lua.luaversion}-1'; 130 luaffi='${lua.luaversion}-1'; 131 bit='${lua.luaversion}-1'; 132 } 133 '' + '' 134 -- For single-output external dependencies 135 external_deps_dirs = { 136 ${lib.concatStringsSep "\n, " externalDepsDirs} 137 } 138 variables = { 139 -- Some needed machinery to handle multiple-output external dependencies, 140 -- as per https://github.com/luarocks/luarocks/issues/766 141 ${lib.optionalString (lib.length depVariables > 0) '' 142 ${lib.concatStringsSep "\n " depVariables}''} 143 ${extraVariablesStr} 144 } 145 ''; 146}