#env-file #env #dotenv #environment #env-var #build-config

build dotenv-build

Crate to help you supply compile time environment variables from your .env file

2 releases

0.1.1 Jan 1, 2022
0.1.0 Jan 1, 2022

#787 in Build Utils

Download history 388/week @ 2024-08-24 559/week @ 2024-08-31 284/week @ 2024-09-07 387/week @ 2024-09-14 624/week @ 2024-09-21 324/week @ 2024-09-28 305/week @ 2024-10-05 519/week @ 2024-10-12 578/week @ 2024-10-19 678/week @ 2024-10-26 770/week @ 2024-11-02 703/week @ 2024-11-09 418/week @ 2024-11-16 675/week @ 2024-11-23 673/week @ 2024-11-30 529/week @ 2024-12-07

2,487 downloads per month
Used in 3 crates

MIT license

32KB
819 lines

dotenv-build

Crates.io Documentation License

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

  1. Ensure you have build scripts enabled via the build configuration in your Cargo.toml
  2. Add dotenv-build as a build dependency
  3. Create a build.rs file that uses dotenv-build to generate cargo: instructions.
  4. Use the env! or option_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();

No runtime deps