From f452533db5641ac53347f34de907450d2a73cfb8 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Tue, 31 Oct 2023 12:01:21 -0400 Subject: [PATCH 1/4] Fix nix-direnv-reload No longer assumes location of layout directory relative to code. --- direnvrc | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/direnvrc b/direnvrc index e4b3e77..33282a1 100644 --- a/direnvrc +++ b/direnvrc @@ -37,16 +37,34 @@ _nix_direnv_preflight () { if [[ ! -d "$layout_dir/bin" ]]; then mkdir -p "$layout_dir/bin" fi + # N.B. This script relies on variable expansion in *this* shell. + # (i.e. The written out file will have the variables expanded) + # If the source path changes, the script becomes broken. + # Because direnv_layout_dir is user controlled, + # we can't assume to be able to reverse it to get the source dir + # So there's little to be done about this. + cat > "${layout_dir}/bin/nix-direnv-reload" <<-EOF +#!/usr/bin/env bash +set -e +if [[ ! -d "$PWD" ]]; then + echo "Cannot find source directory; Did you move it?" + echo "(Looking for "$PWD")" + echo 'Cannot force reload with this script - use "direnv reload" manually and then try again' + exit 1 +fi + +# rebuild the cache forcefully +_nix_direnv_force_reload=1 direnv exec "$PWD" true + +# Update the mtime for .envrc. +# This will cause direnv to reload again - but without re-building. +touch "$PWD/.envrc" + +# Also update the timestamp of whatever profile_rc we have. +# This makes sure that we know we are up to date. +touch -r "$PWD/.envrc" ${layout_dir}/*.rc +EOF - # shellcheck disable=SC2016 - echo > "${layout_dir}/bin/nix-direnv-reload" '#!/usr/bin/env bash -dir="$(realpath $(dirname ${BASH_SOURCE[0]})/../..)" -_nix_direnv_force_reload=1 direnv exec "$dir" true -direnv reload -# direnv reload updates the mtime of .envrc. Also update the timestamp of the -# profile_rc file to keep track that we actually are up to date. -touch $dir/.direnv/{nix,flake}-profile-*.rc -' if [[ ! -x "${layout_dir}/bin/nix-direnv-reload" ]]; then chmod +x "${layout_dir}/bin/nix-direnv-reload" fi From 999093c7e64c68aa3e91e8b01b01deee01e2a0e7 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Tue, 31 Oct 2023 12:02:36 -0400 Subject: [PATCH 2/4] Promote Out of date cache messages to error This provides color so that the user can see them in the possible wall of text that direnv sometimes spits out. --- direnvrc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/direnvrc b/direnvrc index 33282a1..ef31f2e 100644 --- a/direnvrc +++ b/direnvrc @@ -253,9 +253,9 @@ use_flake() { then if [[ "$_nix_direnv_manual_reload" == "1" && -z "${_nix_direnv_force_reload-}" ]]; then if [[ -e "$profile_rc" ]]; then - log_status "nix-direnv: cache is out of date. use \"nix-direnv-reload\" to reload" + log_error "nix-direnv: cache is out of date. use \"nix-direnv-reload\" to reload" else - log_status "nix-direnv: cache does not exist. use \"nix-direnv-reload\" to create it" + log_error "nix-direnv: cache does not exist. use \"nix-direnv-reload\" to create it" fi else @@ -408,9 +408,9 @@ use_nix() { then if [[ "$_nix_direnv_manual_reload" == "1" && -z "${_nix_direnv_force_reload-}" ]]; then if [[ -e "$profile_rc" ]]; then - log_status "nix-direnv: cache is out of date. use \"nix-direnv-reload\" to reload" + log_error "nix-direnv: cache is out of date. use \"nix-direnv-reload\" to reload" else - log_status "nix-direnv: cache does not exist. use \"nix-direnv-reload\" to create it" + log_error "nix-direnv: cache does not exist. use \"nix-direnv-reload\" to create it" fi else _nix_clean_old_gcroots "$layout_dir" From 4bf5d40290981c867d70f6d3c0d505dd03bc2c0d Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Tue, 31 Oct 2023 12:03:26 -0400 Subject: [PATCH 3/4] Fix local flakes when prefixed with "path:" As mentioned in #347 --- direnvrc | 1 + 1 file changed, 1 insertion(+) diff --git a/direnvrc b/direnvrc index ef31f2e..498e2f9 100644 --- a/direnvrc +++ b/direnvrc @@ -212,6 +212,7 @@ use_flake() { flake_expr="${1:-.}" flake_dir="${flake_expr%#*}" + flake_dir=${flake_dir#"path:"} if [[ "$flake_expr" = -* ]]; then if [[ -n "$2" ]]; then From da6f18f71ba037c331b56c0afade8294723749b2 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Wed, 1 Nov 2023 09:25:35 -0400 Subject: [PATCH 4/4] Quote layout_dir during script write MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jörg Thalheim --- direnvrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/direnvrc b/direnvrc index 498e2f9..3e1cdd6 100644 --- a/direnvrc +++ b/direnvrc @@ -62,7 +62,7 @@ touch "$PWD/.envrc" # Also update the timestamp of whatever profile_rc we have. # This makes sure that we know we are up to date. -touch -r "$PWD/.envrc" ${layout_dir}/*.rc +touch -r "$PWD/.envrc" "${layout_dir}"/*.rc EOF if [[ ! -x "${layout_dir}/bin/nix-direnv-reload" ]]; then