Last active
July 18, 2018 12:26
-
-
Save Profpatsch/871841f30e08c8b8376c856d2cd76bad to your computer and use it in GitHub Desktop.
Use `--disk_cache` to share the bazel cache between multiple derivations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file uses the `--disk_cache` argument to share bazel caches | |
# between separate nix builds. Proof-of-work | |
# The second build succeds (after analyze phase) with: | |
# `INFO: 3 processes: 3 remote cache hit.` | |
with import <nixpkgs> {}; | |
let | |
bazel-example = { name, buildPhase, caches }: stdenv.mkDerivation { | |
inherit name; | |
buildInputs = [ unzip ]; | |
# we use the cpp hello examples in the bazel source distribution | |
src = bazel.src; | |
sourceRoot = "."; | |
phases = [ "unpackPhase" "buildPhase" ]; | |
buildPhase = '' | |
export HOME=$(mktemp -d) | |
# out is used as storage for the cache | |
mkdir $out | |
# link all inputs of caches into a combined, writable cache in $out | |
${lib.concatMapStringsSep "\n" | |
(c: "ln -s -t $out ${c}/*") caches} | |
echo "NEW cache contents in $out" | |
ls $out | |
${buildPhase} | |
''; | |
}; | |
lib-cache = { name, caches }: bazel-example { | |
name = "hello-lib-cache"; | |
inherit caches; | |
buildPhase = '' | |
${bazel}/bin/bazel build \ | |
--disk_cache=$out //examples/cpp:hello-lib | |
''; | |
}; | |
# first time around, there is no cache and lib gets built | |
first-build = lib-cache { | |
name = "hello-lib-1"; | |
caches = []; | |
}; | |
# second time around, everything is a cache hit | |
# (gives 3 remote cache hits) | |
second-build = lib-cache { | |
name = "hello-lib-2"; | |
caches = [ first-build ]; | |
}; | |
# since hello uses hello-lib, we can use the cache for those targets | |
# (gives 1 remote cache hit) | |
hello-build = bazel-example { | |
name = "hello-world"; | |
caches = [second-build]; | |
buildPhase = '' | |
${bazel}/bin/bazel build \ | |
--disk_cache=$out //examples/cpp:hello-world | |
''; | |
}; | |
in hello-build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> nix-build default.nix | |
these derivations will be built: | |
/nix/store/94rprhg2pnk2wv4nqh1jf0scvd3y2nib-hello-lib-cache.drv | |
/nix/store/987jwyv7k10dcgx802c9ik5dfn8x5z5c-hello-lib-cache.drv | |
/nix/store/k3mwc1b5mslhrw05zcwv5mzwnhx9b52d-hello-world.drv | |
building '/nix/store/94rprhg2pnk2wv4nqh1jf0scvd3y2nib-hello-lib-cache.drv'... | |
unpacking sources | |
unpacking source archive /nix/store/62081xrk8ipgcj9a0xm8dvcpk657sjfg-bazel-0.15.1-dist.zip | |
source root is . | |
setting SOURCE_DATE_EPOCH to timestamp 315532800 of file ./tools/zip/BUILD.tools | |
building | |
NEW cache contents in /nix/store/17sm3q14wi3x14z98f0q833bhw6hmrak-hello-lib-cache | |
Extracting Bazel installation... | |
Starting local Bazel server and connecting to it... | |
................. | |
Loading: | |
Loading: 0 packages loaded | |
Analyzing: target //examples/cpp:hello-lib (1 packages loaded) | |
Analyzing: target //examples/cpp:hello-lib (2 packages loaded) | |
Analyzing: target //examples/cpp:hello-lib (4 packages loaded) | |
INFO: Analysed target //examples/cpp:hello-lib (7 packages loaded). | |
INFO: Found 1 target... | |
[0 / 6] [-----] Writing file examples/cpp/libhello-lib.so-2.params | |
Target //examples/cpp:hello-lib up-to-date: | |
bazel-bin/examples/cpp/libhello-lib.a | |
bazel-bin/examples/cpp/libhello-lib.so | |
INFO: Elapsed time: 5.601s, Critical Path: 0.56s | |
INFO: 3 processes: 3 processwrapper-sandbox. | |
INFO: Build completed successfully, 6 total actions | |
INFO: Build completed successfully, 6 total actions | |
building '/nix/store/987jwyv7k10dcgx802c9ik5dfn8x5z5c-hello-lib-cache.drv'... | |
unpacking sources | |
unpacking source archive /nix/store/62081xrk8ipgcj9a0xm8dvcpk657sjfg-bazel-0.15.1-dist.zip | |
source root is . | |
setting SOURCE_DATE_EPOCH to timestamp 315532800 of file ./tools/zip/BUILD.tools | |
building | |
NEW cache contents in /nix/store/r5bng0l0i1wm9ds1kaf5vfjp43vw1sjy-hello-lib-cache | |
11491c374ddaf658b0c20d9e621d3f104ad8a52a69df9d5f977baef3d684d1a8 | |
5b2377ad14d73890b72ce6b4156f5759c2b317b36afdcc16958c5618eaa15f88 | |
75e50b4429d950c101093e7507ee87ceacce15eaa17f8808746c7698883685cf | |
8e411653e08fdbb03ca45a0f4f2ad72bf0f1cdf7b1b6e08bc393cb7a7f0fb2eb | |
8fbafb4819ed2c5dc3a5be3029d2b1ff0bd82737de8dc4c1cfc45196ad3d80bf | |
a92b16a8210086d028a5b8ce1d57b8b3d328854447e55d9833714173a90e7817 | |
c768625f80761de32e48fcb60bb54ddadabdeb33fadb562d2eacc62384303eac | |
Extracting Bazel installation... | |
Starting local Bazel server and connecting to it... | |
................ | |
Loading: | |
Loading: 0 packages loaded | |
Analyzing: target //examples/cpp:hello-lib (1 packages loaded) | |
Analyzing: target //examples/cpp:hello-lib (2 packages loaded) | |
Analyzing: target //examples/cpp:hello-lib (3 packages loaded) | |
INFO: Analysed target //examples/cpp:hello-lib (7 packages loaded). | |
INFO: Found 1 target... | |
[0 / 5] [-----] BazelWorkspaceStatusAction stable-status.txt | |
Target //examples/cpp:hello-lib up-to-date: | |
bazel-bin/examples/cpp/libhello-lib.a | |
bazel-bin/examples/cpp/libhello-lib.so | |
INFO: Elapsed time: 5.321s, Critical Path: 0.11s | |
INFO: 3 processes: 3 remote cache hit. | |
INFO: Build completed successfully, 6 total actions | |
INFO: Build completed successfully, 6 total actions | |
building '/nix/store/k3mwc1b5mslhrw05zcwv5mzwnhx9b52d-hello-world.drv'... | |
unpacking sources | |
unpacking source archive /nix/store/62081xrk8ipgcj9a0xm8dvcpk657sjfg-bazel-0.15.1-dist.zip | |
source root is . | |
setting SOURCE_DATE_EPOCH to timestamp 315532800 of file ./tools/zip/BUILD.tools | |
building | |
NEW cache contents in /nix/store/96rj8rzirw7w85zvmhxk0m2pa40y2agy-hello-world | |
11491c374ddaf658b0c20d9e621d3f104ad8a52a69df9d5f977baef3d684d1a8 | |
5b2377ad14d73890b72ce6b4156f5759c2b317b36afdcc16958c5618eaa15f88 | |
75e50b4429d950c101093e7507ee87ceacce15eaa17f8808746c7698883685cf | |
8e411653e08fdbb03ca45a0f4f2ad72bf0f1cdf7b1b6e08bc393cb7a7f0fb2eb | |
8fbafb4819ed2c5dc3a5be3029d2b1ff0bd82737de8dc4c1cfc45196ad3d80bf | |
a92b16a8210086d028a5b8ce1d57b8b3d328854447e55d9833714173a90e7817 | |
c768625f80761de32e48fcb60bb54ddadabdeb33fadb562d2eacc62384303eac | |
Extracting Bazel installation... | |
Starting local Bazel server and connecting to it... | |
................ | |
Loading: | |
Loading: 0 packages loaded | |
Analyzing: target //examples/cpp:hello-world (1 packages loaded) | |
Analyzing: target //examples/cpp:hello-world (2 packages loaded) | |
Analyzing: target //examples/cpp:hello-world (3 packages loaded) | |
INFO: Analysed target //examples/cpp:hello-world (7 packages loaded). | |
INFO: Found 1 target... | |
[0 / 6] [-----] Creating source manifest for //examples/cpp:hello-world | |
Target //examples/cpp:hello-world up-to-date: | |
bazel-bin/examples/cpp/hello-world | |
INFO: Elapsed time: 5.670s, Critical Path: 0.50s | |
INFO: 3 processes: 1 remote cache hit, 2 processwrapper-sandbox. | |
INFO: Build completed successfully, 7 total actions | |
INFO: Build completed successfully, 7 total actions | |
/nix/store/96rj8rzirw7w85zvmhxk0m2pa40y2agy-hello-world |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment