# manual config nix_version=2.3 nix_openpgp=B541D55301270E0BCF15CA5D8170B4726D7198DE nixpkgs_channel=nixos-19.09-small nixshell_sources=(shell.nix $(test ! -d shell || find shell -type f -not -name "*~" | sort)) # nix if ! has nix || test "$(nix --version)" != "nix (Nix) $nix_version" then log_status "installing Nix core tools" if test ! -e .config/nix/install -o ! -e .config/nix/install.asc then mkdir -p .config/nix (cd .config/nix; curl -OO https://nixos.org/releases/nix/nix-"$nix_version"/{install,install.asc}) gpg2 --keyserver hkp://keys.gnupg.net --recv-keys "$nix_openpgp" fi test -e ~/.nix-profile/etc/profile.d/nix.sh || { gpg --verify .config/nix/install.asc sh .config/nix/install } . ~/.nix-profile/etc/profile.d/nix.sh fi # nixpkgs if test ! -e .config/nixpkgs-channel/$nixpkgs_channel.nix then log_status "installing nixpkgs from $nixpkgs_channel (This may take some time. To update: delete .config/nixpkgs-channel/$nixpkgs_channel.nix)" rev=$(curl -L https://nixos.org/channels/"$nixpkgs_channel"/git-revision | head -n1 | tr -dC 'a-z0-9') sha256=$(nix-prefetch-url --unpack https://github.com/NixOS/nixpkgs-channels/archive/"$rev".tar.gz) mkdir -p .config/nixpkgs-channel echo >.config/nixpkgs-channel/$nixpkgs_channel.nix "builtins.fetchTarball {url=\"https://github.com/NixOS/nixpkgs-channels/archive/$rev.tar.gz\"; sha256=\"$sha256\";}" else log_status "using nixpkgs from $nixpkgs_channel" fi watch_file .config/nixpkgs-channel/$nixpkgs_channel.nix # Get the store path of this nixpkgs, # it will not be registered as a root for the garbage-collector # but will be preserved as long as it is used by shell.nix # which itself will produce a registered derivation. nixpkgs=$(nix-instantiate --eval .config/nixpkgs-channel/$nixpkgs_channel.nix) nixpkgs=${nixpkgs#'"'} nixpkgs=${nixpkgs%'"'} export NIX_PATH="${NIX_PATH:+$NIX_PATH:}nixpkgs=$nixpkgs" # nix-shell has shasum || fail "shasum is needed to cache environment" for e in "${nixshell_sources[@]}" do watch_file "$e"; done hash=$(for e in "${nixshell_sources[@]}"; do shasum -a 256 "$e"; done | shasum -a 256 | cut -c -64) if test -e ".cache/nix-shell/${hash}/dump" then log_status "reusing .cache/nix-shell/$hash/" else log_status "building .cache/nix-shell/$hash/" # Unregister previous derivations rm -rf ".cache/nix-shell" trap "rm -rf '$PWD/.cache/nix-shell'" EXIT mkdir -p ".cache/nix-shell/$hash" # Register the derivation as a root for the garbage-collector nix-instantiate >/dev/null ./shell.nix \ --indirect --add-root .cache/nix-shell/"$hash"/shell.drv \ ${TRACE:+--show-trace} \ ${OFFLINE:+--option substituters ""} nix-store >/dev/null \ --indirect --add-root .cache/nix-shell/"$hash"/shell.dep \ ${OFFLINE:+--option substituters ""} \ --realise $(nix-store --query --references .cache/nix-shell/"$hash"/shell.drv) # Dump the environment from within the nix-shell local dump dump="$(mktemp .cache/nix-shell/dump-XXXXXXXX)" if nix-shell ${TRACE:+--show-trace} --pure \ --run "$(command -v direnv) dump" >"$dump" \ ${OFFLINE:+--option substituters ""} then mv -f "$dump" .cache/nix-shell/"$hash"/dump else rm -f "$dump"; false fi trap "" EXIT fi # Load the cached environment direnv_load cat .cache/nix-shell/"$hash"/dump # Re-run the shellHook to update envvars like GPG_TTY eval "$shellHook"