Caching nix shell build inputs

From NixOS Wiki
Revision as of 19:16, 2 September 2020 by imported>Fzakaria (Created page with "== Problem statement == Caching a Nix build is ''straightforward'' if there's a build result. We can use {{ic|nix-store --query --requisites}} to query the runtime closure of...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Problem statement

Caching a Nix build is straightforward if there's a build result. We can use nix-store --query --requisites to query the runtime closure of the build for a binary distribution".

Unfortunately, this is not doable with mkShell as they are purposefully not build-able.

let nixpkgs = import <nixpkgs> {};
in
with nixpkgs;
with stdenv;
with stdenv.lib;
mkShell {
  name = "example-shell";
  buildInputs = [];
}
$ nix-build shell.nix

This derivation is not meant to be built, aborting

We could perform nix-store --query --requisites on the derivation nix-instantiate shell.nix however that results in a source transitive closure; which is unecessary if the goal is to simply cache buildInputs.

How can we cache all buildInputs for mkShell?

A blog post on the subject improved upon the previous wisdom and came up with the following

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

The "trick" here; is to rely on --references which gives only the immediate dependencies. For each dependency then it's runtime closure is calculated.

inputDerivation

A recent improvement to 'nixpkgs included the inputDerivation attribute. https://github.com/NixOS/nixpkgs/pull/95536

This further improves on the previous example by also supporting derivations with multiple outputs.

nix-build shell.nix -A inputDerivation

or if you are using cachix

nix-build shell.nix -A inputDerivation | cachix push $name