about summary refs log tree commit diff
path: root/pkgs/games/build-support/build-game.nix
blob: e402787cb4e8de6137109a22d39fd3da947a0270 (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
79
80
81
82
83
84
85
86
87
88
89
{ stdenv, lib, file, unzip, gcc, makeSetupHook

, withPulseAudio ? true, libpulseaudio ? null
, alsaLib
}:

assert withPulseAudio -> libpulseaudio != null;

{ buildInputs ? []
, nativeBuildInputs ? []
, preUnpack ? ""
, setSourceRoot ? ""
, installCheckPhase ? ""
, runtimeDependencies ? []
, extraSandboxPaths ? [ "$XDG_DATA_HOME" "$XDG_CONFIG_HOME" ]
, ...
}@attrs:

let
  sandboxHook = makeSetupHook {
    substitutions = {
      inherit gcc;
      sandbox_main = ./sandbox.c;
    };
  } ./setup-hooks/make-sandbox.sh;

in stdenv.mkDerivation ({
  buildInputs = [ stdenv.cc.cc ] ++ buildInputs;

  nativeBuildInputs = [
    unzip file ./setup-hooks/auto-patchelf.sh
  ] ++ nativeBuildInputs;

  preUnpack = preUnpack + ''
    mkdir "$name"
    pushd "$name" &> /dev/null
  '';

  # Try to evade tarbombs
  setSourceRoot = ''
    popd &> /dev/null
  '' + lib.optionalString (setSourceRoot == "") ''
    unpackedFiles="$(find "$name" -mindepth 1 -maxdepth 1 -print)"
    if [ $(echo "$unpackedFiles" | wc -l) -gt 1 ]; then
      sourceRoot="$name"
    else
      sourceRoot="$name/''${unpackedFiles##*/}"
    fi
  '';

  # Use ":!*!:" as delimiter as we can consider this highly unlikely to
  # be part of a real path component and we're out of Nix territory, so
  # the path components could contain almost anything.
  extraSandboxPaths = lib.concatStringsSep ":!*!:" extraSandboxPaths;

  runtimeDependencies = let
    deps = lib.singleton alsaLib
        ++ lib.optional withPulseAudio libpulseaudio
        ++ runtimeDependencies;
  in map (dep: dep.lib or dep) deps;

  doInstallCheck = true;

  installCheckPhase = ''
    runHook preInstallCheck

    echo "checking dependencies for libraries and executables" >&2

    local errors="$(
        IFS=$'\n'
        for elf in $(findElfs "$prefix"); do checkElfDep "$elf"; done
    )"

    if [ -n "$errors" ]; then
        echo "$errors" >&2
        exit 1
    fi

    ${installCheckPhase}

    runHook postInstallCheck
  '';

  dontStrip = true;
  dontPatchELF = true;
} // removeAttrs attrs [
  "buildInputs" "nativeBuildInputs" "preUnpack" "setSourceRoot"
  "installCheckPhase" "runtimeDependencies" "extraSandboxPaths"
])