Skip to content
Instant startup time for nix-shell
Rust Shell C Nix Other
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows
nix-trace
nix
src
tests
.gitattributes
.gitignore
Cargo.lock
Cargo.toml
LICENSE.MIT
UNLICENSE
build.rs
default.nix
maintain.sh
rcfile.sh
readme.md
rustfmt.toml
shell.nix

readme.md

cached-nix-shell

Build Status License

cached-nix-shell is a caching layer for nix-shell featuring instant startup time on subsequent runs.

It supports NixOS and Linux.

Installation

nix-env -i -f https://github.com/xzfc/cached-nix-shell/tarball/master

Usage

Just replace nix-shell with cached-nix-shell in the shebang line:

#! /usr/bin/env cached-nix-shell
#! nix-shell -i python3 -p python
print("Hello, World!")

Alternatively, call cached-nix-shell directly:

$ cached-nix-shell ./hello.py
$ cached-nix-shell -p python3 --run 'python --version'

Performance

$ time ./hello.py # first run; no cache used
cached-nix-shell: updating cache
Hello, World!
./hello.py  0.33s user 0.06s system 91% cpu 0.435 total
$ time ./hello.py
Hello, World!
./hello.py  0.02s user 0.01s system 97% cpu 0.029 total

Caching and cache invalidation

cached-nix-shell stores environment variables set up by nix-shell and reuses them on subsequent runs. It traces which files are read by nix during an evaluation, and performs a proper cache invalidation if any of the used files are changed. The cache is stored in ~/.cache/cached-nix-shell/.

The following situations are covered:

  • builtins.readFile is used
  • builtins.readDir is used
  • import ./file.nix is used
  • updating /etc/nix/nix.conf or ~/.config/nix/nix.conf
  • updating nix channels
  • updating $NIX_PATH environment variable

The following situations aren't handled by cached-nix-shell and may lead to staled cache:

  • builtins.fetchurl or other network builtins are used (e.g. in nixpkgs-mozilla)

Related

You can’t perform that action at this time.