feat: support dotenv

This commit is contained in:
Michael Bolin
2025-07-22 10:19:42 -07:00
parent d5809ef6ef
commit dfa9a44202
4 changed files with 15 additions and 1 deletions

7
codex-rs/Cargo.lock generated
View File

@@ -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"

View File

@@ -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",

View File

@@ -99,6 +99,12 @@ fn main() -> anyhow::Result<()> {
}
async fn cli_main(codex_linux_sandbox_exe: Option<PathBuf>) -> 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 {

View File

@@ -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<PathBuf> {
pub fn find_codex_home() -> std::io::Result<PathBuf> {
// 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") {