Skip to content

Add first-class project command environments for agent shell execution #25452

Open

Description

@breathe

Summary

Codex should support first-class project command environments for agent shell execution.

Today, projects that depend on a repo-local development environment have to rely on prompt instructions, shell startup behavior, or repeated command wrappers. That is fragile for agent workflows because the command runner does not have an explicit concept of "commands for this workspace should run inside this project environment."

This request is intentionally not Nix-specific. Nix is a concrete example of the problem, but the feature should be generic enough to support tools such as direnv, devenv, devbox, language version managers, containers, and other project-scoped environment providers.

Problem

Many projects intentionally keep their build tools, test runners, compilers, linters, SDKs, and environment variables out of the global user environment. Instead, the project defines a development shell or activation mechanism.

For example, a project may expect contributors to enter the environment with one of:

nix develop
direnv allow
direnv exec .
devenv shell
devbox shell

When Codex runs shell/tool commands, the model can be told in AGENTS.md to use one of those mechanisms, but that is advisory. It is easy for commands like pytest, cargo test, pnpm test, which rg, or repo scripts to run outside the intended project environment.

This shows up as missing tools, incorrect PATH, inconsistent environment variables, and confusing failures where the underlying command is fine but the command execution context is wrong.

Related issues and why this is different

Several existing issues describe concrete failures in this area. I do not think this request is a duplicate of those reports; rather, those reports are examples of symptoms that become easier to solve if Codex has an explicit project command environment abstraction.

In short: the existing issues are mostly about bugs in inherited shell state, shell snapshots, or platform-specific shell selection. This request is for a generic harness primitive that can address those needs more directly: "commands in this workspace run inside this declared project environment."

Desired behavior

A project or user should be able to declare the command environment for a workspace once. After that, agent shell commands should run inside that environment by default.

Conceptually:

workspace -> activate/materialize project command environment -> run agent commands there

Not:

agent command -> model remembers to rewrite it as a provider-specific wrapper

The active command environment should be visible to the agent/tooling metadata so the model can reason about where commands are being executed.

Why simple command wrapping is not enough

For example, for Nix projects it is tempting to say "just run every command as nix develop -c ...". That is useful as a fallback, but it should not be the core abstraction.

Repeated command wrapping has several problems:

  • quoting and shell-composition edge cases;
  • different behavior for direct exec vs shell snippets;
  • repeated startup cost for heavy environments;
  • unclear distinction between environment activation failures and command failures;
  • noisy sandbox/approval interactions;
  • reliance on the model remembering the correct wrapper every time;
  • poor fit for tools that expect a persistent activated shell/session.

The user intent is not "prefix this string"; it is "this workspace's commands run in this workspace's development environment."

Possible design

Add a project-level command environment concept to Codex config.

For example:

[command_environment]
mode = "session"
provider = "generic"
enter = ["direnv", "exec", "."]

or:

[command_environment]
mode = "session"
provider = "nix"
flake = "."
shell = "default"

The exact config shape is less important than the abstraction. Useful implementation modes might include:

  • session: start or enter a persistent shell/process inside the project environment and run subsequent commands there;
  • env: materialize an environment snapshot once and apply it to subsequent command executions;
  • wrap: provider-specific command wrapping as a fallback when session/env modes are not available.

Provider-specific helpers could exist for common tools, but a generic provider is important so users can integrate custom project environment managers without waiting for Codex-native support.

Acceptance criteria

  • A workspace can declare a default command environment once.
  • Agent shell commands run inside that environment by default.
  • The agent does not need to remember provider-specific command prefixes.
  • The active command environment is visible in command/tool metadata.
  • There is an explicit per-command escape hatch to run outside the project environment.
  • Environment activation/materialization failures are reported separately from failures of commands run inside the environment.
  • The design supports generic providers, with Nix/direnv/devenv/devbox as examples rather than special cases only.
  • Sandbox and approval behavior remains explicit and predictable.
  • Repeated heavy environment startup is avoidable when a session or env-snapshot mode is available.

Example motivating case

A repo uses a Nix flake or direnv/devenv setup so project tools are available only after entering the project environment. In a regular terminal, the user can run:

direnv exec . pytest

or enter the shell once and then run:

pytest
cargo test
rg something

Codex should have a harness-level way to represent that same command execution context, rather than requiring the model to remember to wrap every individual command.

Non-goals

  • This should not require making Nix the only supported environment system.
  • This should not be only a PATH override.
  • This should not depend solely on shell login/profile behavior.
  • This should not require every command to be rewritten as nix develop -c ..., though that may be a useful fallback implementation mode.

