How do I set environment variables with Cargo?

Since Cargo 1.56, you can use the configurable-env feature via the [env] section in config.toml. This is not the same file as Cargo.toml, but it can still be set per project:

[env]
FOO = "bar"
PATH_TO_SOME_TOOL = { value = "bin/tool", relative = true }
USERNAME = { value = "test_user", force = true }

Environment variables set in this section will be applied to the environment of any processes executed by Cargo.

relative means the variable represents a path relative to the location of the directory that contains the .cargo/ directory that contains the config.toml file.

force means the variable can override an existing environment variable.

For more information about the history of this feature, see the related GitHub issue.

Leave a Comment