From 8c6897e66d1645de749657d9362f8515f6a78383 Mon Sep 17 00:00:00 2001 From: Marmel Coder Date: Thu, 12 Nov 2026 16:49:26 +0300 Subject: [PATCH] feat: add codex-oauth-proxy Home Manager service module --- modules/codex-oauth-proxy-hm.nix | 131 +++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 modules/codex-oauth-proxy-hm.nix diff --git a/modules/codex-oauth-proxy-hm.nix b/modules/codex-oauth-proxy-hm.nix new file mode 100644 index 0000000..7aff14b --- /dev/null +++ b/modules/codex-oauth-proxy-hm.nix @@ -0,0 +1,131 @@ +{ + config, + lib, + pkgs, + ... +}: +let + cfg = config.programs.codex-oauth-proxy; + marker = ".codex-oauth-proxy-hm-managed"; + unit = "codex-oauth-proxy.service"; + unitPath = "${lib.removePrefix config.home.homeDirectory config.xdg.configHome}/systemd/user/${unit}"; + systemctl = "${pkgs.coreutils}/bin/timeout 30s ${lib.escapeShellArg config.systemd.user.systemctlPath} --user"; + managerSetup = '' + export XDG_RUNTIME_DIR="''${XDG_RUNTIME_DIR:-/run/user/$(id -u)}" + case "$(${systemctl} is-system-running 2>/dev/null || true)" in + running|degraded) ;; + *) + echo "codex-oauth-proxy: user systemd manager is unavailable; lifecycle activation failed." >&2 + exit 1 + ;; + esac + ''; +in +{ + options.programs.codex-oauth-proxy = { + enable = lib.mkEnableOption "the codex-oauth-proxy systemd user service"; + + package = lib.mkOption { + type = lib.types.package; + default = pkgs.millerson-nix-overlay.codex-oauth-proxy or pkgs.codex-oauth-proxy; + defaultText = lib.literalExpression "pkgs.millerson-nix-overlay.codex-oauth-proxy or pkgs.codex-oauth-proxy"; + description = "The codex-oauth-proxy package to use."; + }; + + admin_api_key = lib.mkOption { + type = lib.types.nullOr ( + lib.types.addCheck lib.types.str (value: builtins.match "[ -~]*" value != null) + ); + default = null; + description = '' + Optional printable ASCII value for this service's ADMIN_API_KEY environment variable. + Null omits the assignment. An empty string supplies an empty value. + This value is public in the Nix store and through systemd introspection. + Do not use this option for a confidential production key. + ''; + }; + }; + + config = lib.mkMerge [ + { + home.activation.codexOauthProxyStop = + lib.hm.dag.entryBetween [ "linkGeneration" ] [ "writeBoundary" ] + '' + ( + : + ${lib.optionalString (!cfg.enable) '' + if [[ -n "''${oldGenPath:-}" && -f "$oldGenPath/home-files/${marker}" ]]; then + ${managerSetup} + loadState=$(${systemctl} show --property=LoadState --value ${unit}) || exit $? + if [[ "$loadState" != not-found ]]; then + run ${systemctl} stop ${unit} + fi + fi + ''} + ) + ''; + + home.activation.codexOauthProxyStart = lib.hm.dag.entryAfter [ "linkGeneration" "reloadSystemd" ] '' + ( + if ${ + if cfg.enable then + "true" + else + ''[[ -n "''${oldGenPath:-}" && -f "$oldGenPath/home-files/${marker}" ]]'' + }; then + ${managerSetup} + run ${systemctl} daemon-reload + ${lib.optionalString cfg.enable '' + action=start + ${lib.optionalString + (builtins.elem config.systemd.user.startServices [ + false + "suggest" + ]) + '' + if [[ -n "''${oldGenPath:-}" && -f "$oldGenPath/home-files/${marker}" ]] && + ! cmp -s "$oldGenPath/home-files"${lib.escapeShellArg unitPath} "$newGenPath/home-files"${lib.escapeShellArg unitPath}; then + action=restart + fi + '' + } + run ${systemctl} "$action" ${unit} + ''} + fi + ) + ''; + } + (lib.mkIf cfg.enable { + assertions = [ + { + assertion = pkgs.stdenv.hostPlatform.isLinux; + message = "programs.codex-oauth-proxy requires a Linux systemd user manager."; + } + ]; + + home.packages = [ cfg.package ]; + home.file.${marker}.text = "codex-oauth-proxy Home Manager ownership\n"; + + systemd.user.services.codex-oauth-proxy = { + Unit.Description = "Codex OAuth proxy"; + Service = { + Type = "simple"; + ExecStart = "${lib.getExe cfg.package} --creds-store=auto --creds-path=%S/codex-oauth-proxy/auth.json"; + StateDirectory = "codex-oauth-proxy"; + StateDirectoryMode = "0700"; + UMask = "0077"; + Restart = "on-failure"; + RestartSec = 5; + StandardOutput = "null"; + StandardError = "null"; + } + // lib.optionalAttrs (cfg.admin_api_key != null) { + Environment = [ + (builtins.replaceStrings [ "%" ] [ "%%" ] (builtins.toJSON ("ADMIN_API_KEY=" + cfg.admin_api_key))) + ]; + }; + Install.WantedBy = [ "default.target" ]; + }; + }) + ]; +}