diff --git a/docs/docs/usage/envars.md b/docs/docs/usage/envars.md index 884a717c..61cc892a 100644 --- a/docs/docs/usage/envars.md +++ b/docs/docs/usage/envars.md @@ -17,6 +17,7 @@ present in an active environment are: |-----------|------| | `HERMIT_ENV` | Path to the active Hermit environment. | | `HERMIT_BIN` | Path to the active Hermit environment `bin` directory. | +| `HERMIT_PREPEND_PATH` | If set, prepends its value to the front of `PATH` after all other environment operations are applied. Useful for ensuring specific directories always take precedence over Hermit-managed packages. | An empty environment might look something like the following: diff --git a/env.go b/env.go index 1d1600b1..0d847416 100644 --- a/env.go +++ b/env.go @@ -1429,6 +1429,9 @@ func (e *Env) allEnvarOpsForPackages(runtimeDeps []*manifest.Package, targetPkg } ops = append(ops, e.localEnvarOps()...) ops = append(ops, e.ephemeralEnvars...) + if prependPath := os.Getenv("HERMIT_PREPEND_PATH"); prependPath != "" { + ops = append(ops, &envars.Prepend{Name: "PATH", Value: prependPath}) + } return ops } diff --git a/integration/integration_test.go b/integration/integration_test.go index 52eba852..545d21e5 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -673,6 +673,15 @@ EOF assert test "${CLEANUP_DONE:-}" = "yes" `, }, + { + name: "HermitPrependPathIsRespected", + preparations: prep{fixture("testenv1")}, + script: ` + export HERMIT_PREPEND_PATH="/prepend/first:/prepend/second" + . bin/activate-hermit + assert echo "$PATH" | grep -q "^/prepend/first:/prepend/second:" + `, + }, { name: "InstallOnActivateEnsuresPackagesAreUnpacked", preparations: prep{fixture("testenv-install-on-activate"), activate(".")},