Nix Build Caching Inside Docker Containers
Published: 2022-01-02 21:45:36

---
title: Nix Build Caching Inside Docker Containers
published: true
date: 2022-01-02 17:03:00 UTC
tags: 
canonical_url: https://kevincox.ca/2022/01/02/nix-in-docker-caching/
---

<main><p><a href="https://nixos.org/explore.html">Nix</a> is a very useful tool for CI because it provides a portable and consistent build environment with accurate caching. I use it for almost all of my projects and have codified my setup in <a href="https://gitlab.com/kevincox/nix-ci/">https://gitlab.com/kevincox/nix-ci/</a>. This is a ready-to-go CI template for GitLab that uses Nix. While it supports binary caches (only Cachix at the moment), re-downloading all of the dependencies for each job can still take a minute or two. Not only is this a waste of time and resources but it also increased the odds that a small blip—whether on the runner or the binary cache—would cause a transient build failure. Since I am using self-hosted runners for a number of projects (my CPUs are a <strong>lot</strong> faster than those available on the shared runners) I wanted to see if I could cache dependencies without uploading, downloading or copying them around for each job.</p>
<h2 id="cache-volumes"><a href="https://kevincox.ca/2022/01/02/nix-in-docker-caching/#cache-volumes">Cache Volumes</a></h2>
<p>GitLab supports disk-based caches for self-hosted runners but using it with a Nix-based image such as nix-ci was problematic. The most obvious solution won’t work. You can declare <code>/nix</code> to be a cache volume that will be reused across jobs, but this has a couple of problems.</p>
<div><pre><code><span># GitLab Runner Config</span>
<span>[[runners]]</span>
	<span>executor</span> <span>=</span> <span>"docker"</span>
	<span>[runners.docker]</span>
		<span>volumes</span> <span>=</span> <span>["/nix"]</span>
</code></pre></div>
<p>One problem with this approach is that two concurrently running jobs will get different <code>/nix</code> volumes. While the volumes will be reused for future jobs, it means that the probably of a cache hit drops with the number of parallel jobs.</p>
<p>The bigger problem is that because the container is Nix-based it has its own files at <code>/nix</code>. This seems to work for the first job, and for subsequent jobs with the same image. However, jobs with a different image will fail with confusing “file not found” errors as the cache volume shadows the image files.</p>
<aside>I'm not sure why the first image works. Are the paths from the image somehow copied into the volume when it is first created?</aside><h2 id="chroot-store"><a href="https://kevincox.ca/2022/01/02/nix-in-docker-caching/#chroot-store">chroot Store</a></h2>
<p>The solution to this was to take advantage of the “chroot-store” feature of Nix. By passing the <code>--store /some/path</code> flag to <code>nix-build</code> it runs the build using the passed path as the store. <code>nix-build --store=/tmp/store/nix</code> can be thought of as logically equivalent to <code>chroot /tmp/store -- nix-build</code>. But <code>nix-build --store</code> reads the <code>nix-store</code> executable, source expressions and Nix config from outside the chroot before running the build inside.</p>
<p>I updated nix-ci to use a chroot store (located at <code>/mnt/nix</code>) for the build. Now that the “build store” didn’t conflict with the “image store” I could mount a cache-volume there and benefit from local caching across builds.</p>
<div><pre><code><span>[[runners]]</span>
	<span>executor</span> <span>=</span> <span>"docker"</span>
	<span>[runners.docker]</span>
		<span>volumes</span> <span>=</span> <span>["/mnt/nix"]</span>
</code></pre></div>
<h2 id="shared-store"><a href="https://kevincox.ca/2022/01/02/nix-in-docker-caching/#shared-store">Shared Store</a></h2>
<p>Cache volumes work fairly well but aren’t optimal with concurrent jobs. The GitLab Runner will create multiple volumes when jobs run concurrently which results in multiple independent stores. Not only does this waste disk space but a rebuild or redownload may occur just because the required path is in a different cache volume.</p>
<p>This can be resolved by bind-mounting all jobs to the same volume, but concurrent writes will cause errors in this scenario as the different jobs aren’t coordinating.</p>
<h2 id="host-passthrough"><a href="https://kevincox.ca/2022/01/02/nix-in-docker-caching/#host-passthrough">Host Passthrough</a></h2>
<p>These issues can be resolved if you are running a nix-daemon on the host. You can then bind the host store to the chroot store used for the build. This way jobs will have access to all paths available on the host and the daemon will be used to build new paths.</p>
<p>Not only does this work correctly for concurrent writes but the daemon also manages concurrency intelligently over the whole host. For example, <code>jobs</code> will apply across all CI jobs instead of each CI job starting a job per core and overloading the machine.</p>
<div><pre><code><span>[[runners]]</span>
	<span>executor</span> <span>=</span> <span>"docker"</span>
	<span>[runners.docker]</span>
		<span>volumes</span> <span>=</span> <span>["/nix:/mnt/nix"]</span>
</code></pre></div>
<h2 id="alternatives"><a href="https://kevincox.ca/2022/01/02/nix-in-docker-caching/#alternatives">Alternatives</a></h2>
<p>One alternative would be to build the image with an alternate store path. However, this requires compiling all packages from source. Doing this will require a lot of resources every time you update instead of simply downloading the prebuilt packages from <a href="https://cache.nixos.org">cache.nixos.org</a>.</p>
<h2 id="conclusion"><a href="https://kevincox.ca/2022/01/02/nix-in-docker-caching/#conclusion">Conclusion</a></h2>
<p>My particular use case is GitLab CI which uses Docker images for running jobs but I think this technique can be used any time you need to run a Nix build inside a container based on Nix. Also, this feature doesn’t really appear to be documented so I wanted to share a basic example.</p></main>