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;
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 ? []
96 , rocksSubdir ? "rocks-subdir"
99 rocksTrees = lib.imap0
101 name = "dep-${toString i}";
103 # packages built by buildLuaPackage or luarocks doesn't contain rocksSubdir
104 # hence a default here
105 rocks_dir = if dep ? rocksSubdir then "${dep}/${dep.rocksSubdir}" else "${dep.pname}-${dep.version}-rocks";
109 # Explicitly point luarocks to the relevant locations for multiple-output
110 # derivations that are external dependencies, to work around an issue it has
111 # (https://github.com/luarocks/luarocks/issues/766)
112 depVariables = zipAttrsWithLast (lib.lists.map ({name, dep}: {
113 "${name}_INCDIR" = "${lib.getDev dep}/include";
114 "${name}_LIBDIR" = "${lib.getLib dep}/lib";
115 "${name}_BINDIR" = "${lib.getBin dep}/bin";
117 zipAttrsWithLast = lib.attrsets.zipAttrsWith (name: lib.lists.last);
119 # example externalDeps': [ { name = "CRYPTO"; dep = pkgs.openssl; } ]
120 externalDeps' = lib.filter (dep: !lib.isDerivation dep) externalDeps;
122 externalDepsDirs = map
123 (x: builtins.toString x)
124 (lib.filter (lib.isDerivation) externalDeps);
129 # To prevent collisions when creating environments, we install the rock
130 # files into per-package subdirectories
131 rocks_subdir = rocksSubdir;
133 # first tree is the default target where new rocks are installed,
134 # any other trees in the list are treated as additional sources of installed rocks for matching dependencies.
136 [{name = "current"; root = "${placeholder "out"}"; rocks_dir = "current"; }] ++
139 } // lib.optionalAttrs lua.pkgs.isLuaJIT {
140 # Luajit provides some additional functionality built-in; this exposes
141 # that to luarock's dependency system
143 jit = "${lua.luaversion}-1";
144 ffi = "${lua.luaversion}-1";
145 luaffi = "${lua.luaversion}-1";
146 bit = "${lua.luaversion}-1";
149 # For single-output external dependencies
150 external_deps_dirs = externalDepsDirs;
151 # Some needed machinery to handle multiple-output external dependencies,
152 # as per https://github.com/luarocks/luarocks/issues/766
153 variables = depVariables;
155 // removeAttrs args [ "requiredLuaRocks" "externalDeps" ]