Skip to content

Commit d6c4083

Browse files
authored
feat: support dotenv (including ~/.codex/.env) (openai#1653)
This PR adds a `load_dotenv()` helper function to the `codex-common` crate that is available when the `cli` feature is enabled. The function uses [`dotenvy`](https://crates.io/crates/dotenvy) to update the environment from: - `$CODEX_HOME/.env` - `$(pwd)/.env` To test: - ran `printenv OPENAI_API_KEY` to verify the env var exists in my environment - ran `just codex exec hello` to verify the CLI uses my `OPENAI_API_KEY` - ran `unset OPENAI_API_KEY` - ran `just codex exec hello` again and got **ERROR: Missing environment variable: `OPENAI_API_KEY`**, as expected - created `~/.codex/.env` and added `OPENAI_API_KEY=sk-proj-...` (also ran `chmod 400 ~/.codex/.env` for good measure) - ran `just codex exec hello` again and it worked, verifying it picked up `OPENAI_API_KEY` from `~/.codex/.env` Note this functionality was available in the TypeScript CLI: openai#122 and was recently requested over on openai#1262 (comment).
1 parent 3ef544f commit d6c4083

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

codex-rs/Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codex-rs/core/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ fn default_model() -> String {
561561
/// function will Err if the path does not exist.
562562
/// - If `CODEX_HOME` is not set, this function does not verify that the
563563
/// directory exists.
564-
fn find_codex_home() -> std::io::Result<PathBuf> {
564+
pub fn find_codex_home() -> std::io::Result<PathBuf> {
565565
// Honor the `CODEX_HOME` environment variable when it is set to allow users
566566
// (and tests) to override the default location.
567567
if let Ok(val) = std::env::var("CODEX_HOME") {

codex-rs/linux-sandbox/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ workspace = true
1717
[dependencies]
1818
anyhow = "1"
1919
clap = { version = "4", features = ["derive"] }
20+
codex-common = { path = "../common", features = ["cli"] }
2021
codex-core = { path = "../core" }
22+
dotenvy = "0.15.7"
2123
tokio = { version = "1", features = ["rt-multi-thread"] }
2224

2325
[dev-dependencies]

codex-rs/linux-sandbox/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ where
4343
crate::run_main();
4444
}
4545

46+
// This modifies the environment, which is not thread-safe, so do this
47+
// before creating any threads/the Tokio runtime.
48+
load_dotenv();
49+
4650
// Regular invocation – create a Tokio runtime and execute the provided
4751
// async entry-point.
4852
let runtime = tokio::runtime::Runtime::new()?;
@@ -61,3 +65,11 @@ where
6165
pub fn run_main() -> ! {
6266
panic!("codex-linux-sandbox is only supported on Linux");
6367
}
68+
69+
/// Load env vars from ~/.codex/.env and `$(pwd)/.env`.
70+
fn load_dotenv() {
71+
if let Ok(codex_home) = codex_core::config::find_codex_home() {
72+
dotenvy::from_path(codex_home.join(".env")).ok();
73+
}
74+
dotenvy::dotenv().ok();
75+
}

0 commit comments

Comments
 (0)