Caching nix shell build inputs

From NixOS Wiki
Revision as of 11:02, 6 April 2024 by Maintenance script (talk | contribs) (rollback unauthorized mass edits)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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 pkgs = import <nixpkgs> {};
in
with pkgs.stdenv;
with pkgs.stdenv.lib;
pkgs.mkShell {
  name = "example-shell";
  nativeBuildInputs = with pkgs.buildPackages; [ /* tools */ ];
  buildInputs = with pkgs; [ /* libs */ ];
}
$ 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 (mirror) 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