From 48e3b26cfa66d70e643568199a8930dc7afa1b1b Mon Sep 17 00:00:00 2001
From: "Yang, Bo" <bo.yang@protonbase.io>
Date: Thu, 5 Sep 2024 14:48:27 +0000
Subject: [PATCH 1/6] feat: add `devenv.flakeArgs` to avoid `--no-pure-eval`

---
 src/devenv-devShell.nix      | 2 +-
 src/modules/top-level.nix    | 7 +++++++
 templates/flake-parts/.envrc | 7 ++++---
 3 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/devenv-devShell.nix b/src/devenv-devShell.nix
index 2be3a85bc..d44f67bf5 100644
--- a/src/devenv-devShell.nix
+++ b/src/devenv-devShell.nix
@@ -19,7 +19,7 @@ pkgs.writeScriptBin "devenv" ''
 
   case $command in
     up)
-      procfilescript=$(nix build '.#${shellPrefix (config._module.args.name or "default")}devenv-up' --no-link --print-out-paths --no-pure-eval)
+      procfilescript=$(nix build '.#${shellPrefix (config._module.args.name or "default")}devenv-up' --no-link --print-out-paths ${lib.escapeShellArgs config.devenv.flakeArgs})
       if [ "$(cat $procfilescript|tail -n +2)" = "" ]; then
         echo "No 'processes' option defined: https://devenv.sh/processes/"
         exit 1
diff --git a/src/modules/top-level.nix b/src/modules/top-level.nix
index edf4b361c..dd1361d0e 100644
--- a/src/modules/top-level.nix
+++ b/src/modules/top-level.nix
@@ -155,6 +155,12 @@ in
         default = builtins.getEnv "PWD";
       };
 
