Skip to content

Introduction

Rust library and command-line tool

Read and edit .env files exactly as written

adotenv is a literal, lossless .env parser and editing library. It keeps every byte of the source, reports precise locations and checks every edit before it returns the result.

Literal values

${HOME}, $USER and $(command) stay text. Nothing is expanded or run unless you ask for it.

Lossless source

Any UTF-8 input prints back byte for byte, including comments, invalid lines, a BOM and mixed line endings.

Precise locations

Every token has a byte span. Positions convert to UTF-8, Unicode scalar or UTF-16 columns.

Verified edits

Set, rename, insert, remove, format and sort. Each result is parsed again and checked before you get it.

Explicit dialects

Portable, Node dotenv, python-dotenv, Docker Compose and Docker presets, plus custom reading rules.

Small core

No required dependencies, no I/O, no unsafe code and a no_std build with alloc.

[dependencies]
adotenv = "1"

The core needs Rust 1.81. File loading, Serde support and command execution are optional features; see Getting started.

let text = "# app\nexport PORT=8080\nURL=\"${HOST}/api\"\n";
let doc = adotenv::parse_strict(text).unwrap();
// Values are literal, and the source prints back unchanged.
assert_eq!(doc.get("PORT"), Some("8080"));
assert_eq!(doc.get("URL"), Some("${HOST}/api"));
assert_eq!(doc.to_string(), text);
// Edits return the verified output and the text edits that produce it.
let edited = doc.set("PORT", "9090").unwrap();
assert_eq!(edited.as_str(), "# app\nexport PORT=9090\nURL=\"${HOST}/api\"\n");

When only the final values are needed, the values-only parser skips the document model:

let values = adotenv::parse_values("A=1\nA=2\nB=3\n").unwrap();
assert_eq!(values.get("A").map(String::as_str), Some("2"));
// Compact storage avoids one map node and two strings per key.
let compact = adotenv::parse_values_compact("A=1\nA=2\nB=3\n").unwrap();
assert_eq!(compact.get("A"), Some("2"));
Crate Use it to Reference
adotenv Parse, inspect, edit, format, sort, compare and resolve .env files. Optional features load files, deserialize with Serde and run commands. API
adotenv-cli Check, format, compare and load files from a shell or a CI job. Commands
adotenv-process Apply a loaded environment to the current process. API
adotenv-config Use values as a config source. API
adotenv-figment Use values as a Figment provider. API
adotenv-lint Project policy checks and verified fixes. API
adotenv-lsp Language server operations for editors. Language server