3 nix_openpgp=B541D55301270E0BCF15CA5D8170B4726D7198DE
4 nixpkgs_channel=nixos-19.09-small
5 nixshell_sources=(.envrc shell.nix $(test ! -d shell || find shell -type f -not -name "*~" | sort))
8 if ! has nix || test "$(nix --version)" != "nix (Nix) $nix_version"
9 then log_status "installing Nix core tools"
10 if test ! -e .config/nix/install -o ! -e .config/nix/install.asc
13 (cd .config/nix; curl -OO https://nixos.org/releases/nix/nix-"$nix_version"/{install,install.asc})
14 gpg2 --keyserver hkp://keys.gnupg.net --recv-keys "$nix_openpgp"
16 test -e ~/.nix-profile/etc/profile.d/nix.sh || {
17 gpg --verify .config/nix/install.asc &&
18 sh .config/nix/install
20 . ~/.nix-profile/etc/profile.d/nix.sh
24 if test ! -e .config/nixpkgs-channel/$nixpkgs_channel.nix
25 then log_status "installing nixpkgs from $nixpkgs_channel (This may take some time. To update: delete .config/nixpkgs-channel/$nixpkgs_channel.nix)"
26 rev=$(curl -L https://nixos.org/channels/"$nixpkgs_channel"/git-revision | head -n1 | tr -dC 'a-z0-9')
27 sha256=$(nix-prefetch-url --unpack https://github.com/NixOS/nixpkgs-channels/archive/"$rev".tar.gz)
28 mkdir -p .config/nixpkgs-channel
29 echo >.config/nixpkgs-channel/$nixpkgs_channel.nix "builtins.fetchTarball {url=\"https://github.com/NixOS/nixpkgs-channels/archive/$rev.tar.gz\"; sha256=\"$sha256\";}"
30 else log_status "using nixpkgs from .config/nixpkgs-channel/$nixpkgs_channel.nix"
32 watch_file .config/nixpkgs-channel/$nixpkgs_channel.nix
33 # Get the store path of this nixpkgs,
34 # it will not be registered as a root for the garbage-collector
35 # but will be preserved as long as it is used by shell.nix
36 # which itself will produce a registered derivation.
37 #nixpkgs=$(nix-instantiate --eval .config/nixpkgs-channel/$nixpkgs_channel.nix)
38 #nixpkgs=${nixpkgs#'"'}
39 #nixpkgs=${nixpkgs%'"'}
40 #export NIX_PATH="nixpkgs=$nixpkgs${NIX_PATH:+:$NIX_PATH}"
43 has shasum || { log_error "shasum is needed to cache environment"; return 1; }
44 for e in "${nixshell_sources[@]}"
45 do watch_file "$e"; done
46 hash=$(shasum -a 256 "${nixshell_sources[@]}" | shasum -a 256 | cut -c -64)
47 if test -e ".cache/nix-shell/$hash/dump"
49 log_status "reusing .cache/nix-shell/$hash/"
50 # Load the cached environment
51 direnv_load cat .cache/nix-shell/"$hash"/dump
52 # Re-run the shellHook to update envvars like GPG_TTY,
53 # and run gpg-connect-agent updatestartuptty /bye
55 else log_status "building .cache/nix-shell/$hash/"
56 mkdir -p ".cache/nix-shell/$hash"
57 local dump="$(mktemp .cache/nix-shell/$hash/dump-XXXXXXXX)"
58 # Register the derivation as a root for the garbage-collector,
59 # then cache a dump of the environment from within the nix-shell,
60 # then unregister previous derivations,
61 # then load the cached environment.
62 nix-instantiate >/dev/null ./shell.nix \
63 --indirect --add-root .cache/nix-shell/"$hash"/shell.drv \
64 ${TRACE:+--show-trace} \
65 ${OFFLINE:+--option substituters ""} &&
66 nix-store >/dev/null \
67 --indirect --add-root .cache/nix-shell/"$hash"/shell.dep \
68 ${OFFLINE:+--option substituters ""} \
69 --realise $(nix-store --query --references .cache/nix-shell/"$hash"/shell.drv) &&
70 nix-shell >"$dump" ${TRACE:+--show-trace} --pure \
71 --run "$(join_args "$direnv" dump)" \
72 ${OFFLINE:+--option substituters ""} &&
73 mv -f "$dump" .cache/nix-shell/"$hash"/dump &&
74 find .cache/nix-shell -mindepth 1 -maxdepth 1 -not -name "$hash" -exec rm -rf {} + &&
75 direnv_load cat .cache/nix-shell/"$hash"/dump || {
76 rm -rf "$PWD/.cache/nix-shell/$hash"
77 log_error "cannot build shell.nix"