1# pkgs.mkBinaryCache {#sec-pkgs-binary-cache}
2
3`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.
4
5Nix 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.
6
7Note 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.
8
9## Example {#sec-pkgs-binary-cache-example}
10
11The following derivation will construct a flat-file binary cache containing the closure of `hello`.
12
13```nix
14mkBinaryCache {
15 rootPaths = [hello];
16}
17```
18
19- `rootPaths` specifies a list of root derivations. The transitive closure of these derivations' outputs will be copied into the cache.
20
21Here's an example of building and using the cache.
22
23Build the cache on one machine, `host1`:
24
25```shellSession
26nix-build -E 'with import <nixpkgs> {}; mkBinaryCache { rootPaths = [hello]; }'
27```
28
29```shellSession
30/nix/store/cc0562q828rnjqjyfj23d5q162gb424g-binary-cache
31```
32
33Copy the resulting directory to the other machine, `host2`:
34
35```shellSession
36scp result host2:/tmp/hello-cache
37```
38
39Substitute the derivation using the flat-file binary cache on the other machine, `host2`:
40```shellSession
41nix-build -A hello '<nixpkgs>' \
42 --option require-sigs false \
43 --option trusted-substituters file:///tmp/hello-cache \
44 --option substituters file:///tmp/hello-cache
45```
46
47```shellSession
48/nix/store/gl5a41azbpsadfkfmbilh9yk40dh5dl0-hello-2.12.1
49```