3 inherit (lib.generators) toLua;
4 requiredLuaModules = drvs: let
5 modules = lib.filter hasLuaModule drvs;
6 in lib.unique ([lua] ++ modules ++ lib.concatLists (lib.catAttrs "requiredLuaModules" modules));
7 # Check whether a derivation provides a lua module.
8 hasLuaModule = drv: drv ? luaModule;
12 Use this to override the arguments passed to buildLuarocksPackage
14 overrideLuarocks = drv: f: (drv.override (args: args // {
15 buildLuarocksPackage = drv: (args.buildLuarocksPackage drv).override f;
17 overrideScope = scope: overrideLuarocks (drv.overrideScope scope) f;
22 inherit overrideLuarocks;
23 inherit hasLuaModule requiredLuaModules;
26 "share/lua/${lua.luaversion}/?.lua"
27 "share/lua/${lua.luaversion}/?/init.lua"
30 "lib/lua/${lua.luaversion}/?.so"
33 /* generate paths without a prefix
35 luaPathRelStr = lib.concatStringsSep ";" luaPathList;
36 luaCPathRelStr = lib.concatStringsSep ";" luaCPathList;
38 /* generate LUA_(C)PATH value for a specific derivation, i.e., with absolute paths
40 genLuaPathAbsStr = drv: lib.concatMapStringsSep ";" (x: "${drv}/${x}") luaPathList;
41 genLuaCPathAbsStr = drv: lib.concatMapStringsSep ";" (x: "${drv}/${x}") luaCPathList;
43 /* Generate a LUA_PATH with absolute paths
45 # genLuaPathAbs = drv:
46 # lib.concatStringsSep ";" (map (x: "${drv}/x") luaPathList);
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";
55 /* generates the relative path towards the folder where
56 seems stable even when using lua_modules_path = ""
59 getDataFolder luaPackages.stdlib
60 => stdlib-41.2.2-1-rocks/stdlib/41.2.2-1/doc
63 "${drv.pname}-${drv.version}-rocks/${drv.pname}/${drv.version}";
65 /* Convert derivation to a lua module.
66 so that luaRequireModules can be run later
69 drv.overrideAttrs(oldAttrs: {
70 # Use passthru in order to prevent rebuilds when possible.
71 passthru = (oldAttrs.passthru or {}) // {
73 requiredLuaModules = requiredLuaModules drv.propagatedBuildInputs;
78 /* generate a luarocks config conforming to:
79 https://github.com/luarocks/luarocks/wiki/Config-file-format
81 The config lists folders where to find lua dependencies
84 generateLuarocksConfig {
85 externalDeps = [ { name = "CRYPTO"; dep = pkgs.openssl; } ];
86 rocksSubdir = "subdir";
90 generateLuarocksConfig :: AttrSet -> String
92 generateLuarocksConfig = {
94 # a list of lua derivations
95 , requiredLuaRocks ? []
98 rocksTrees = lib.imap0
100 name = "dep-${toString i}";
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";
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";
116 zipAttrsWithLast = lib.attrsets.zipAttrsWith (name: lib.lists.last);
118 # example externalDeps': [ { name = "CRYPTO"; dep = pkgs.openssl; } ]
119 externalDeps' = lib.filter (dep: !lib.isDerivation dep) externalDeps;
121 externalDepsDirs = map
122 (x: builtins.toString x)
123 (lib.filter (lib.isDerivation) externalDeps);
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.
131 [{name = "current"; root = "${placeholder "out"}"; rocks_dir = "current"; }] ++
134 } // lib.optionalAttrs lua.pkgs.isLuaJIT {
135 # Luajit provides some additional functionality built-in; this exposes
136 # that to luarock's dependency system
138 jit = "${lua.luaversion}-1";
139 ffi = "${lua.luaversion}-1";
140 luaffi = "${lua.luaversion}-1";
141 bit = "${lua.luaversion}-1";
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;
150 // removeAttrs args [ "requiredLuaRocks" "externalDeps" ]