Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Process environment

adotenv-process applies a loaded environment to the current process. It is a separate crate because changing the process environment is unsafe: the environment is shared by every thread, and on some platforms reading it while another thread writes it is undefined behavior. The core adotenv crate forbids unsafe code and never changes the environment.

[dependencies]
adotenv = { version = "1", features = ["load"] }
adotenv-process = "1"

The crate has no features of its own and needs Rust 1.81 or later.

Prefer passing values explicitly

Most programs do not need to change their own environment. Read values from LoadedEnvironment::get or values(), deserialize them with the serde feature, or pass them to a child process:

use std::process::Command;

let loaded = adotenv::load::load_cascade(".", "production")?;
let status = Command::new("my-server").envs(loaded.values()).status()?;
let _ = status;
Ok::<(), Box<dyn std::error::Error>>(())

adotenv-cli run does the same from a shell. Use adotenv-process only when code you do not control reads the process environment.

Apply

use adotenv::load::load_cascade;
use adotenv_process::apply;

let loaded = load_cascade("/srv/my-app", "production")?;

// SAFETY: called at startup, before any other thread exists.
unsafe { apply(&loaded)? };

apply(&LoadedEnvironment) sets every loaded key to its loaded value:

  • It validates every key and value before the first change, so a rejected environment changes nothing.
  • It sets the loaded keys in sorted order. A loaded key replaces a variable of the same name. Variables with other names are kept, and nothing is removed.
  • Values are set as loaded. References such as ${HOME} are not expanded.
  • Changes cannot be rolled back.

The caller must guarantee that no other thread reads or writes the process environment while apply runs. In practice, call it at the start of main, before starting threads or an async runtime.

apply takes a LoadedEnvironment, and apply_compact / validate_compact provide the same validation and mutation semantics for CompactValues (such as values from load_cascade_values_compact or load_values_compact). To apply resolved values, resolve them with LoadedEnvironment::resolve_contextual and set them yourself.

Validate

validate(&LoadedEnvironment) and validate_compact(&CompactValues) run the same checks without changing anything. Both are safe to call from any thread.

ApplyError reports the first problem, in key order. These are the inputs for which std::env::set_var may panic:

VariantMeaning
EmptyKeyA key is empty. The loaders skip empty keys, so this is a safeguard.
InvalidKey { key }A key contains NUL or =.
InvalidValue { key }A value contains NUL.
DuplicateKey { key }Windows: two keys differ only by case.
ValueTooLong { key, limit }Windows: a value exceeds 32,767 UTF-16 code units.

The error names the key but never includes a value.