Activity

  1. added
    enhancement
    CLI
    config
    tool-calls
    on May 31, 2026
  2. github-actions commented on May 31, 2026

    @github-actions
    Contributor

    Potential duplicates detected. Please review them and close your issue if it is a duplicate.

    Powered by Codex Action

  3. Lightborne commented on Jun 5, 2026

    @Lightborne

    The best approach that I've been able to find is to leverage bash/s non-interactive startup hook. If it detects that the command is being executed by Codex, it basically walks up from the working directory trying to find out setup script and the sources it. The command then sees the needed environment variables.

    This is extremely clunky, obviously. plus it means we pay the startup penalty on every command invocation. not ideal. I really think this is an important feature to add.

  4. breathe commented on Jun 5, 2026

    @breathe
    Author

    The best approach that I've been able to find is to leverage bash/s non-interactive startup hook. If it detects that the command is being executed by Codex, it basically walks up from the working directory trying to find out setup script and the sources it. The command then sees the needed environment variables.

    This is extremely clunky, obviously. plus it means we pay the startup penalty on every command invocation. not ideal. I really think this is an important feature to add.

    hmm -- interesting -- feels quite sneaky .... I agree with you that the sneaky approaches are non-ideal and will tend to hide important information about the process (and side-effects! of the process) from the model ...

    I think this example adds more support to the argument that its important for us to have a good first-class way to do this ... It feels like there's a lot of subpar ways we can try to workaround (at least I haven't found any that aren't subpar)-- and ultimately what we want is to ensure that the model knows what's going on to create and initialize the development env and also is unable to forget to run observational commands within that carefully / purposefully arranged development environment ... Sneakily hiding stuff about the process from the model to try and hack in the guarantee that the model will run observation producing commands in the right way ultimately makes it harder for the model to do things like modifying and debugging the development env flake itself without confusing itself ...

  5. goofansu commented on Jun 6, 2026

    @goofansu

    Currently a stable workaround I use is to set the PATH in project's .codex/config.toml through a script. When you're in terminal, run the script to create the config.toml with the PATH env variable. Then in the Codex app, start a new conversation, and you'll see it uses the new PATH immediately. The nuance is that you may need to run the script again when you changed direnv/devenv configuration in your project.

    Script (commit):

    #!/nix/store/55976waa157aqb1ncbwm6b8sa64b6m3h-bash-5.3p9/bin/bash
    set -o errexit
    set -o nounset
    set -o pipefail
    
    export PATH="/nix/store/y0ha8v4gh5vnwwmp2r0msfbdlvgwv2np-coreutils-9.11/bin:$PATH"
    
    config_dir=".codex"
    config_file="$config_dir/config.toml"
    
    mkdir -p "$config_dir"
    tmp_file=$(mktemp "$config_dir/config.toml.tmp.XXXXXX")
    
    cleanup() {
      rm -f "$tmp_file"
    }
    trap cleanup EXIT
    
    {
      printf '%s\n' '[shell_environment_policy]'
      printf '%s\n' 'inherit = "all"'
      printf 'set = { PATH = "%s" }\n' "$PATH"
    } > "$tmp_file"
    
    mv "$tmp_file" "$config_file"

    Example config.toml:

    [shell_environment_policy]
    inherit = "all"
    set = { PATH = "<the terminal PATH>" }
  6. burdiyan commented on Jun 9, 2026

    @burdiyan

    Setting PATH is not always enough, because when you use something like direnv or mise you also sometimes want to set other environment variables.

    Codex desktop app already has the concept of environments with setup scripts for worktrees. It would be nice to have something like this for local chats too.

  7. TyceHerrman commented on Aug 20, 2026

    @TyceHerrman

    This could also cover workspace-bound STDIO MCP servers. For example, Serena's PATH-resolved language adapters can use a different toolchain if Codex applies mise, direnv, or Nix only to shell commands but starts MCP from the host or app-server environment. Per-server env and env_vars would require duplicating the dynamic project environment.

    Suggested acceptance criteria:

    • Apply the resolved project environment and workspace root to local STDIO MCP startup.
    • Document the precedence of explicit per-server environment settings.
    • Include the project-environment fingerprint in MCP reuse identity and restart the server when it changes.
  8. kevin-neel-oracle-com commented on Sep 2, 2026

    @kevin-neel-oracle-com

    .

    I have a concrete use case for this in large Oracle development environments accessed over SSH from Codex Desktop and VS Code.

    Entering one of these environments can involve expensive process and filesystem setup, not merely exporting variables or modifying PATH. Repeating that setup for every rg, build, or test makes the workflow impractical.

    The desired model is:

    workspace/project → initialized development environment → long-lived command executor → child command

    An exec-server started inside each initialized environment seems like a promising existing primitive. Codex could run normal commands as separate child processes while reusing the established environment context.

    Important requirements are:

    • Initialize the environment once and avoid repeated startup cost.
    • Preserve process/filesystem context, not just an environment-variable snapshot.
    • Support multiple independent environments on the same SSH VM.
    • Keep each workspace or thread associated with the correct environment.
    • Provide consistent behavior across Codex Desktop, VS Code, and ideally the CLI.
    • Report environment setup failures separately from command failures.
    • Make the active environment visible to tooling, with an explicit escape hatch for running outside it.

    This seems complementary to #25454’s project/workspace organization under one SSH host and the environment contract questions raised in discussion #26901. The Oracle case may be one example of a broader need for persistent, project-scoped command environments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CLIconfigenhancementtool-calls

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions