Question
How do I set environment variables with Cargo?
I have a dependency listed in Cargo.toml that needs a specific environment variable set. I can run export FOO=bar
in bash and all works well, but for the life of me I can't figure out how to export this environment variable at compile time with Cargo. I've tried setting the environment variable in build.rs
via std::env
, Command
, and println!
, all to no effect:
// build.rs
fn main() {
Command::new("ls")
.env("FOO", "bar")
.spawn()
.expect("ls command failed to start");
}
// build.rs
fn main() {
std::env::set_var("FOO", "bar");
}
// build.rs
fn main() {
println!("cargo:rustc-env=FOO=bar");
}