diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 9b4a4e32d4..a48963ddf4 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -636,6 +636,7 @@ dependencies = [ "codex-login", "codex-mcp-server", "codex-tui", + "dotenvy", "serde_json", "tokio", "tracing", @@ -1272,6 +1273,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" + [[package]] name = "dupe" version = "0.9.1" diff --git a/codex-rs/cli/Cargo.toml b/codex-rs/cli/Cargo.toml index 943788157b..3e2d191a2d 100644 --- a/codex-rs/cli/Cargo.toml +++ b/codex-rs/cli/Cargo.toml @@ -26,6 +26,7 @@ codex-login = { path = "../login" } codex-linux-sandbox = { path = "../linux-sandbox" } codex-mcp-server = { path = "../mcp-server" } codex-tui = { path = "../tui" } +dotenvy = "0.15.7" serde_json = "1" tokio = { version = "1", features = [ "io-std", diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index 7e23782d75..3dc28c9155 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -99,6 +99,12 @@ fn main() -> anyhow::Result<()> { } async fn cli_main(codex_linux_sandbox_exe: Option) -> anyhow::Result<()> { + // Load env vars from ~/.codex/.env and `$(pwd)/.env`. + if let Ok(codex_home) = codex_core::config::find_codex_home() { + dotenvy::from_path(codex_home.join(".env")).ok(); + } + dotenvy::dotenv().ok(); + let cli = MultitoolCli::parse(); match cli.subcommand { diff --git a/codex-rs/core/src/config.rs b/codex-rs/core/src/config.rs index f1d0dd9d60..51554b6a21 100644 --- a/codex-rs/core/src/config.rs +++ b/codex-rs/core/src/config.rs @@ -539,7 +539,7 @@ fn default_model() -> String { /// function will Err if the path does not exist. /// - If `CODEX_HOME` is not set, this function does not verify that the /// directory exists. -fn find_codex_home() -> std::io::Result { +pub fn find_codex_home() -> std::io::Result { // Honor the `CODEX_HOME` environment variable when it is set to allow users // (and tests) to override the default location. if let Ok(val) = std::env::var("CODEX_HOME") {