direnv: fix infinite reload
authorJulien Moutinho <julm@sourcephile.fr>
Sat, 28 Mar 2020 23:34:19 +0000 (00:34 +0100)
committerJulien Moutinho <julm@sourcephile.fr>
Sat, 28 Mar 2020 23:35:31 +0000 (00:35 +0100)
.envrc

diff --git a/.envrc b/.envrc
index 39d84c0b1b5e6d1704ac990046f274b1880c1b83..496eee1a1e9b273e17ea3d3b2035aa8cb1844794 100644 (file)
--- a/.envrc
+++ b/.envrc
@@ -2,18 +2,18 @@
 nix_version=2.3.3
 nix_openpgp=B541D55301270E0BCF15CA5D8170B4726D7198DE
 nixpkgs_channel=nixos-unstable-small
-nixshell_sources=(.envrc shell.nix overlays.nix
- .config/nixpkgs-channel/"$nixpkgs_channel".nix
+nixshell_sources=".envrc shell.nix overlays.nix
+ .config/nixpkgs-channel/$nixpkgs_channel.nix
  $(for d in patches shell overlays; 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"
+  gpg2 --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys "$nix_openpgp"
   mkdir -p .config/nix/"$nix_version"
   {
   flock --exclusive 3
@@ -60,9 +60,9 @@ export nixpkgs_channel
 
 # 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)
+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"
@@ -70,12 +70,18 @@ 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
+  } 3<"$cache"/dump
 else
   log_status "building $cache/"
   mkdir -p "$cache"
@@ -89,7 +95,7 @@ else
    ${TRACE:+--show-trace} \
    ${OFFLINE:+--option substituters ""} &&
   nix-store >/dev/null --indirect --add-root "$cache"/shell.dep \
-   --realise $(nix-store --query --references $cache/shell.drv) \
+   --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\" \
@@ -99,5 +105,5 @@ else
     log_error "cannot build shell.nix"
     return 1
   }
-  } 3>$cache/dump
+  } 3>"$cache"/dump
 fi