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