tvl-depot/nix/buildBazelPackageNG/buildBazelPackageNG.nix
Luke Granger-Brown c05bf02a85 chore(3p/gerrit): create buildBazelPackageNG and migrate gerrit to it
This bumps Gerrit to 3.10.0, and also introduces a new mechanism for
building it that should hopefully have some more stable hashes than the
previous bodgery.

In this world, we only cache what we explicitly want to. There are some
hooks implemented for `rules_java` and `rules_nodejs` (before version
6) that force use of local binaries; this means we can drop the use of
the FHSUserEnv and use the java and nodejs binaries provided by nixpkgs
instead.

detzip is deleted; it hasn't been used in yonks.

We also add https://gerrit-review.googlesource.com/c/gerrit/+/431977,
which bumps the SSHd version so that we can have U2F-based SSH keys.

Change-Id: Ie12a9a33bbb1e4bd96aa252580aca3b8bc4a1205
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11963
Reviewed-by: lukegb <lukegb@tvl.fyi>
Autosubmit: lukegb <lukegb@tvl.fyi>
Tested-by: BuildkiteCI
2024-07-08 00:17:56 +00:00

105 lines
2.6 KiB
Nix

{ 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