From 5d83c83dbd23e72e75363a1384cac45717eb6f01 Mon Sep 17 00:00:00 2001
From: Scott Robinson <ssr@squareup.com>
Date: Mon, 3 Jul 2023 19:29:31 +1000
Subject: [PATCH 1/3] fix: runtime-dependencies integration test names

---
 it/full/spec/it_spec.sh | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/it/full/spec/it_spec.sh b/it/full/spec/it_spec.sh
index c2c610bdb..2c1890f5c 100644
--- a/it/full/spec/it_spec.sh
+++ b/it/full/spec/it_spec.sh
@@ -318,7 +318,7 @@ Describe "Hermit"
 
   Describe "Runtime dependencies"
     . bin/activate-hermit
-    It "Does not install runtime-dependencies to the environment"
+    It "do not install to the environment"
       When call hermit install testbin1
       The status should be success
       The stderr should be blank
@@ -326,13 +326,13 @@ Describe "Hermit"
       The file ./bin/testbin2 should not be exist
     End
 
-    It "allows installing packages with binaries conflicting with runtime dependencies"
+    It "allow installing packages with conflicting binaries"
       When call hermit install faketestbin2
       The status should be success
       The stderr should be blank
     End
 
-    It "Calls the runtime dependency correctly"
+    It "calls the runtime dependency correctly"
       When call ./bin/testbin1
       The status should be success
       The stdout should equal "Hello from testbin2"
@@ -344,7 +344,7 @@ Describe "Hermit"
     ln -s . ./symlinked
     cd ./symlinked
     . bin/activate-hermit
-    It "Allows calling binaries in the environment"
+    It "allows calling binaries in the environment"
       When call ./bin/testbin1
       The status should be success
       The stdout should not be blank

From 376d15d7dc74bb3321dc78b372b157db7a1ce5d6 Mon Sep 17 00:00:00 2001
From: Alec Thomas <alec@swapoff.org>
Date: Fri, 25 Mar 2022 13:29:00 +1100
Subject: [PATCH 2/3] fix: allow envars to be overriden in active environments

---
 env.go                          | 20 ++++++++++++++++++++
 integration/integration_test.go | 10 ++++++++++
 2 files changed, 30 insertions(+)

diff --git a/env.go b/env.go
index 50fbd9c54..f53381666 100644
--- a/env.go
+++ b/env.go
@@ -748,6 +748,7 @@ func (e *Env) Exec(l *ui.UI, pkg *manifest.Package, binary string, args []string
 		return errors.WithStack(err)
 	}
 	ops := e.allEnvarOpsForPackages(runtimeDeps, pkg, installed...)
+	ops = append(ops, e.systemEnvOverrideOps(ops)...)
 	packageHermitBin, err := e.getPackageRuntimeEnvops(pkg)
 	if err != nil {
 		return errors.WithStack(err)
@@ -777,6 +778,25 @@ func (e *Env) Exec(l *ui.UI, pkg *manifest.Package, binary string, args []string
 	return errors.Errorf("%s: could not find binary %q", pkg, binary)
 }
 
+// systemEnvOverrideOps returns environment variables overrode in an already activated system environment
+func (e *Env) systemEnvOverrideOps(ops envars.Ops) envars.Ops {
+	if activeEnv, ok := os.LookupEnv("HERMIT_ENV"); !ok || activeEnv != e.envDir {
+		return envars.Ops{}
+	}
+
+	system := envars.Parse(os.Environ())
+	changed := system.Apply(e.Root(), ops).Changed(false)
+
+	var overrides envars.Ops
+	for envar := range changed {
+		if v, ok := system[envar]; ok {
+			overrides = append(overrides, &envars.Force{Name: envar, Value: v})
+		}
+	}
+
+	return overrides
+}
+
 func (e *Env) getPackageRuntimeEnvops(pkg *manifest.Package) (envars.Op, error) {
 	// If the package contains a Hermit env, add that to the PATH for runtime dependencies
 	pkgEnv, err := OpenEnv(pkg.Root, e.state, e.packageSource, nil, e.httpClient, e.scriptSums)
diff --git a/integration/integration_test.go b/integration/integration_test.go
index 62684ca71..6e9d33431 100644
--- a/integration/integration_test.go
+++ b/integration/integration_test.go
@@ -265,6 +265,16 @@ func TestIntegration(t *testing.T) {
 			assert test "$(testbin1.sh)" = "FOO=runtimefoo"
 			assert test "$(testbin2.sh)" = "BAR=hermitbar"
 			`},
+		{name: "SystemEnvOverridesAlreadyAcitvatedHermitEnv",
+			preparations: prep{fixture("testenv4"), activate(".")},
+			script: `
+			hermit install testbin1
+			hermit install testbin2
+			export FOO=systemfoo
+			assert test "$(testbin1.sh)" = "FOO=systemfoo"
+			export BAR=systembar
+			assert test "$(testbin2.sh)" = "BAR=systembar"
+			`},
 	}
 
 	checkForShells(t)

From 3cefbf581cbd37e6e5fcaad4d87127f46ba23a3b Mon Sep 17 00:00:00 2001
From: Scott Robinson <ssr@squareup.com>
Date: Tue, 4 Jul 2023 11:15:38 +1000
Subject: [PATCH 3/3] [WIP] double-check envar changes

---
 env.go | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/env.go b/env.go
index f53381666..3aa130091 100644
--- a/env.go
+++ b/env.go
@@ -788,9 +788,9 @@ func (e *Env) systemEnvOverrideOps(ops envars.Ops) envars.Ops {
 	changed := system.Apply(e.Root(), ops).Changed(false)
 
 	var overrides envars.Ops
-	for envar := range changed {
-		if v, ok := system[envar]; ok {
-			overrides = append(overrides, &envars.Force{Name: envar, Value: v})
+	for envar, v_new := range changed {
+		if v_system, ok := system[envar]; ok && v_new != v_system {
+			overrides = append(overrides, &envars.Force{Name: envar, Value: v_system})
 		}
 	}