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 coreutils
41 jq
42 python3
43 nix
44 ]
45 ++ lib.optional (compression == "xz") xz
46 ++ lib.optional (compression == "zstd") zstd;
47
48 buildCommand = ''
49 mkdir -p $out/nar
50
51 python ${./make-binary-cache.py} --compression "${compression}"
52
53 # These directories must exist, or Nix might try to create them in LocalBinaryCacheStore::init(),
54 # which fails if mounted read-only
55 mkdir $out/realisations
56 mkdir $out/debuginfo
57 mkdir $out/log
58 '';
59}