2 releases
0.1.1 | Jan 1, 2022 |
---|---|
0.1.0 | Jan 1, 2022 |
#787 in Build Utils
2,487 downloads per month
Used in 3 crates
32KB
819 lines
dotenv-build
heavily based on dotenv
dotenv-build
helps you to supply your .env file as compile time environment variables in your build.rs
.
For more information, please visit the documentation.
lib.rs
:
Overview
This crate allows you to load .env files in your compilation step. It is built to be used in your build.rs file.
Usage
- Ensure you have build scripts enabled via the
build
configuration in yourCargo.toml
- Add
dotenv-build
as a build dependency - Create a
build.rs
file that usesdotenv-build
to generatecargo:
instructions. - Use the
env!
oroption_env!
macro in your code
Cargo.toml
[package]
#..
build = "build.rs"
[dependencies]
#..
[build-dependencies]
dotenv-build = "0.1"
build.rs
// in build.rs
fn main() {
dotenv_build::output(dotenv_build::Config::default()).unwrap();
}
Use in code
println!("Your environment variable in .env: {}", env!("TEST_VARIABLE"));
Configuration
Read more about the available options here: Config
let config = dotenv_build::Config {
filename: std::path::Path::new(".env.other"),
recursive_search: false,
fail_if_missing_dotenv: false,
..Default::default()
};
dotenv_build::output(config).unwrap();
Multiple files
Use output_multiple
for this:
use std::path::Path;
use dotenv_build::Config;
let configs = vec![
// load .env.base
Config {
filename: Path::new(".env.base"),
// fail_if_missing_dotenv: true,
..Default::default()
},
// load .env.staging
Config {
filename: Path::new(".env.staging"),
..Default::default()
},
// load .env
Config::default(),
];
dotenv_build::output_multiple(configs).unwrap();