{"record":{"id":"bcd0e9810d8f7f04","repo":"nautechsystems/nautilus_trader","slug":"username-not-set-nor-available-in-env-tws-username","errorCode":null,"errorMessage":"username not set nor available in env TWS_USERNAME","messagePattern":"username not set nor available in env TWS_USERNAME","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"critical","filePath":"crates/adapters/interactive_brokers/src/gateway/dockerized.rs","lineNumber":180,"sourceCode":"    }\n\n    /// Create a new DockerizedIBGateway from configuration.\n    ///\n    /// # Arguments\n    ///\n    /// * `config` - Configuration for the gateway\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if:\n    /// - Username or password is not provided and not available in environment variables\n    /// - Docker client creation fails\n    pub fn new(mut config: DockerizedIBGatewayConfig) -> anyhow::Result<Self> {\n        let username = config\n            .username\n            .take()\n            .or_else(|| std::env::var(\"TWS_USERNAME\").ok().map(SecretString::from))\n            .ok_or_else(|| anyhow::anyhow!(\"username not set nor available in env TWS_USERNAME\"))?;\n\n        let password = config\n            .password\n            .take()\n            .or_else(|| std::env::var(\"TWS_PASSWORD\").ok().map(SecretString::from))\n            .ok_or_else(|| anyhow::anyhow!(\"password not set nor available in env TWS_PASSWORD\"))?;\n\n        // Connect to Docker\n        let docker = Docker::connect_with_local_defaults().context(\n            \"Failed to connect to the local Docker daemon. Ensure Docker is running and the local Docker socket is available\",\n        )?;\n\n        // Determine port based on trading mode\n        let mode_str = match config.trading_mode {\n            crate::config::TradingMode::Paper => \"Paper\",\n            crate::config::TradingMode::Live => \"Live\",\n        };\n        let port = Self::host_port_for_mode(config.trading_mode);","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/interactive_brokers/src/gateway/dockerized.rs#L162-L198","documentation":"DockerizedIBGateway::new requires IB Gateway credentials. It takes username from the DockerizedIBGatewayConfig and falls back to the TWS_USERNAME environment variable; if both are absent it returns this error rather than constructing the gateway manager. Credentials are held as SecretString and are never read later.","triggerScenarios":"Calling DockerizedIBGateway::new with config.username == None while TWS_USERNAME is not set in the process environment.","commonSituations":"Running the node under systemd/Docker/K8s where the shell profile exporting TWS_USERNAME is not loaded; forgetting the env var in CI; building the config programmatically and omitting the username field.","solutions":["Set TWS_USERNAME in the environment before starting the process (e.g. `export TWS_USERNAME=...` or pass it via Docker/systemd EnvFile).","Set `username` explicitly on DockerizedIBGatewayConfig.","Load credentials from a .env file or secret manager into the process environment at startup.","Verify with a quick check that the env var is visible to the exact process running the gateway."],"exampleFix":"// before\nlet gateway = DockerizedIBGateway::new(DockerizedIBGatewayConfig::default())?;\n// after\nlet gateway = DockerizedIBGateway::new(DockerizedIBGatewayConfig {\n    username: Some(SecretString::from(\"my_user\")),\n    ..Default::default()\n})?;","handlingStrategy":"validation","validationCode":"if std::env::var(\"TWS_USERNAME\").is_err() {\n    panic!(\"TWS_USERNAME must be set (or pass username in DockerizedIBGatewayConfig) before starting the gateway\");\n}","typeGuard":null,"tryCatchPattern":"match DockerizedIBGateway::new(config) {\n    Ok(gw) => (),\n    Err(e) if e.to_string().contains(\"username not set nor available in env TWS_USERNAME\") => {\n        return Err(anyhow!(\"gateway credentials missing: set TWS_USERNAME or config.username\"));\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Export TWS_USERNAME/TWS_PASSWORD in the service unit, container env, or CI secrets — not just your shell profile","Fail fast at process startup by checking env vars before building the node","Prefer explicit config fields over env fallbacks in production deployments"],"tags":["credentials","env-var","docker","gateway"],"backgroundTag":"missing-env-var","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}