# Nix store cache in local network

**URL:** https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972
**Category:** Help
**Created:** [June 10, 2023, 3:00pm UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972 "2023-06-10T15:00:27Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![pbek](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pbek/32/9122_2.png) [@pbek](https://discourse.nixos.org/u/pbek)
#### Post date: [June 10, 2023, 3:00pm UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/1 "2023-06-10T15:00:27Z")

</div>

I was wondering what would be the best way to keep and serve a nix store cache in my local network to be able to more quickly update local machines which all run on a GitHub hosted flake [https://github.com/pbek/nixcfg/blob/9d15eecf1c45cd599e7bdc5459a6c4be8b4ad4d1/flake.nix](https://github.com/pbek/nixcfg/blob/9d15eecf1c45cd599e7bdc5459a6c4be8b4ad4d1/flake.nix).

The cache would have the highest priority, so derivations will not be downloaded from the internet if they are available locally.

I had several ideas:

1. I tried gathering all derivations of the current generation and pushing them on an [attic](https://github.com/zhaofengli/attic) server in the current network (running in docker on my Synology NAS) with [https://github.com/pbek/nixcfg/blob/9d15eecf1c45cd599e7bdc5459a6c4be8b4ad4d1/scripts/push-all-to-attic.sh#L41](https://github.com/pbek/nixcfg/blob/9d15eecf1c45cd599e7bdc5459a6c4be8b4ad4d1/scripts/push-all-to-attic.sh#L41)
  - For some reason that process is terribly slow, often only kB/s on my GBit network (attic even has a Postgres DB). ☹

2. Building a docker image of the flake on GitHub with the help of `nixos/nix:latest`, `docker pull` it to my NAS and then serve `/nix/store` in the local network somehow.
  - I wasn’t able to build the flake in docker yet, because there is no `nixos-rebuild` and I was able to achieve it with `nix` itself. ☹

3. `rsync avh --delete /nix/store` via SSH to my NAS from the first machine that downloaded the new derivations from the internet and serving it from the NAS somehow.
  - Storage size would be huge, because lots of generations would be synced.
  - Serving with [Serving a Nix store via SSH - Nix Reference Manual](https://nixos.org/manual/nix/stable/package-management/ssh-substituter.html) wouldn’t be easy from my NAS.

4. Leaving one NixOS machine running and serving the nix store via [Serving a Nix store via SSH - Nix Reference Manual](https://nixos.org/manual/nix/stable/package-management/ssh-substituter.html)
  - electricity is expensive nowadays, I already have two NAS and several Raspberry Pis running 😅

Any ideas or advice?

---

<div class="post-metadata">

### Author: ![danielbarter](https://discourse.nixos.org/user_avatar/discourse.nixos.org/danielbarter/32/7420_2.png) [@danielbarter](https://discourse.nixos.org/u/danielbarter)
#### Post date: [June 10, 2023, 3:26pm UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/2 "2023-06-10T15:26:08Z")

</div>

I have a machine on my LAN running [GitHub - edolstra/nix-serve: A standalone Nix binary cache server](https://github.com/edolstra/nix-serve). It also runs a bunch of other services, and pulls around 10-15W when not doing anything heavy.

I do all my building on there and all other machines use it as a substituter. Works great!

The only thing to watch out for is negative caching. If another machine tries to build something and finds it not in the local cache, it won’t query the local cache again for some time (even if you build the artifact there). You can reset the lookup cache by running

`rm $HOME/.cache/nix/binary-cache-v*.sqlite`

---

<div class="post-metadata">

### Author: ![misuzu](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/m/ba9def/32.png) [@misuzu](https://discourse.nixos.org/u/misuzu)
#### Post date: [June 10, 2023, 4:39pm UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/3 "2023-06-10T16:39:22Z")

</div>

> **[GitHub - cid-chan/peerix: Peer2Peer Nix-Binary-Cache](https://github.com/cid-chan/peerix)**
>
> Peer2Peer Nix-Binary-Cache. Contribute to cid-chan/peerix development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![ElvishJerricco](https://discourse.nixos.org/user_avatar/discourse.nixos.org/elvishjerricco/32/984_2.png) [@ElvishJerricco](https://discourse.nixos.org/u/ElvishJerricco)
#### Post date: [June 10, 2023, 9:17pm UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/4 "2023-06-10T21:17:23Z")

</div>

I have a Hydra instance set up on a server on my network configured with:

```auto
store_uri = file:///var/lib/nixcache?secret-key=/var/lib/hydra/nix-key&compression=zstd

```

And an nginx server that serves that `/var/lib/nixcache` directory. Works out great as a local cache. Downloads are fast and zstd decompression is nice to have.

That said, I have been eying `attic`. It seems really nice. Just needs hydra integration maybe?

---

<div class="post-metadata">

### Author: ![Solene](https://discourse.nixos.org/user_avatar/discourse.nixos.org/solene/32/5738_2.png) [@Solene](https://discourse.nixos.org/u/Solene)
#### Post date: [June 10, 2023, 10:05pm UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/5 "2023-06-10T22:05:17Z")

</div>

You can simply use Nginx on a server as a reverse proxy that will keep the files

I wrote a guide here [Solene'% : How to make a local NixOS cache server](https://dataswamp.org/~solene/2022-06-02-nixos-local-cache.html)

You could also use peerix to exchange story entry between machines on your local network, but it’s unmaintained, and sometimes doesn’t work well [Solene'% : Local peer to peer binary cache with NixOS and Peerix](https://dataswamp.org/~solene/2022-08-25-nixos-with-peerix.html)

---

<div class="post-metadata">

### Author: ![pbek](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pbek/32/9122_2.png) [@pbek](https://discourse.nixos.org/u/pbek)
#### Post date: [June 11, 2023, 7:12am UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/6 "2023-06-11T07:12:46Z")

</div>

Yeah, thank you. Some day I will also need to put a NixOS box up in my LAN to always run 😅  
Thank you for the info about negative caching, I didn’t know that!

---

<div class="post-metadata">

### Author: ![pbek](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pbek/32/9122_2.png) [@pbek](https://discourse.nixos.org/u/pbek)
#### Post date: [June 11, 2023, 7:14am UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/7 "2023-06-11T07:14:20Z")

</div>

Hah, that is nice too! Looks like it also needs NixOS, or can it run in a Docker container?

---

<div class="post-metadata">

### Author: ![pbek](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pbek/32/9122_2.png) [@pbek](https://discourse.nixos.org/u/pbek)
#### Post date: [June 11, 2023, 7:17am UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/8 "2023-06-11T07:17:41Z")

</div>

Thank you! Does Hyrda also run in a Docker container?  
Attic is great, you can have multiple caches. Set them public or not public. Does deduplication on block level over all caches. Has a garbage collector. AND I have it running in a Docker container!

---

<div class="post-metadata">

### Author: ![ElvishJerricco](https://discourse.nixos.org/user_avatar/discourse.nixos.org/elvishjerricco/32/984_2.png) [@ElvishJerricco](https://discourse.nixos.org/u/ElvishJerricco)
#### Post date: [June 11, 2023, 7:21am UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/9 "2023-06-11T07:21:46Z")

</div>

I’ve never tried to use hydra in a docker container. NixOS already has a good module for it so I just use it: [NixOS Search](https://search.nixos.org/options?channel=23.05&from=0&size=50&sort=relevance&type=packages&query=services.hydra)

In fairness, I _do_ use it in [a _NixOS style_ container](https://nixos.org/manual/nixos/stable/index.html#sec-declarative-containers), but that’s just for file system isolation purposes.

---

<div class="post-metadata">

### Author: ![pbek](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pbek/32/9122_2.png) [@pbek](https://discourse.nixos.org/u/pbek)
#### Post date: [June 11, 2023, 7:24am UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/10 "2023-06-11T07:24:22Z")

</div>

Thanks a lot for your blog posts! A proxy server that does the caching! Why didn’t I come up with that? 😆 You are keeping stuff for 60d, right? I wonder if I can directly build a Docker image from your config, or else I’d need to back-port it to be used with the conventional Nginx Docker image.

---

<div class="post-metadata">

### Author: ![pbek](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pbek/32/9122_2.png) [@pbek](https://discourse.nixos.org/u/pbek)
#### Post date: [June 11, 2023, 7:28am UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/11 "2023-06-11T07:28:11Z")

</div>

Thank you, one of these days I’ll simply put up a local NixOS server too, would make some things a lot easier. But Attic also did manage to build the atticd for a Docker image in [https://github.com/zhaofengli/attic/blob/b1fb790b5f2afaaa1b2f7f18979b8318abe604bb/.github/workflows/build.yml](https://github.com/zhaofengli/attic/blob/b1fb790b5f2afaaa1b2f7f18979b8318abe604bb/.github/workflows/build.yml).

---

<div class="post-metadata">

### Author: ![pbek](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pbek/32/9122_2.png) [@pbek](https://discourse.nixos.org/u/pbek)
#### Post date: [June 11, 2023, 2:03pm UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/12 "2023-06-11T14:03:47Z")

</div>

I rewrote the nix-nginx config as nginx config in [https://github.com/pbek/nixcfg/blob/b8ed727902e99becfb4fb51277b6081cd853fa33/docker/nix-cache-nginx/nginx.conf](https://github.com/pbek/nixcfg/blob/b8ed727902e99becfb4fb51277b6081cd853fa33/docker/nix-cache-nginx/nginx.conf) and replaced [https://cache.nixos.org/](https://cache.nixos.org/) with my local cache server, which runs on docker in my LAN.

Works great!

Of course nothing is ever found in the `/var/public-nix-cache` (`/var/nix/public-nix-cache` in my config), but after the 1st time a file is accessed it comes from cache.

@Solene, what purpose had `/var/public-nix-cache` for you? Did you put something there?

---

<div class="post-metadata">

### Author: ![pbek](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pbek/32/9122_2.png) [@pbek](https://discourse.nixos.org/u/pbek)
#### Post date: [June 11, 2023, 2:26pm UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/13 "2023-06-11T14:26:58Z")

</div>

The relevant part of my `docker-compose.yml` looks like this:

```plaintext
  nix-cache:
    image: ghcr.io/pbek/nixcfg-nix-cache-nginx:latest
    restart: always
    ports:
      - "8282:80"
    volumes:
      - /volume1/nix-cache:/var/nix

```

---

<div class="post-metadata">

### Author: ![Solene](https://discourse.nixos.org/user_avatar/discourse.nixos.org/solene/32/5738_2.png) [@Solene](https://discourse.nixos.org/u/Solene)
#### Post date: [June 12, 2023, 1:02pm UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/14 "2023-06-12T13:02:55Z")

</div>

I don’t remember exactly, I found that from another idea of caching, this file shouldn’t be cached for long IIRC

---

<div class="post-metadata">

### Author: ![ibizaman](https://discourse.nixos.org/user_avatar/discourse.nixos.org/ibizaman/32/6154_2.png) [@ibizaman](https://discourse.nixos.org/u/ibizaman)
#### Post date: [February 5, 2025, 9:59am UTC](https://discourse.nixos.org/t/nix-store-cache-in-local-network/28972/15 "2025-02-05T09:59:55Z")

</div>

You might be interested in [Announcing ncps: A Nix Cache Proxy Server for Faster Builds!](https://discourse.nixos.org/t/announcing-ncps-a-nix-cache-proxy-server-for-faster-builds/58166) that came out recently.
