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