Pull in env vars from `source ./.venv/scripts/activate` and similar, like mise's `env._.source`
tl;dr ability to import env vars set by bootstrappers like mise, uv, .venv/Scripts/activate
Context
For python virtual environments, when we use . ./.venv/Scripts/activate, mise, and/or uv, these tools bootstrap (your shell's) environment variables. Without those env vars, commands like python don't work correctly.
You either have to:
- prefix every command with
uv runormise x --so instead ofpython main.py, youuv run main.pyormise x -- python main.py - install mise shims into your
$Path, so$ pythonspawns a mise shim which sets env vars before spawning the correctpython - set the env vars in your shell with
. ./.venv/Scripts/activateoreval "$(mise hook-env -s bash)"
What if just could pull in those env vars on startup?
mise has a feature to do this
mise doesn't normally need this because it, and its shims, set all the env vars themselves. Yet it also has a feature to pull in env vars from external executable sources like . ./.venv/activate.sh, and it would be cool if just had the same.
https://mise.jdx.dev/environments/#env-source
Source an external bash script and pull exported environment variables out of it:
# In your mise.toml
[env]
_.source = './script.sh'
Implementation
In just parlance, it's doing the following:
- Spawn a bash shell. Inside that shell:
- dot-source
./script.sh - run
export -pto dump entire environment to stdout
- dot-source
- Back in rust land, parse bash's output and
export FOO :=each env var
Why?
Configuring a shell integration, installing mise shims on $PATH, these are great options if you already use those tools. But as an open-source contributor, perhaps wanting to fix a simple bug, do I want the maintainer to impose their mise environment on me? Or would I rather their justfile set up everything automatically, so I can focus on fixing the bug?
From the perspective of the open-source maintainer, wanting to provide a smooth on-ramp for said contributors, we want our justfile to do the heavy lifting, which means bootstrapping the environment.
Workarounds
Here are the workarounds you can use today:
- install
justviamise. Whenjustis amiseshim, it gets allmiseenv vars on startup -
alias just='mise x -- just' - ~~
set shell := ['mise', 'x', '--', 'sh', '-euc']andset script-interpreter :=...~~- first workaround does the same and is simpler
- pre-export env vars to a
.envfile whichjustimports- requires
set dotenv-override -
.envmust be regenerated often to avoid falling out-of-date
- requires