# manual config nix_version=2.3.6 nix_openpgp=B541D55301270E0BCF15CA5D8170B4726D7198DE nixpkgs_channel=nixos-unstable-small nixshell_sources=".envrc shell.nix servers.nix .config/nixpkgs-channel/$nixpkgs_channel.nix $(for d in shell nixpkgs; do test ! -d "$d" || find "$d" -type f -not -name "*~" done | sort ) " # nix if ! has nix || test "$(nix --version)" != "nix (Nix) $nix_version" then log_status "installing Nix core tools" gpg2 --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys "$nix_openpgp" mkdir -p .config/nix/"$nix_version" { flock --exclusive 3 if test ! -s .config/nix/"$nix_version"/install -o ! -s .config/nix/"$nix_version"/install.asc then (cd .config/nix/"$nix_version" curl -LOO 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/"$nix_version"/install.asc && sh .config/nix/"$nix_version"/install || { log_error "cannot install nix-$nix_version" return 1 } } . ~/.nix-profile/etc/profile.d/nix.sh } 3>>.config/nix/"$nix_version"/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 src in $nixshell_sources do watch_file "$src"; 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 direnv_watches=$DIRENV_WATCHES # Load the cached environment direnv_load sh -c "cat >\$DIRENV_DUMP_FILE_PATH $cache/dump" # Restore DIRENV_WATCHES to prevent infinite reload # when a watched file is touched without being modified # and thus without changing $hash. DIRENV_WATCHES=$direnv_watches #{ printf "\x1f\x8b\x08\x00\x00\x00\x00\x00"; echo $DIRENV_WATCHES | tr _- /+ | base64 -d; } | gzip -dc # 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 as a roots for the garbage-collector, # the --references of shell.nix's derivation # (non-buildable due to mkShell), # 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