nautechsystems/nautilus_trader · error
environment variable '{key}' must be set
Error message
environment variable '{key}' must be set What it means
`get_env_var` reads an environment variable and returns a descriptive error when it is not set at all (std::env::VarError::NotPresent). The library throws it because a required configuration value can only come from the environment and proceeding without it would be undefined.
Source
Thrown at crates/core/src/env.rs:30
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------
//! Cross-platform environment variable utilities.
//!
//! This module provides functions for safely accessing environment variables
//! with proper error handling.
/// Returns the value of the environment variable for the given `key`.
///
/// # Errors
///
/// Returns an error if the environment variable is not set or is not valid Unicode.
pub fn get_env_var(key: &str) -> anyhow::Result<String> {
match std::env::var(key) {
Ok(var) => Ok(var),
Err(std::env::VarError::NotPresent) => {
anyhow::bail!("environment variable '{key}' must be set")
}
Err(std::env::VarError::NotUnicode(_)) => {
anyhow::bail!("environment variable '{key}' is not valid Unicode")
}
}
}
/// Returns the provided `value` if `Some`, otherwise falls back to reading
/// the environment variable for the given `key`.
///
/// Only attempts to read the environment variable when `value` is `None`,
/// avoiding unnecessary environment variable lookups and errors.
///
/// # Errors
///
/// Returns an error if `value` is `None` and the environment variable is not set.
pub fn get_or_env_var(value: Option<String>, key: &str) -> anyhow::Result<String> {
match value {View on GitHub (pinned to 18893faf8b)
Solutions
- Export the environment variable before running: export KEY=value
- Add it to your .env file / shell profile / CI secrets so it is always present
- Check for typos in the variable name shown in the error message
- Use `get_or_env_var` with a default if the value is genuinely optional
Example fix
// before
let path = get_env_var("NAUTILUS_PATH")?; // panics-free error if unset
// after
let path = get_or_env_var("NAUTILUS_PATH", "/tmp/nautilus")?; // or ensure: export NAUTILUS_PATH=... Defensive patterns
Strategy: validation
Validate before calling
let val = std::env::var("MY_KEY");
if val.is_err() {
eprintln!("MY_KEY must be set; see deployment docs");
std::process::exit(1);
} Type guard
fn env_var_set(key: &str) -> bool {
std::env::var_os(key).is_some()
} Try / catch
match get_env_var("MY_KEY") {
Ok(v) => configure(v),
Err(e) => anyhow::bail!("startup config incomplete: {e}"), // fail fast with context
} Prevention
- Maintain a .env.example listing every required variable
- Validate all env vars at startup, before doing any work
- Use get_or_env_var with defaults for genuinely optional values
- Check CI/deployment secrets match the variable names in code
When it happens
Trigger: Calling `get_env_var("KEY")` (directly, or via `from_env`/`main` config loading) when `KEY` was never exported in the process environment.
Common situations: Running the app without a .env file loaded; forgetting to export a variable in the shell or CI; typo in the variable name (e.g. NAUTILUS_PATH vs NAUTILUSPATH); variable defined in one environment (prod) but not another (local dev container).
Understand the failure class
Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.
Related errors
- Missing environment variable: {key_env}
- Missing environment variable: {secret_env}
- Unsupported blockchain {blockchain} for RPC connection
- Kraken Spot does not support the demo environment
- Redis config error: username supplied without password. Eith
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/d9e1876234d1da02.
Report an issue: GitHub.