{ config, lib, pkgs, ... }: let cfg = config.codex; profile = "agent-environments"; settingsFile = (pkgs.formats.toml { }).generate "codex-${profile}.config.toml" cfg.settings; in { options.codex = { enable = lib.mkEnableOption "Codex"; package = lib.mkOption { type = lib.types.package; description = "The Codex package; must provide bin/codex."; }; settings = lib.mkOption { type = (pkgs.formats.toml { }).type; default = { }; description = "Settings layered over the account's config.toml as the '${profile}' profile."; }; }; config.build.agents.codex = lib.mkIf cfg.enable { exe = lib.getExe' cfg.package "codex"; stateEnvVar = "CODEX_HOME"; instructionsFile = "AGENTS.md"; files = lib.optionalAttrs (cfg.settings != { }) { "${profile}.config.toml" = settingsFile; }; flags = lib.optionals (cfg.settings != { }) [ "--profile" profile ]; }; }