Delay invalidating cache and gcroots until print-dev-env call succeeds

We currently eagerly invalidate the gcroots and old profile rc,
assuming that the devshell is in a usable state.
If this assumption does not hold,
we can invalidate a working state for a broken one.

Here we just delay calling _nix_clean_old_gcroots
until we know that we're in a usable state.

In the case that the flake is in an unusable state,
this simply reuses the newest working state.

This should address #412.
This commit is contained in:
Bryan Bennett 2023-11-29 13:58:27 -05:00
parent f33c17faff
commit 0c03af5544
No known key found for this signature in database
GPG key ID: EE149E4215408DE9

View file

@ -299,13 +299,12 @@ use_flake() {
_nix_direnv_warn_manual_reload "$profile_rc" _nix_direnv_warn_manual_reload "$profile_rc"
else else
local tmp_profile_rc
local tmp_profile="${layout_dir}/flake-profile.$$"
if tmp_profile_rc=$(_nix print-dev-env --profile "$tmp_profile" "$@"); then
_nix_clean_old_gcroots "$layout_dir" _nix_clean_old_gcroots "$layout_dir"
# We need to update our cache # We need to update our cache
local tmp_profile="${layout_dir}/flake-profile.$$"
local tmp_profile_rc
tmp_profile_rc=$(_nix print-dev-env \
--profile "$tmp_profile" "$@")
echo "$tmp_profile_rc" > "$profile_rc" echo "$tmp_profile_rc" > "$profile_rc"
_nix_add_gcroot "$tmp_profile" "$profile" _nix_add_gcroot "$tmp_profile" "$profile"
@ -326,10 +325,15 @@ use_flake() {
_nix_direnv_info "renewed cache" _nix_direnv_info "renewed cache"
fi fi
fi
else else
# Our cache is valid, use that" if [[ -e "${profile_rc}" ]]; then
# Our cache is valid, use that
_nix_direnv_info "using cached dev shell" _nix_direnv_info "using cached dev shell"
else
# We don't have a profile_rc to use!
_nix_direnv_fatal "use_flake failed - Is your flake's devShell working?"
fi
fi fi
_nix_import_env "$profile_rc" _nix_import_env "$profile_rc"
@ -442,11 +446,8 @@ use_nix() {
if [[ $_nix_direnv_manual_reload -eq 1 && -z "${_nix_direnv_force_reload-}" ]]; then if [[ $_nix_direnv_manual_reload -eq 1 && -z "${_nix_direnv_force_reload-}" ]]; then
_nix_direnv_warn_manual_reload "$profile_rc" _nix_direnv_warn_manual_reload "$profile_rc"
else else
_nix_clean_old_gcroots "$layout_dir"
local tmp_profile="${layout_dir}/flake-profile.$$" local tmp_profile="${layout_dir}/flake-profile.$$"
local tmp_profile_rc local tmp_profile_rc
if [[ -n "$packages" ]]; then if [[ -n "$packages" ]]; then
extra_args+=("--expr" "with import <nixpkgs> {}; mkShell { buildInputs = [ $packages ]; }") extra_args+=("--expr" "with import <nixpkgs> {}; mkShell { buildInputs = [ $packages ]; }")
else else
@ -458,20 +459,27 @@ use_nix() {
fi fi
fi fi
tmp_profile_rc=$(_nix \
if tmp_profile_rc=$(_nix \
print-dev-env \ print-dev-env \
--profile "$tmp_profile" \ --profile "$tmp_profile" \
--impure \ --impure \
"${extra_args[@]}") "${extra_args[@]}"); then
_nix_clean_old_gcroots "$layout_dir"
echo "$tmp_profile_rc" > "$profile_rc" echo "$tmp_profile_rc" > "$profile_rc"
_nix_add_gcroot "$tmp_profile" "$profile" _nix_add_gcroot "$tmp_profile" "$profile"
rm -f "$tmp_profile" "$tmp_profile"* rm -f "$tmp_profile" "$tmp_profile"*
_nix_direnv_info "renewed cache" _nix_direnv_info "renewed cache"
fi
fi fi
else else
if [[ -e "${profile_rc}" ]]; then
_nix_direnv_info "using cached dev shell" _nix_direnv_info "using cached dev shell"
else
_nix_direnv_fatal "use_nix failed - Is your nix shell working?"
fi
fi fi
_nix_import_env "$profile_rc" _nix_import_env "$profile_rc"