132 lines
4.5 KiB
Nix
132 lines
4.5 KiB
Nix
{
|
|
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" ];
|
|
};
|
|
})
|
|
];
|
|
}
|