2020-07-23 00:17:48 +03:00
|
|
|
# Serving a Nix store via HTTP
|
|
|
|
|
|
|
|
You can easily share the Nix store of a machine via HTTP. This allows
|
|
|
|
other machines to fetch store paths from that machine to speed up
|
|
|
|
installations. It uses the same *binary cache* mechanism that Nix
|
|
|
|
usually uses to fetch pre-built binaries from <https://cache.nixos.org>.
|
|
|
|
|
|
|
|
The daemon that handles binary cache requests via HTTP, `nix-serve`, is
|
|
|
|
not part of the Nix distribution, but you can install it from Nixpkgs:
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
```console
|
2021-11-17 17:30:39 +02:00
|
|
|
$ nix-env -iA nixpkgs.nix-serve
|
2020-07-31 16:43:25 +03:00
|
|
|
```
|
2020-07-23 00:17:48 +03:00
|
|
|
|
|
|
|
You can then start the server, listening for HTTP connections on
|
|
|
|
whatever port you like:
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
```console
|
|
|
|
$ nix-serve -p 8080
|
|
|
|
```
|
2020-07-23 00:17:48 +03:00
|
|
|
|
|
|
|
To check whether it works, try the following on the client:
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
```console
|
|
|
|
$ curl http://avalon:8080/nix-cache-info
|
|
|
|
```
|
2020-07-23 00:17:48 +03:00
|
|
|
|
|
|
|
which should print something like:
|
|
|
|
|
|
|
|
StoreDir: /nix/store
|
|
|
|
WantMassQuery: 1
|
|
|
|
Priority: 30
|
|
|
|
|
|
|
|
On the client side, you can tell Nix to use your binary cache using
|
|
|
|
`--option extra-binary-caches`, e.g.:
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
```console
|
2021-11-17 17:30:39 +02:00
|
|
|
$ nix-env -iA nixpkgs.firefox --option extra-binary-caches http://avalon:8080/
|
2020-07-31 16:43:25 +03:00
|
|
|
```
|
2020-07-23 00:17:48 +03:00
|
|
|
|
|
|
|
The option `extra-binary-caches` tells Nix to use this binary cache in
|
|
|
|
addition to your default caches, such as <https://cache.nixos.org>.
|
|
|
|
Thus, for any path in the closure of Firefox, Nix will first check if
|
|
|
|
the path is available on the server `avalon` or another binary caches.
|
|
|
|
If not, it will fall back to building from source.
|
|
|
|
|
|
|
|
You can also tell Nix to always use your binary cache by adding a line
|
|
|
|
to the `nix.conf` configuration file like this:
|
|
|
|
|
|
|
|
binary-caches = http://avalon:8080/ https://cache.nixos.org/
|