Environment variables and .env file loading.
Config-driven applications out of the box. Load from .env files in development, from platform environment in production. Built on Rust's std::env and dotenvy.
Every function in the std.env module.
Reads key=value pairs from a .env file in the current directory and loads them into the environment. Silently skips if the file doesn't exist — perfect for production where vars come from the platform.
env.load_dotenv() // loads .env if present, no-op otherwise
Returns the value of an environment variable, or an empty string if not set. Use for optional configuration with fallback defaults.
app_name := env.get("APP_NAME") // "" if not set
log_level := env.get("LOG_LEVEL") // use default if empty
Returns the value of an environment variable or panics with a clear error if not set. Use for configuration the app cannot run without.
port := env.require("PORT") // panics: "Required env var PORT not set"
secret := env.require("JWT_SECRET") // app won't start without it
db_url := env.require("DATABASE_URL") // must know where the DB is
Returns all environment variables as a key-value map. Useful for debugging configuration in development — never expose in production as it includes sensitive values.
all_vars := env.all()
// Returns: {"PATH": "/usr/bin:...", "PORT": "3000", "HOME": "/root", ...}
Config-driven application with required and optional variables.
module main
use std.http
use std.env
fn main() {
env.load_dotenv() // load .env file
port := env.require("PORT") // required — panics if missing
database_url := env.require("DATABASE_URL") // required
app_name := env.get("APP_NAME") // optional — "" if not set
log_level := env.get("LOG_LEVEL") // optional
server := http.new(port: 3000)
server.get("/config") => fn(req) -> Response {
Response.json({
app_name: "My App",
environment: "development",
port: "3000"
})
}
// Debug endpoint — dev only!
server.get("/env") => fn(req) -> Response {
all_vars := env.all()
Response.json({ variables: all_vars })
}
server.start()
}
Standard key=value pairs, one per line.
# .env — local development configuration
PORT=3000
DATABASE_URL=sqlite://app.db
APP_NAME=My AID App
APP_ENV=development
LOG_LEVEL=debug
JWT_SECRET=my-secret-key
API_KEY=sk-abc123
What the compiler produces for environment operations.
Transpiled Output (simplified)use dotenvy::dotenv;
use std::env;
// env.load_dotenv()
dotenv().ok(); // .ok() silently ignores missing .env
// env.get("APP_NAME")
let app_name = env::var("APP_NAME").unwrap_or_default();
// env.require("PORT")
let port = env::var("PORT")
.expect("Required env var PORT not set");
// env.all()
let all_vars: HashMap<String, String> = env::vars().collect();