# manual config nix_version=2.3 nix_openpgp=B541D55301270E0BCF15CA5D8170B4726D7198DE nixpkgs_channel=nixos-19.09-small nixshell_sources=(.envrc 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 .config/nixpkgs-channel/$nixpkgs_channel.nix" 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="nixpkgs=$nixpkgs${NIX_PATH:+:$NIX_PATH}" # nix-shell has shasum || { log_error "shasum is needed to cache environment"; return 1; } for e in "${nixshell_sources[@]}" do watch_file "$e"; done hash=$(shasum -a 256 "${nixshell_sources[@]}" | shasum -a 256 | cut -c -64) if test -e ".cache/nix-shell/$hash/dump" then log_status "reusing .cache/nix-shell/$hash/" # Load the cached environment direnv_load cat .cache/nix-shell/"$hash"/dump # Re-run the shellHook to update envvars like GPG_TTY, # and run gpg-connect-agent updatestartuptty /bye eval "$shellHook" else log_status "building .cache/nix-shell/$hash/" mkdir -p ".cache/nix-shell/$hash" local dump="$(mktemp .cache/nix-shell/$hash/dump-XXXXXXXX)" # Register the derivation as a root for the garbage-collector, # then cache a dump of the environment from within the nix-shell, # then unregister previous derivations, # then load the cached environment. 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) && nix-shell >"$dump" ${TRACE:+--show-trace} --pure \ --run "$(join_args "$direnv" dump)" \ ${OFFLINE:+--option substituters ""} && mv -f "$dump" .cache/nix-shell/"$hash"/dump && find .cache/nix-shell -mindepth 1 -maxdepth 1 -not -name "$hash" -exec rm -rf {} + && direnv_load cat .cache/nix-shell/"$hash"/dump || { rm -rf "$PWD/.cache/nix-shell/$hash" log_error "cannot build shell.nix" return 1 } fi