#env-file #env #dotenv #environment #env-var #compile-time #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

#653 in Build Utils

Download history 169/week @ 2024-01-07 198/week @ 2024-01-14 215/week @ 2024-01-21 211/week @ 2024-01-28 327/week @ 2024-02-04 252/week @ 2024-02-11 275/week @ 2024-02-18 254/week @ 2024-02-25 171/week @ 2024-03-03 186/week @ 2024-03-10 245/week @ 2024-03-17 219/week @ 2024-03-24 427/week @ 2024-03-31 304/week @ 2024-04-07 341/week @ 2024-04-14 300/week @ 2024-04-21

1,382 downloads per month
Used in packwiz-modlist

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