Best practice for caching your nix-shell

I have been enjoying nix-shell for reproducible build-environments; but was not happy at the common wisdom on how to cache the shell in a binary store such as something in Cachix.

The prevailing wisdom outlines a way that ends up caching the whole build-time transitive closure which is way too large.

Here is a much more concise graph for what you need to exactly run your nix-shell

 $ nix-store --query --references $(nix-instantiate shell.nix) | \
    xargs nix-store --realise | \
    xargs nix-store --query --requisites | \
    cachix push your_cache

I wrote a little blog post on the subject as well https://fzakaria.com/2020/08/11/caching-your-nix-shell.html

10 Likes

There’s some new development which you can read up