1{
2 lib,
3 stdenv,
4 coreutils,
5 jq,
6 python3,
7 nix,
8 xz,
9 zstd,
10}:
11
12# This function is for creating a flat-file binary cache, i.e. the kind created by
13# nix copy --to file:///some/path and usable as a substituter (with the file:// prefix).
14
15# For example, in the Nixpkgs repo:
16# nix-build -E 'with import ./. {}; mkBinaryCache { rootPaths = [hello]; }'
17
18{
19 name ? "binary-cache",
20 compression ? "zstd", # one of ["none" "xz" "zstd"]
21 rootPaths,
22}:
23
24assert lib.elem compression [
25 "none"
26 "xz"
27 "zstd"
28];
29
30stdenv.mkDerivation {
31 inherit name;
32
33 __structuredAttrs = true;
34
35 exportReferencesGraph.closure = rootPaths;
36
37 preferLocalBuild = true;
38
39 nativeBuildInputs =
40 [
41 coreutils
42 jq
43 python3
44 nix
45 ]
46 ++ lib.optional (compression == "xz") xz
47 ++ lib.optional (compression == "zstd") zstd;
48
49 buildCommand = ''
50 mkdir -p $out/nar
51
52 python ${./make-binary-cache.py} --compression "${compression}"
53
54 # These directories must exist, or Nix might try to create them in LocalBinaryCacheStore::init(),
55 # which fails if mounted read-only
56 mkdir $out/realisations
57 mkdir $out/debuginfo
58 mkdir $out/log
59 '';
60}