just icon indicating copy to clipboard operation
just copied to clipboard

Pull in env vars from `source ./.venv/scripts/activate` and similar, like mise's `env._.source`

Open cspotcode opened this issue 3 months ago • 0 comments

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 run or mise x -- so instead of python main.py, you uv run main.py or mise x -- python main.py
  • install mise shims into your $Path, so $ python spawns a mise shim which sets env vars before spawning the correct python
  • set the env vars in your shell with . ./.venv/Scripts/activate or eval "$(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 -p to dump entire environment to stdout
  • 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 just via mise. When just is a mise shim, it gets all mise env vars on startup
  • alias just='mise x -- just'
  • ~~set shell := ['mise', 'x', '--', 'sh', '-euc'] and set script-interpreter := ...~~
    • first workaround does the same and is simpler
  • pre-export env vars to a .env file which just imports
    • requires set dotenv-override
    • .env must be regenerated often to avoid falling out-of-date

cspotcode avatar Nov 19 '25 17:11 cspotcode