Caching nix shell build inputs: Difference between revisions
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..." |
mNo edit summary |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
let | let pkgs = import <nixpkgs> {}; | ||
in | in | ||
with | with pkgs.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> | ||
Line 27: | Line 27: | ||
== How can we cache all buildInputs for mkShell? == | == How can we cache all buildInputs for mkShell? == | ||
A [https://fzakaria.com/2020/08/11/caching-your-nix-shell.html blog post] on the subject improved upon the previous wisdom and came up with the following | A [https://fzakaria.com/2020/08/11/caching-your-nix-shell.html blog post] ([https://web.archive.org/web/20201012134126/https://fzakaria.com/2020/08/11/caching-your-nix-shell.html mirror]) on the subject improved upon the previous wisdom and came up with the following | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 53: | Line 53: | ||
nix-build shell.nix -A inputDerivation | cachix push $name | nix-build shell.nix -A inputDerivation | cachix push $name | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:nix]] |
Latest revision as of 19:55, 24 April 2024
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