Caching nix shell build inputs: Difference between revisions

From NixOS Wiki
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..."
 
imported>Ericson2314
m Make the shell.nix example ideomatically respect the buildInputs nativeBuildInputs distinction
Line 6: Line 6:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
let nixpkgs = import <nixpkgs> {};
let pkgs = import <nixpkgs> {};
in
in
with nixpkgs;
with pkgs.stdenv;
with stdenv;
with pkgs.stdenv.lib;
with stdenv.lib;
pkgs.mkShell {
mkShell {
   name = "example-shell";
   name = "example-shell";
   buildInputs = [];
  nativeBuildInputs = with pkgs.buildPackages; [ /* tools */ ];
   buildInputs = with pkgs; [ /* libs */ ];
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 21:13, 24 November 2020

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 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