# manual config nix_version=2.3.2 nix_openpgp=B541D55301270E0BCF15CA5D8170B4726D7198DE nixpkgs_channel=nixos-unstable-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" gpg2 --keyserver hkp://wwwkeys.de.pgp.net:80 --recv-keys "$nix_openpgp" { flock --exclusive 3 if test ! -s .config/nix/install -o ! -s .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}) fi test -e ~/.nix-profile/etc/profile.d/nix.sh || { gpg --verify .config/nix/install.asc && sh .config/nix/install || { log_error "cannot install nix-$nix_version" return 1 } } . ~/.nix-profile/etc/profile.d/nix.sh } 3>>.config/nix/install fi # nixpkgs mkdir -p .config/nixpkgs-channel { flock --exclusive 3 if test ! -s .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) 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 } 3>>.config/nixpkgs-channel/$nixpkgs_channel.nix watch_file .config/nixpkgs-channel/$nixpkgs_channel.nix # Used in shell.nix export nixpkgs_channel # 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) cache=.cache/nix-shell/"$hash" unset DIRENV_DUMP_FILE_PATH if test -e "$cache/dump" then log_status "reusing $cache/" { flock --shared 3 # Load the cached environment direnv_load sh -c "cat >\$DIRENV_DUMP_FILE_PATH $cache/dump" # Re-run the shellHook to update envvars like GPG_TTY, # and run gpg-connect-agent updatestartuptty /bye eval "$shellHook" } 3<$cache/dump else log_status "building $cache/" mkdir -p "$cache" { flock --exclusive 3 # 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"/shell.drv \ ${TRACE:+--show-trace} \ ${OFFLINE:+--option substituters ""} && nix-store >/dev/null --indirect --add-root "$cache"/shell.dep \ --realise $(nix-store --query --references $cache/shell.drv) \ ${OFFLINE:+--option substituters ""} && direnv_load sh -c "nix-shell ${TRACE:+--show-trace} \ --run \"DIRENV_DUMP_FILE_PATH= $direnv dump | tee $cache/dump >\$DIRENV_DUMP_FILE_PATH\" \ ${OFFLINE:+--option substituters ""}" && find .cache/nix-shell -mindepth 1 -maxdepth 1 -not -name "$hash" -exec rm -rf {} + || { rm -rf "$PWD/.cache/nix-shell/$hash" log_error "cannot build shell.nix" return 1 } } 3>$cache/dump fi