summaryrefslogtreecommitdiff
path: root/pkgs/development/lua-modules/lib.nix
blob: 7b6598f6ee21f26bcb4c4b0c0a553ac077b54cad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
{
  pkgs,
  lib,
  lua,
}:
let
  inherit (lib.generators) toLua;
  requiredLuaModules =
    drvs:
    let
      modules = lib.filter hasLuaModule drvs;
    in
    lib.unique ([ lua ] ++ modules ++ lib.concatLists (lib.catAttrs "requiredLuaModules" modules));
  # Check whether a derivation provides a lua module.
  hasLuaModule = drv: drv ? luaModule;

  # Use this to override the arguments passed to buildLuarocksPackage
  overrideLuarocks =
    drv: f:
    (drv.override (
      args:
      args
      // {
        buildLuarocksPackage = drv: (args.buildLuarocksPackage drv).override f;
      }
    ))
    // {
      overrideScope = scope: overrideLuarocks (drv.overrideScope scope) f;
    };

in
rec {
  inherit overrideLuarocks;
  inherit hasLuaModule requiredLuaModules;

  luaPathList = [
    "share/lua/${lua.luaversion}/?.lua"
    "share/lua/${lua.luaversion}/?/init.lua"
  ];
  luaCPathList = [
    "lib/lua/${lua.luaversion}/?.so"
  ];

  # generate paths without a prefix
  luaPathRelStr = lib.concatStringsSep ";" luaPathList;
  luaCPathRelStr = lib.concatStringsSep ";" luaCPathList;

  # generate LUA_(C)PATH value for a specific derivation, i.e., with absolute paths
  genLuaPathAbsStr = drv: lib.concatMapStringsSep ";" (x: "${drv}/${x}") luaPathList;
  genLuaCPathAbsStr = drv: lib.concatMapStringsSep ";" (x: "${drv}/${x}") luaCPathList;

  # Generate a LUA_PATH with absolute paths
  # genLuaPathAbs = drv:
  #   lib.concatStringsSep ";" (map (x: "${drv}/x") luaPathList);

  luaAtLeast = lib.versionAtLeast lua.luaversion;
  luaOlder = lib.versionOlder lua.luaversion;
  isLua51 = (lib.versions.majorMinor lua.version) == "5.1";
  isLua52 = (lib.versions.majorMinor lua.version) == "5.2";
  isLua53 = lua.luaversion == "5.3";
  isLuaJIT = lib.getName lua == "luajit";

  /*
    generates the relative path towards the folder where
    seems stable even when using  lua_modules_path = ""

    Example:
     getDataFolder luaPackages.stdlib
     => stdlib-41.2.2-1-rocks/stdlib/41.2.2-1/doc
  */
  getDataFolder = drv: "${drv.pname}-${drv.version}-rocks/${drv.pname}/${drv.version}";

  /*
    Convert derivation to a lua module.
    so that luaRequireModules can be run later
  */
  toLuaModule =
    drv:
    drv.overrideAttrs (oldAttrs: {
      # Use passthru in order to prevent rebuilds when possible.
      passthru = (oldAttrs.passthru or { }) // {
        luaModule = lua;
        requiredLuaModules = requiredLuaModules drv.propagatedBuildInputs;
      };
    });

  /*
    generate a luarocks config conforming to:
    https://github.com/luarocks/luarocks/wiki/Config-file-format

    The config lists folders where to find lua dependencies

    Example:
      generateLuarocksConfig {
        externalDeps = [ { name = "CRYPTO"; dep = pkgs.openssl; } ];
        rocksSubdir = "subdir";
      };

    Type:
       generateLuarocksConfig :: AttrSet -> String
  */
  generateLuarocksConfig =
    {
      externalDeps ? [ ],
      # a list of lua derivations
      requiredLuaRocks ? [ ],
      ...
    }@args:
    let
      rocksTrees = lib.imap0 (i: dep: {
        name = "dep-${toString i}";
        root = "${dep}";
        # packages built by buildLuaPackage or luarocks doesn't contain rocksSubdir
        # hence a default here
        rocks_dir =
          if dep ? rocksSubdir then "${dep}/${dep.rocksSubdir}" else "${dep.pname}-${dep.version}-rocks";
      }) requiredLuaRocks;

      # Explicitly point luarocks to the relevant locations for multiple-output
      # derivations that are external dependencies, to work around an issue it has
      # (https://github.com/luarocks/luarocks/issues/766)
      depVariables = zipAttrsWithLast (
        lib.lists.map (
          { name, dep }:
          {
            "${name}_INCDIR" = "${lib.getDev dep}/include";
            "${name}_LIBDIR" = "${lib.getLib dep}/lib";
            "${name}_BINDIR" = "${lib.getBin dep}/bin";
          }
        ) externalDeps'
      );
      zipAttrsWithLast = lib.attrsets.zipAttrsWith (name: lib.lists.last);

      # example externalDeps': [ { name = "CRYPTO"; dep = pkgs.openssl; } ]
      externalDeps' = lib.filter (dep: !lib.isDerivation dep) externalDeps;

      externalDepsDirs = map (x: toString x) (lib.filter (lib.isDerivation) externalDeps);

      generatedConfig = (
        {

          # first tree is the default target where new rocks are installed,
          # any other trees in the list are treated as additional sources of installed rocks for matching dependencies.
          rocks_trees = (
            [
              {
                name = "current";
                root = "${placeholder "out"}";
                rocks_dir = "current";
              }
            ]
            ++ rocksTrees
          );
        }
        // lib.optionalAttrs lua.pkgs.isLuaJIT {
          # Luajit provides some additional functionality built-in; this exposes
          # that to luarock's dependency system
          rocks_provided = {
            jit = "${lua.luaversion}-1";
            ffi = "${lua.luaversion}-1";
            luaffi = "${lua.luaversion}-1";
            bit = "${lua.luaversion}-1";
          };
        }
        // {
          # For single-output external dependencies
          external_deps_dirs = externalDepsDirs;
          # Some needed machinery to handle multiple-output external dependencies,
          # as per https://github.com/luarocks/luarocks/issues/766
          variables = depVariables;
        }
        // removeAttrs args [
          "requiredLuaRocks"
          "externalDeps"
        ]
      );
    in
    generatedConfig;
}