3 nix_openpgp=B541D55301270E0BCF15CA5D8170B4726D7198DE
4 nixpkgs_channel=nixos-unstable-small
5 nixshell_sources=(.envrc shell.nix
6 $(for d in patches shell; do
7 test ! -d "$d" || find "$d" -type f -not -name "*~"
13 if ! has nix || test "$(nix --version)" != "nix (Nix) $nix_version"
14 then log_status "installing Nix core tools"
15 gpg2 --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys "$nix_openpgp"
18 if test ! -s .config/nix/install -o ! -s .config/nix/install.asc
21 (cd .config/nix; curl -OO https://nixos.org/releases/nix/nix-"$nix_version"/{install,install.asc})
23 test -e ~/.nix-profile/etc/profile.d/nix.sh || {
24 gpg --verify .config/nix/install.asc &&
25 sh .config/nix/install || {
26 log_error "cannot install nix-$nix_version"
30 . ~/.nix-profile/etc/profile.d/nix.sh
31 } 3>>.config/nix/install
35 mkdir -p .config/nixpkgs-channel
38 if test ! -s .config/nixpkgs-channel/$nixpkgs_channel.nix
39 then log_status "installing nixpkgs from $nixpkgs_channel (This may take some time. To update: delete .config/nixpkgs-channel/$nixpkgs_channel.nix)"
40 rev=$(curl -L https://nixos.org/channels/"$nixpkgs_channel"/git-revision | head -n1 | tr -dC 'a-z0-9')
41 sha256=$(nix-prefetch-url --unpack https://github.com/NixOS/nixpkgs-channels/archive/"$rev".tar.gz)
42 echo >.config/nixpkgs-channel/$nixpkgs_channel.nix \
43 "builtins.fetchTarball {url=\"https://github.com/NixOS/nixpkgs-channels/archive/$rev.tar.gz\"; sha256=\"$sha256\";}"
44 else log_status "using nixpkgs from .config/nixpkgs-channel/$nixpkgs_channel.nix"
46 } 3>>.config/nixpkgs-channel/$nixpkgs_channel.nix
47 watch_file .config/nixpkgs-channel/$nixpkgs_channel.nix
49 export nixpkgs_channel
50 # Get the store path of this nixpkgs,
51 # it will not be registered as a root for the garbage-collector
52 # but will be preserved as long as it is used by shell.nix
53 # which itself will produce a registered derivation.
54 #nixpkgs=$(nix-instantiate --eval .config/nixpkgs-channel/$nixpkgs_channel.nix)
55 #nixpkgs=${nixpkgs#'"'}
56 #nixpkgs=${nixpkgs%'"'}
57 #export NIX_PATH="nixpkgs=$nixpkgs${NIX_PATH:+:$NIX_PATH}"
60 has shasum || { log_error "shasum is needed to cache environment"; return 1; }
61 for e in "${nixshell_sources[@]}"
62 do watch_file "$e"; done
63 hash=$(shasum -a 256 "${nixshell_sources[@]}" | shasum -a 256 | cut -c -64)
64 cache=.cache/nix-shell/"$hash"
65 unset DIRENV_DUMP_FILE_PATH
66 if test -e "$cache/dump"
68 log_status "reusing $cache/"
71 # Load the cached environment
72 direnv_load sh -c "cat >\$DIRENV_DUMP_FILE_PATH $cache/dump"
73 # Re-run the shellHook to update envvars like GPG_TTY,
74 # and run gpg-connect-agent updatestartuptty /bye
78 log_status "building $cache/"
82 # Register the derivation as a root for the garbage-collector,
83 # then cache a dump of the environment from within the nix-shell,
84 # then unregister previous derivations,
85 # then load the cached environment.
86 nix-instantiate >/dev/null ./shell.nix --indirect --add-root "$cache"/shell.drv \
87 ${TRACE:+--show-trace} \
88 ${OFFLINE:+--option substituters ""} &&
89 nix-store >/dev/null --indirect --add-root "$cache"/shell.dep \
90 --realise $(nix-store --query --references $cache/shell.drv) \
91 ${OFFLINE:+--option substituters ""} &&
92 direnv_load sh -c "nix-shell ${TRACE:+--show-trace} \
93 --run \"DIRENV_DUMP_FILE_PATH= $direnv dump | tee $cache/dump >\$DIRENV_DUMP_FILE_PATH\" \
94 ${OFFLINE:+--option substituters ""}" &&
95 find .cache/nix-shell -mindepth 1 -maxdepth 1 -not -name "$hash" -exec rm -rf {} + || {
96 rm -rf "$PWD/.cache/nix-shell/$hash"
97 log_error "cannot build shell.nix"