summary refs log tree commit diff
path: root/pkgs/development/lua-modules/generic/default.nix
blob: 3dae32b5e15d7fbd8fa2d24db6a71c37d4debfc9 (plain) (blame)
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
{ lua, writeText, toLuaModule }:

{ buildInputs ? [], disabled ? false, ... } @ attrs:

if disabled then
  throw "${attrs.name} not supported by interpreter lua-${lua.luaversion}"
else
  toLuaModule( lua.stdenv.mkDerivation (
    {
      makeFlags = [
        "PREFIX=$(out)"
        "LUA_LIBDIR=$(out)/lib/lua/${lua.luaversion}"
        "LUA_INC=-I${lua}/include"
      ];
    }
    //
    attrs
    //
    {
      name = "lua${lua.luaversion}-" + attrs.name;
      buildInputs = buildInputs ++ [ lua ];

      setupHook = writeText "setup-hook.sh" ''
        # check for lua/clua modules and don't add duplicates

        addLuaLibPath() {
          local package_path="$1/share/lua/${lua.luaversion}"
          if [[ ! -d $package_path ]]; then return; fi
          if [[ $LUA_PATH = *"$package_path"* ]]; then return; fi

          if [[ -z $LUA_PATH ]]; then
            export LUA_PATH="$package_path/?.lua;$package_path/?/init.lua"
          else
            export LUA_PATH="$LUA_PATH;$package_path/?.lua;$package_path/?/init.lua"
          fi
        }

        addLuaLibCPath() {
          local package_cpath="$1/lib/lua/${lua.luaversion}"
          if [[ ! -d $package_cpath ]]; then return; fi
          if [[ $LUA_CPATH = *"$package_cpath"* ]]; then return; fi

          if [[ -z $LUA_CPATH ]]; then
            export LUA_CPATH="$package_cpath/?.so"
          else
            export LUA_CPATH="$LUA_CPATH;$package_cpath/?.so"
          fi
        }

        addEnvHooks "$hostOffset" addLuaLibPath
        addEnvHooks "$hostOffset" addLuaLibCPath
      '';
    }
  ) )