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

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

#686 in Build Utils

Download history 239/week @ 2024-03-14 218/week @ 2024-03-21 349/week @ 2024-03-28 383/week @ 2024-04-04 324/week @ 2024-04-11 287/week @ 2024-04-18 257/week @ 2024-04-25 173/week @ 2024-05-02 157/week @ 2024-05-09 168/week @ 2024-05-16 232/week @ 2024-05-23 200/week @ 2024-05-30 250/week @ 2024-06-06 385/week @ 2024-06-13 316/week @ 2024-06-20 2592/week @ 2024-06-27

3,578 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