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
Section titled “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()?;adotenv-cli run does the same from a shell. Use adotenv-process only when
code you do not control reads the process environment.
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, 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), and
apply_compact_entries / validate_compact_entries handle CompactEntries
(from parse_entries_compact or load_cascade_entries_compact). Bare
declarations (None values kept under BareKeys::Preserve) are skipped without
setting or removing any process variable, matching apply, while bare keys
converted to Some("") under BareKeys::Empty are validated and set. To apply
resolved values, resolve them with LoadedEnvironment::resolve_contextual and
set them yourself.
Validate
Section titled “Validate”validate(&LoadedEnvironment), validate_compact(&CompactValues) and
validate_compact_entries(&CompactEntries) run the same checks without changing
anything. All three 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:
| Variant | Meaning |
|---|---|
EmptyKey |
A 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.