+      flakeArgs = lib.mkOption {
+        type = types.listOf types.str;
+        internal = true;
+        default = [ "--override-input" "devenv-root" "path:.devenv/state/pwd" ];
+      };
+
       dotfile = lib.mkOption {
         type = types.str;
         internal = true;
@@ -272,6 +278,7 @@ in
       fi
 
       mkdir -p "$DEVENV_STATE"
+      printf %s "$DEVENV_ROOT" > "$DEVENV_STATE/pwd"
       if [ ! -L "$DEVENV_DOTFILE/profile" ] || [ "$(${pkgs.coreutils}/bin/readlink $DEVENV_DOTFILE/profile)" != "${profile}" ]
       then
         ln -snf ${profile} "$DEVENV_DOTFILE/profile"
diff --git a/templates/flake-parts/.envrc b/templates/flake-parts/.envrc
index 70ce5295f..4ba52b74d 100644
--- a/templates/flake-parts/.envrc
+++ b/templates/flake-parts/.envrc
@@ -5,9 +5,10 @@ fi
 nix_direnv_watch_file flake.nix
 nix_direnv_watch_file flake.lock
 
-DEVENV_ROOT_FILE="$(mktemp)"
-printf %s "$PWD" > "$DEVENV_ROOT_FILE"
-if ! use flake . --override-input devenv-root "file+file://$DEVENV_ROOT_FILE"
+mkdir -p .devenv/state/
+printf %s "$PWD" > .devenv/state/pwd
+
+if ! use flake . --override-input devenv-root path:.devenv/state/pwd
 then
   echo "devenv could not be built. The devenv environment was not loaded. Make the necessary changes to devenv.nix and hit enter to try again." >&2
 fi

From 474452c258fff6ec1d04f927c5eee08d078f6342 Mon Sep 17 00:00:00 2001
From: "Yang, Bo" <bo.yang@protonbase.io>
Date: Sat, 7 Sep 2024 04:20:01 +0000
Subject: [PATCH 2/6] Skip writing `.devenv/state/pwd` when it is up-to-date

---
 templates/flake-parts/.envrc | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/templates/flake-parts/.envrc b/templates/flake-parts/.envrc
index 4ba52b74d..2159c9a89 100644
--- a/templates/flake-parts/.envrc
+++ b/templates/flake-parts/.envrc
@@ -6,7 +6,10 @@ nix_direnv_watch_file flake.nix
 nix_direnv_watch_file flake.lock
 
 mkdir -p .devenv/state/
-printf %s "$PWD" > .devenv/state/pwd
+if [ "$PWD" != "$(<.devenv/state/pwd)" ]
+then
+  printf %s "$PWD" > .devenv/state/pwd
+fi
 
 if ! use flake . --override-input devenv-root path:.devenv/state/pwd
 then

From 2f6bdb5b512ec91edbe8d48847e5e1da15e90f70 Mon Sep 17 00:00:00 2001
From: "Yang, Bo" <bo.yang@protonbase.io>
Date: Tue, 10 Sep 2024 12:49:55 -0700
Subject: [PATCH 3/6] Don't wrte .devenv/state/pwd in enterShell

---
 src/modules/top-level.nix | 1 -
 1 file changed, 1 deletion(-)

diff --git a/src/modules/top-level.nix b/src/modules/top-level.nix
index dd1361d0e..09bc6b4de 100644
--- a/src/modules/top-level.nix
+++ b/src/modules/top-level.nix
@@ -278,7 +278,6 @@ in
       fi
 
       mkdir -p "$DEVENV_STATE"
-      printf %s "$DEVENV_ROOT" > "$DEVENV_STATE/pwd"
       if [ ! -L "$DEVENV_DOTFILE/profile" ] || [ "$(${pkgs.coreutils}/bin/readlink $DEVENV_DOTFILE/profile)" != "${profile}" ]
       then
         ln -snf ${profile} "$DEVENV_DOTFILE/profile"

From 0b73d15ef4e1458fe041f69c6b17b75ccee3bef0 Mon Sep 17 00:00:00 2001
From: "Yang, Bo" <bo.yang@protonbase.io>
Date: Wed, 11 Sep 2024 20:03:16 +0000
Subject: [PATCH 4/6] Revert "Don't wrte .devenv/state/pwd in enterShell"

This reverts commit 2f6bdb5b512ec91edbe8d48847e5e1da15e90f70.
---
 src/modules/top-level.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/modules/top-level.nix b/src/modules/top-level.nix
index 09bc6b4de..dd1361d0e 100644
--- a/src/modules/top-level.nix
+++ b/src/modules/top-level.nix
@@ -278,6 +278,7 @@ in
       fi
 
       mkdir -p "$DEVENV_STATE"
+      printf %s "$DEVENV_ROOT" > "$DEVENV_STATE/pwd"
       if [ ! -L "$DEVENV_DOTFILE/profile" ] || [ "$(${pkgs.coreutils}/bin/readlink $DEVENV_DOTFILE/profile)" != "${profile}" ]
       then
         ln -snf ${profile} "$DEVENV_DOTFILE/profile"

From 56f801adad77df2191a9d0d15c8eddf8d571e0d8 Mon Sep 17 00:00:00 2001
From: "Yang, Bo" <bo.yang@protonbase.io>
Date: Wed, 11 Sep 2024 20:15:40 +0000
Subject: [PATCH 5/6] Create `pwd` file in `enterShell`

---
 src/modules/top-level.nix | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/modules/top-level.nix b/src/modules/top-level.nix
index dd1361d0e..c57fdeecc 100644
--- a/src/modules/top-level.nix
+++ b/src/modules/top-level.nix
@@ -278,7 +278,12 @@ in
       fi
 
       mkdir -p "$DEVENV_STATE"
-      printf %s "$DEVENV_ROOT" > "$DEVENV_STATE/pwd"
+
+      # The pwd file might not exist if the DEVENV_ROOT and DEVENV_STATE are determined from `--no-pure-eval` instead of `--override-input`, so we need to create it here
+      if [ "$DEVENV_ROOT" != "$(< "$DEVENV_STATE/pwd")" ]; then
+        printf %s "$DEVENV_ROOT" > "$DEVENV_STATE/pwd"
+      fi
+
       if [ ! -L "$DEVENV_DOTFILE/profile" ] || [ "$(${pkgs.coreutils}/bin/readlink $DEVENV_DOTFILE/profile)" != "${profile}" ]
       then
         ln -snf ${profile} "$DEVENV_DOTFILE/profile"

From e2e307e3e88d8cba44a37f65bdcdcec2cbf92e9e Mon Sep 17 00:00:00 2001
From: "Yang, Bo" <bo.yang@protonbase.io>
Date: Wed, 11 Sep 2024 20:30:08 +0000
Subject: [PATCH 6/6] Update comments

---
 src/modules/top-level.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/modules/top-level.nix b/src/modules/top-level.nix
index c57fdeecc..ddc83c041 100644
--- a/src/modules/top-level.nix
+++ b/src/modules/top-level.nix
@@ -279,7 +279,7 @@ in
 
       mkdir -p "$DEVENV_STATE"
 
-      # The pwd file might not exist if the DEVENV_ROOT and DEVENV_STATE are determined from `--no-pure-eval` instead of `--override-input`, so we need to create it here
+      # The pwd file might not exist if the DEVENV_ROOT and DEVENV_STATE are determined from `--no-pure-eval` instead of `--override-input`, for example, when running `nix develop --no-pure-eval --command devenv up`, so we need to create it here
       if [ "$DEVENV_ROOT" != "$(< "$DEVENV_STATE/pwd")" ]; then
         printf %s "$DEVENV_ROOT" > "$DEVENV_STATE/pwd"
       fi