nautechsystems/nautilus_trader · critical · anyhow::Error
password not set nor available in env TWS_PASSWORD
Error message
password not set nor available in env TWS_PASSWORD
What it means
DockerizedIBGateway::new requires the IB Gateway password, taken from DockerizedIBGatewayConfig.password with fallback to the TWS_PASSWORD environment variable. If neither is set, construction fails with this error so no gateway container is ever started without credentials.
Source
Thrown at crates/adapters/interactive_brokers/src/gateway/dockerized.rs:186
/// * `config` - Configuration for the gateway
///
/// # Errors
///
/// Returns an error if:
/// - Username or password is not provided and not available in environment variables
/// - Docker client creation fails
pub fn new(mut config: DockerizedIBGatewayConfig) -> anyhow::Result<Self> {
let username = config
.username
.take()
.or_else(|| std::env::var("TWS_USERNAME").ok().map(SecretString::from))
.ok_or_else(|| anyhow::anyhow!("username not set nor available in env TWS_USERNAME"))?;
let password = config
.password
.take()
.or_else(|| std::env::var("TWS_PASSWORD").ok().map(SecretString::from))
.ok_or_else(|| anyhow::anyhow!("password not set nor available in env TWS_PASSWORD"))?;
// Connect to Docker
let docker = Docker::connect_with_local_defaults().context(
"Failed to connect to the local Docker daemon. Ensure Docker is running and the local Docker socket is available",
)?;
// Determine port based on trading mode
let mode_str = match config.trading_mode {
crate::config::TradingMode::Paper => "Paper",
crate::config::TradingMode::Live => "Live",
};
let port = Self::host_port_for_mode(config.trading_mode);
// Generate container name
let container_name = format!("{}-{}", Self::CONTAINER_NAME, mode_str).to_lowercase();
Ok(Self {
config,View on GitHub (pinned to 18893faf8b)
Solutions
- Set TWS_PASSWORD in the environment before starting the process.
- Set `password` explicitly on DockerizedIBGatewayConfig.
- Inject the password via your secret manager / CI secrets into the process environment.
- Double-check both TWS_USERNAME and TWS_PASSWORD are present — construction requires both.
Example fix
// before
let gateway = DockerizedIBGateway::new(DockerizedIBGatewayConfig::default())?;
// after
let gateway = DockerizedIBGateway::new(DockerizedIBGatewayConfig {
password: Some(SecretString::from("my_password")),
..Default::default()
})?; Defensive patterns
Strategy: validation
Validate before calling
if std::env::var("TWS_PASSWORD").is_err() {
panic!("TWS_PASSWORD must be set (or pass password in DockerizedIBGatewayConfig) before starting the gateway");
} Try / catch
match DockerizedIBGateway::new(config) {
Ok(gw) => (),
Err(e) if e.to_string().contains("password not set nor available in env TWS_PASSWORD") => {
return Err(anyhow!("gateway credentials missing: set TWS_PASSWORD or config.password"));
}
Err(e) => return Err(e),
} Prevention
- Inject both TWS_USERNAME and TWS_PASSWORD together from your secret store
- Never rely on interactive shell environment; set secrets in the deployment manifest
- Check credential presence in a startup smoke test before live trading
When it happens
Trigger: Calling DockerizedIBGateway::new with config.password == None while TWS_PASSWORD is not present in the process environment.
Common situations: Setting TWS_USERNAME but forgetting TWS_PASSWORD; running in CI where only one secret was injected; password defined in a secret manager not wired into the process environment.
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
- username not set nor available in env TWS_USERNAME
- Gateway `{}` not ready after {} seconds
- Redis config error: username supplied without password. Eith
- Missing Betfair credentials in config and environment
- Invalid Betfair credentials: username provided but password
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/174a8a87929819b9.
Report an issue: GitHub.