summary refs log tree commit diff
path: root/pkgs/development/interpreters/lua-5/wrapper.nix
blob: 9431522b71e5d06d4c0f7e4dfd3d873cd639966a (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{ lib, stdenv, lua, buildEnv, makeWrapper
, extraLibs ? []
, extraOutputsToInstall ? []
, postBuild ? ""
, ignoreCollisions ? false
, requiredLuaModules
, makeWrapperArgs ? []
}:

# Create a lua executable that knows about additional packages.
let
  env = let
    paths =  requiredLuaModules (extraLibs ++ [ lua ] );
  in buildEnv {
    name = "${lua.name}-env";

    inherit paths;
    inherit ignoreCollisions;
    extraOutputsToInstall = [ "out" ] ++ extraOutputsToInstall;

    nativeBuildInputs = [
      makeWrapper
      (lua.pkgs.lua-setup-hook lua.LuaPathSearchPaths lua.LuaCPathSearchPaths)
    ];

    # we create wrapper for the binaries in the different packages
    postBuild = ''
      if [ -L "$out/bin" ]; then
          unlink "$out/bin"
      fi
      mkdir -p "$out/bin"

      addToLuaPath "$out"

      # take every binary from lua packages and put them into the env
      for path in ${lib.concatStringsSep " " paths}; do
        nix_debug "looking for binaries in path = $path"
        if [ -d "$path/bin" ]; then
          cd "$path/bin"
          for prg in *; do
            if [ -f "$prg" ]; then
              rm -f "$out/bin/$prg"
              if [ -x "$prg" ]; then
                nix_debug "Making wrapper $prg"
                makeWrapper "$path/bin/$prg" "$out/bin/$prg" \
                  --set-default LUA_PATH ";;" \
                  --suffix LUA_PATH ';' "$LUA_PATH" \
                  --set-default LUA_CPATH ";;" \
                  --suffix LUA_CPATH ';' "$LUA_CPATH" \
                  ${lib.concatStringsSep " " makeWrapperArgs}
              fi
            fi
          done
        fi
      done
    '' + postBuild;

    inherit (lua) meta;

    passthru = lua.passthru // {
      interpreter = "${env}/bin/lua";
      inherit lua;
      luaPath = lua.pkgs.lib.genLuaPathAbsStr env;
      luaCpath = lua.pkgs.lib.genLuaCPathAbsStr env;
      env = stdenv.mkDerivation {
        name = "interactive-${lua.name}-environment";
        nativeBuildInputs = [ env ];

        buildCommand = ''
          echo >&2 ""
          echo >&2 "*** lua 'env' attributes are intended for interactive nix-shell sessions, not for building! ***"
          echo >&2 ""
          exit 1
        '';
    };
    };
  };
in env