From 430e0f4a802fa8792accca58f03e011120b94c4e Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Fri, 10 Nov 2017 20:06:26 +0300 Subject: [PATCH] buildBazelPackage: init A separate function for building Bazel-bazed packages. Internally it splits the build into two phases, fetching and building. Users are expected to provide `fetchArgs.sha256` -- checksum of fetched dependencies. Local dependencies should be removed in `fetchArgs.preInstall`. Overall `fetchArgs` and `buildArgs` can be used to add specific steps to fetch and build. --- .../build-bazel-package/default.nix | 78 +++++++++++++++++++ pkgs/top-level/all-packages.nix | 2 + 2 files changed, 80 insertions(+) create mode 100644 pkgs/build-support/build-bazel-package/default.nix diff --git a/pkgs/build-support/build-bazel-package/default.nix b/pkgs/build-support/build-bazel-package/default.nix new file mode 100644 index 000000000000..349a7e5aa333 --- /dev/null +++ b/pkgs/build-support/build-bazel-package/default.nix @@ -0,0 +1,78 @@ +{ stdenv, bazel }: + +args@{ name, bazelFlags ? [], bazelTarget, buildAttrs, fetchAttrs, ... }: + +let + fArgs = removeAttrs args [ "buildAttrs" "fetchAttrs" ]; + fBuildAttrs = fArgs // buildAttrs; + fFetchAttrs = fArgs // removeAttrs fetchAttrs [ "sha256" ]; + +in stdenv.mkDerivation (fBuildAttrs // { + inherit name bazelFlags bazelTarget; + + deps = stdenv.mkDerivation (fFetchAttrs // { + name = "${name}-deps"; + inherit bazelFlags bazelTarget; + + nativeBuildInputs = fFetchAttrs.nativeBuildInputs or [] ++ [ bazel ]; + + preHook = fFetchAttrs.preHook or "" + '' + export bazelOut="$NIX_BUILD_TOP/output" + export HOME="$NIX_BUILD_TOP" + ''; + + buildPhase = fFetchAttrs.buildPhase or '' + runHook preBuild + + bazel --output_base="$bazelOut" fetch $bazelFlags $bazelTarget + + runHook postBuild + ''; + + installPhase = fFetchAttrs.installPhase or '' + runHook preInstall + + # Patching markers to make them deterministic + for i in $bazelOut/external/\@*.marker; do + sed -i 's, -\?[0-9][0-9]*$, 1,' "$i" + done + # Patching symlinks to remove build directory reference + find $bazelOut/external -type l | while read symlink; do + ln -sf $(readlink "$symlink" | sed "s,$NIX_BUILD_TOP,NIX_BUILD_TOP,") "$symlink" + done + + cp -r $bazelOut/external $out + + runHook postInstall + ''; + + dontFixup = true; + outputHashMode = "recursive"; + outputHashAlgo = "sha256"; + outputHash = fetchAttrs.sha256; + }); + + nativeBuildInputs = fBuildAttrs.nativeBuildInputs or [] ++ [ (bazel.override { enableNixHacks = true; }) ]; + + preHook = fBuildAttrs.preHook or "" + '' + export bazelOut="$NIX_BUILD_TOP/output" + export HOME="$NIX_BUILD_TOP" + ''; + + preConfigure = '' + mkdir -p $bazelOut/external + cp -r $deps/* $bazelOut/external + chmod -R +w $bazelOut + find $bazelOut -type l | while read symlink; do + ln -sf $(readlink "$symlink" | sed "s,NIX_BUILD_TOP,$NIX_BUILD_TOP,") "$symlink" + done + '' + fBuildAttrs.preConfigure or ""; + + buildPhase = fBuildAttrs.buildPhase or '' + runHook preBuild + + bazel --output_base="$bazelOut" build -j $NIX_BUILD_CORES $bazelFlags $bazelTarget + + runHook postBuild + ''; +}) diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 958f2e0bcb09..2bcb1d10b093 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -7322,6 +7322,8 @@ with pkgs; bazel_0_4 = callPackage ../development/tools/build-managers/bazel/0.4.nix { }; bazel = callPackage ../development/tools/build-managers/bazel { }; + buildBazelPackage = callPackage ../build-support/build-bazel-package { }; + bear = callPackage ../development/tools/build-managers/bear { }; bin_replace_string = callPackage ../development/tools/misc/bin_replace_string { };