+1
doc/builders/images.xml
+1
doc/builders/images.xml
+49
doc/builders/images/binarycache.section.md
+49
doc/builders/images/binarycache.section.md
···
···+`pkgs.mkBinaryCache` is a function for creating Nix flat-file binary caches. Such a cache exists as a directory on disk, and can be used as a Nix substituter by passing `--substituter file:///path/to/cache` to Nix commands.+Nix packages are most commonly shared between machines using [HTTP, SSH, or S3](https://nixos.org/manual/nix/stable/package-management/sharing-packages.html), but a flat-file binary cache can still be useful in some situations. For example, you can copy it directly to another machine, or make it available on a network file system. It can also be a convenient way to make some Nix packages available inside a container via bind-mounting.+Note that this function is meant for advanced use-cases. The more idiomatic way to work with flat-file binary caches is via the [nix-copy-closure](https://nixos.org/manual/nix/stable/command-ref/nix-copy-closure.html) command. You may also want to consider [dockerTools](#sec-pkgs-dockerTools) for your containerization needs.+The following derivation will construct a flat-file binary cache containing the closure of `hello`.+- `rootPaths` specifies a list of root derivations. The transitive closure of these derivations' outputs will be copied into the cache.
+1
nixos/tests/all-tests.nix
+1
nixos/tests/all-tests.nix
···
+62
nixos/tests/binary-cache.nix
+62
nixos/tests/binary-cache.nix
···
···+cachePath = machine.succeed("nix-build --no-out-link -E 'with import <nixpkgs> {}; mkBinaryCache { rootPaths = [hello]; }'").strip()+grepLogs = machine.succeed("grep -l 'StorePath: /nix/store/[[:alnum:]]*-hello-.*' /tmp/cache/*.narinfo")+match = re.match(r"^StorePath: (/nix/store/[a-z0-9]*-hello-.*)$", narInfoContents, re.MULTILINE)+logs = machine.succeed("nix-build -A hello '<nixpkgs>' --option require-sigs false --option trusted-substituters file:///tmp/cache --option substituters file:///tmp/cache 2>&1")+if not "this path will be fetched" in logLines[0]: raise Exception("Unexpected first log line")
+40
pkgs/build-support/binary-cache/default.nix
+40
pkgs/build-support/binary-cache/default.nix
···
···+PATH = "${buildPackages.coreutils}/bin:${buildPackages.jq}/bin:${buildPackages.python3}/bin:${buildPackages.nix}/bin:${buildPackages.xz}/bin";+# These directories must exist, or Nix might try to create them in LocalBinaryCacheStore::init(),
+43
pkgs/build-support/binary-cache/make-binary-cache.py
+43
pkgs/build-support/binary-cache/make-binary-cache.py
···
···+fileHash = subprocess.run(["nix-hash", "--base32", "--type", "sha256", item["path"]], capture_output=True).stdout.decode().strip()
+2
pkgs/top-level/all-packages.nix
+2
pkgs/top-level/all-packages.nix