about summary refs log tree commit diff
path: root/nix/buildBazelPackageNG/buildBazelPackageNG.nix
blob: 39c67adea797e7779065765f85a2486da10c860b (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{ stdenv
, lib
, pkgs
, coreutils
}:

{ name ? "${baseAttrs.pname}-${baseAttrs.version}"
, bazelTargets
, bazel ? pkgs.bazel
, depsHash
, extraCacheInstall ? ""
, extraBuildSetup ? ""
, extraBuildInstall ? ""
, ...
}@baseAttrs:

let
  cleanAttrs = lib.flip removeAttrs [
    "bazelTargets"
    "depsHash"
    "extraCacheInstall"
    "extraBuildSetup"
    "extraBuildInstall"
  ];
  attrs = cleanAttrs baseAttrs;

  base = stdenv.mkDerivation (attrs // {
    nativeBuildInputs = (attrs.nativeBuildInputs or [ ]) ++ [
      bazel
    ];

    preUnpack = ''
      if [[ ! -d $HOME ]]; then
        export HOME=$NIX_BUILD_TOP/home
        mkdir -p $HOME
      fi
    '';

    bazelTargetNames = builtins.attrNames bazelTargets;
  });

  cache = base.overrideAttrs (base: {
    name = "${name}-deps";

    bazelPhase = "cache";

    buildPhase = ''
      runHook preBuild

      bazel sync --repository_cache=repository-cache $bazelFlags "''${bazelFlagsArray[@]}"
      bazel build --repository_cache=repository-cache --nobuild $bazelFlags "''${bazelFlagsArray[@]}" $bazelTargetNames

      runHook postBuild
    '';

    installPhase = ''
      runHook preInstall

      mkdir $out
      echo "${bazel.version}" > $out/bazel_version
      cp -R repository-cache $out/repository-cache
      ${extraCacheInstall}

      runHook postInstall
    '';

    outputHashMode = "recursive";
    outputHash = depsHash;
  });

  build = base.overrideAttrs (base: {
    bazelPhase = "build";

    inherit cache;

    nativeBuildInputs = (base.nativeBuildInputs or [ ]) ++ [
      coreutils
    ];

    buildPhase = ''
      runHook preBuild

      ${extraBuildSetup}
      bazel build --repository_cache=$cache/repository-cache $bazelFlags "''${bazelFlagsArray[@]}" $bazelTargetNames

      runHook postBuild
    '';

    installPhase = ''
      runHook preInstall

      ${builtins.concatStringsSep "\n" (lib.mapAttrsToList (target: outPath: lib.optionalString (outPath != null) ''
        TARGET_OUTPUTS="$(bazel cquery --repository_cache=$cache/repository-cache $bazelFlags "''${bazelFlagsArray[@]}" --output=files "${target}")"
        if [[ "$(echo "$TARGET_OUTPUTS" | wc -l)" -gt 1 ]]; then
          echo "Installing ${target}'s outputs ($TARGET_OUTPUTS) into ${outPath} as a directory"
          mkdir -p "${outPath}"
          cp $TARGET_OUTPUTS "${outPath}"
        else
          echo "Installing ${target}'s output ($TARGET_OUTPUTS) to ${outPath}"
          mkdir -p "${dirOf outPath}"
          cp "$TARGET_OUTPUTS" "${outPath}"
        fi
      '') bazelTargets)}
      ${extraBuildInstall}

      runHook postInstall
    '';
  });
in
build