diff --git a/nix/host-modules/deploy.nix b/nix/host-modules/deploy.nix index 55c7c361a..781d109ff 100644 --- a/nix/host-modules/deploy.nix +++ b/nix/host-modules/deploy.nix @@ -790,6 +790,9 @@ in and `reviewClassifierClientKeyFile`, minted on the store's host). The key is never written for you: until the operator writes it the service runs and every run fails with "no API key…". + + Set {option}`services.hyperhive.deploy.review-classifier.runtime` to + `k8s` to run it as a pod in the host's cluster instead. ''; }; @@ -810,6 +813,9 @@ in host has the minter's store identity (the pair {option}`services.hyperhive.deploy.bao.claudeLoginClientCertFile` and `claudeLoginClientKeyFile`, minted on the store's host). + + Set {option}`services.hyperhive.deploy.claude-login.runtime` to `k8s` + to run it as a pod in the host's cluster instead. ''; }; diff --git a/nix/host-modules/k8s/render.nix b/nix/host-modules/k8s/render.nix index b399f62b9..4450f65bf 100644 --- a/nix/host-modules/k8s/render.nix +++ b/nix/host-modules/k8s/render.nix @@ -117,6 +117,32 @@ let hostUnit = "container@swarm-stt.service"; }; }; + swarm-review-classifier = { + inherit (deployCfg.review-classifier) enable package; + unit = deployCfg.review-classifier.unit; + kind = "Deployment"; + healthEnv = "SWARM_REVIEW_CLASSIFIER_HEALTH_ADDR"; + etcFiles = [ ]; + publicCredentials.review-classifier-bao-server-ca = baoServerCaKey; + otlp = "hive"; + switch = { + inherit (deployCfg.review-classifier) runtime; + hostUnit = "container@swarm-review-classifier.service"; + }; + }; + swarm-claude-login = { + inherit (deployCfg.claude-login) enable package; + unit = deployCfg.claude-login.unit; + kind = "Deployment"; + healthEnv = "SWARM_CLAUDE_LOGIN_HEALTH_ADDR"; + etcFiles = [ ]; + publicCredentials.claude-login-bao-server-ca = baoServerCaKey; + otlp = "hive"; + switch = { + inherit (deployCfg.claude-login) runtime; + hostUnit = "container@swarm-claude-login.service"; + }; + }; swarm-ci-metrics = { inherit (deployCfg.ci-metrics) enable package; unit = deployCfg.ci-metrics.unit; diff --git a/nix/host-modules/swarm-bao-readers.nix b/nix/host-modules/swarm-bao-readers.nix index 8971cddfd..5d9b7ff97 100644 --- a/nix/host-modules/swarm-bao-readers.nix +++ b/nix/host-modules/swarm-bao-readers.nix @@ -412,7 +412,12 @@ let # principal's name, policy and token lifetimes, and its ServiceAccount is # named after it (../k8s/render.nix renders the pod under that account). k8sPrincipals = - notifyRouterReaders ++ sttReaders ++ ciMetricsReaders ++ [ { name = controllerPolicyName; } ]; + notifyRouterReaders + ++ sttReaders + ++ reviewClassifierReaders + ++ claudeLoginWriters + ++ ciMetricsReaders + ++ [ { name = controllerPolicyName; } ]; # The jwt role of one principal: the token of exactly its ServiceAccount, for # this store's audience. Both the subject and the audience are bound, so a diff --git a/nix/host-modules/swarm-claude-login.nix b/nix/host-modules/swarm-claude-login.nix index 74e9d1891..54c554ec9 100644 --- a/nix/host-modules/swarm-claude-login.nix +++ b/nix/host-modules/swarm-claude-login.nix @@ -5,6 +5,8 @@ # and the pasted code over the queue, and the daemon runs `claude auth login` # and writes the credentials to that agent's store leaf. # docs/4-implementation/swarm/claude-login.md has the design. +# With `deploy.claude-login.runtime = "k8s"` it runs as a pod instead, rendered +# from the unit below (docs/4-implementation/k8s/runtime.md). # # Its own network namespace on the bridge, as ./swarm-review-classifier.nix # has: it reaches the swarm services by name and nothing on the host's @@ -90,6 +92,130 @@ let forwarderCredential = id: "/run/credentials/${forwarderUnit}.service/${id}"; privateNetwork = true; + + onK8s = cfg.runtime == "k8s"; + + # The module the container runs: its own NixOS system on the host's bridge, + # or, with the service on the cluster, only the source of the unit the pod is + # rendered from (`deploy.claude-login.unit`). + containerConfig = + { pkgs, lib, ... }: + { + imports = [ + ../container-modules/swarm-container.nix + ../container-modules/swarm-journal-forwarder.nix + caBundleModule + ]; + + services.hyperhive.swarmContainer = { + inherit privateNetwork; + writesOwnResolvConf = false; + }; + networking.nameservers = [ networkCfg.bridgeIp ]; + networking.interfaces.eth0.useDHCP = true; + + # `claude-code` is unfree, and this container evaluates its own + # nixpkgs, so the host's allowance does not reach here. + nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "claude-code" ]; + + users.users.${unit} = { + isSystemUser = true; + group = unit; + }; + users.groups.${unit} = { }; + + # Authenticated as the daemon's own OIDC client, whose secret its + # store identity already reads: one identity for the whole container. + # That identity is a system credential, so the fetch unit loads it + # too; the daemon's `LoadCredential` below is that unit's alone. + services.hyperhive.swarmJournalForwarder = { + enable = true; + serviceName = "claude-login"; + endpoint = forwarderEndpoint; + inherit clientId; + tokenUrl = "${autheliaCfg.url}/api/oidc/token"; + telemetryPort = forwarderTelemetryPort; + otlpMetricsPort = forwarderOtlpPort; + store = { + package = baoDeploy.package; + addr = "https://${baoCfg.domain}:${toString baoCfg.port}"; + secretPath = "secret/swarm/services/${clientId}/oidc/client"; + clientCertFile = forwarderCredential "claude-login-bao-cert"; + clientKeyFile = forwarderCredential "claude-login-bao-key"; + caFile = + if storeFiles ? claude-login-bao-server-ca then + forwarderCredential "claude-login-bao-server-ca" + else + null; + }; + }; + systemd.services.${forwarderUnit}.serviceConfig.LoadCredential = lib.attrNames storeFiles; + + systemd.services.${unit} = { + description = "log agents in to claude for the swarm UI"; + wantedBy = [ "multi-user.target" ]; + wants = [ "network-online.target" ]; + after = [ "network-online.target" ]; + environment = { + BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}"; + BAO_CLIENT_CERT = credential "claude-login-bao-cert"; + BAO_CLIENT_KEY = credential "claude-login-bao-key"; + SWARM_CLAUDE_LOGIN_NATS_URL = "tls://${natsCfg.domain}:${toString natsCfg.port}"; + SWARM_CLAUDE_LOGIN_OIDC_TOKEN_ENDPOINT = "${autheliaCfg.url}/api/oidc/token"; + SWARM_CLAUDE_LOGIN_OIDC_CLIENT_ID = clientId; + SWARM_CLAUDE_LOGIN_CLAUDE = "${claudeCode pkgs.claude-code}/bin/claude"; + OTEL_EXPORTER_OTLP_ENDPOINT = "http://127.0.0.1:${toString forwarderOtlpPort}"; + OTEL_EXPORTER_OTLP_PROTOCOL = "http/protobuf"; + OTEL_RESOURCE_ATTRIBUTES = "service.name=hyperhive-claude-login,swarm=${ + if hyperhiveCfg.swarm.name == null then "unknown" else hyperhiveCfg.swarm.name + }"; + } + // lib.optionalAttrs (storeFiles ? claude-login-bao-server-ca) { + BAO_CACERT = credential "claude-login-bao-server-ca"; + } + // lib.optionalAttrs caTrust.useSelfSigned { + SWARM_CLAUDE_LOGIN_OIDC_CA_FILE = caTrust.bundlePathFor machine; + }; + serviceConfig = { + ExecStart = "${cfg.package}/bin/swarm-claude-login"; + User = unit; + Group = unit; + # Each login's scratch directory, gone with the unit. + PrivateTmp = true; + UMask = "0077"; + # Bare ids: the system credentials nspawn handed in. + LoadCredential = lib.attrNames storeFiles; + Restart = "on-failure"; + RestartSec = "10s"; + SyslogIdentifier = unit; + }; + }; + }; + + # The container's system, evaluated as `containers..config` evaluates + # it, for a host that has no such container. + podSystem = + (import "${pkgs.path}/nixos/lib/eval-config.nix" { + modules = [ + ( + { options, ... }: + { + config = { + nixpkgs = + if options.nixpkgs ? hostPlatform then + { inherit (pkgs.stdenv) hostPlatform; } + else + { localSystem = pkgs.stdenv.hostPlatform; }; + boot.isNspawnContainer = true; + networking.hostName = lib.mkDefault machine; + networking.useDHCP = false; + }; + } + ) + containerConfig + ]; + system = null; + }).config; in { options.services.hyperhive.swarm.claude-login.clientId = lib.mkOption { @@ -110,156 +236,119 @@ in description = "The claude login minter daemon package."; }; - config = lib.mkIf cfg.enable { - assertions = [ - { - assertion = haveClientIdentity; - message = '' - services.hyperhive.deploy.claude-login.enable requires the minter's - swarm-secret-store client identity: set both - - services.hyperhive.deploy.bao.claudeLoginClientCertFile - services.hyperhive.deploy.bao.claudeLoginClientKeyFile - - The minter reads its queue client secret and writes agents' claude - credentials with it. - - Both default to the pair glue-bao-tls.nix mints when - services.hyperhive.deploy.bao.enable is true (the host that runs the - store). Otherwise copy claude-login.pem and claude-login-key.pem from - the store host's /var/lib/swarm-bao-pki/ and name them in those two - options. - ''; - } - { - assertion = lib.all oneWord (lib.attrValues storeFiles); - message = '' - The claude login minter's store identity paths must not contain - whitespace: nspawn's --load-credential receives them through - nixos-container's unquoted extra flags. - ''; - } + options.services.hyperhive.deploy.claude-login.runtime = lib.mkOption { + type = lib.types.enum [ + "host" + "k8s" ]; - - # The container hangs off the bridge, takes its address from the gateway's - # DHCP and resolves through its dnsmasq. - services.hyperhive.network.enable = lib.mkDefault true; - services.hyperhive.gateway.enable = lib.mkDefault true; - services.hyperhive.gateway.dns.enable = lib.mkDefault true; - - # nspawn fails to start the container when a credential source is - # missing, so where this host mints the leaf the container waits for it. - systemd.services."container@${machine}" = lib.mkMerge [ - caTrust.containerOrdering - (rootfsCheck.warnOnly machine) - { after = [ "swarm-bao-pki.service" ]; } - ]; - - containers.${machine} = { - autoStart = true; - # The daemon keeps no state; each login's scratch directory is under - # PrivateTmp. - ephemeral = false; - # No `--link-journal=host`: the journal stays in the guest, where the - # container's own forwarder reads it (../container-modules/swarm-journal-forwarder.nix). - extraFlags = lib.mapAttrsToList (id: source: "--load-credential=${id}:${source}") storeFiles; - inherit privateNetwork; - hostBridge = networkCfg.bridgeName; - bindMounts = caTrust.bindMount; - - config = - { pkgs, lib, ... }: - { - imports = [ - ../container-modules/swarm-container.nix - ../container-modules/swarm-journal-forwarder.nix - caBundleModule - ]; - - services.hyperhive.swarmContainer = { - inherit privateNetwork; - writesOwnResolvConf = false; - }; - networking.nameservers = [ networkCfg.bridgeIp ]; - networking.interfaces.eth0.useDHCP = true; - - # `claude-code` is unfree, and this container evaluates its own - # nixpkgs, so the host's allowance does not reach here. - nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "claude-code" ]; - - users.users.${unit} = { - isSystemUser = true; - group = unit; - }; - users.groups.${unit} = { }; - - # Authenticated as the daemon's own OIDC client, whose secret its - # store identity already reads: one identity for the whole container. - # That identity is a system credential, so the fetch unit loads it - # too; the daemon's `LoadCredential` below is that unit's alone. - services.hyperhive.swarmJournalForwarder = { - enable = true; - serviceName = "claude-login"; - endpoint = forwarderEndpoint; - inherit clientId; - tokenUrl = "${autheliaCfg.url}/api/oidc/token"; - telemetryPort = forwarderTelemetryPort; - otlpMetricsPort = forwarderOtlpPort; - store = { - package = baoDeploy.package; - addr = "https://${baoCfg.domain}:${toString baoCfg.port}"; - secretPath = "secret/swarm/services/${clientId}/oidc/client"; - clientCertFile = forwarderCredential "claude-login-bao-cert"; - clientKeyFile = forwarderCredential "claude-login-bao-key"; - caFile = - if storeFiles ? claude-login-bao-server-ca then - forwarderCredential "claude-login-bao-server-ca" - else - null; - }; - }; - systemd.services.${forwarderUnit}.serviceConfig.LoadCredential = lib.attrNames storeFiles; - - systemd.services.${unit} = { - description = "log agents in to claude for the swarm UI"; - wantedBy = [ "multi-user.target" ]; - wants = [ "network-online.target" ]; - after = [ "network-online.target" ]; - environment = { - BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}"; - BAO_CLIENT_CERT = credential "claude-login-bao-cert"; - BAO_CLIENT_KEY = credential "claude-login-bao-key"; - SWARM_CLAUDE_LOGIN_NATS_URL = "tls://${natsCfg.domain}:${toString natsCfg.port}"; - SWARM_CLAUDE_LOGIN_OIDC_TOKEN_ENDPOINT = "${autheliaCfg.url}/api/oidc/token"; - SWARM_CLAUDE_LOGIN_OIDC_CLIENT_ID = clientId; - SWARM_CLAUDE_LOGIN_CLAUDE = "${claudeCode pkgs.claude-code}/bin/claude"; - OTEL_EXPORTER_OTLP_ENDPOINT = "http://127.0.0.1:${toString forwarderOtlpPort}"; - OTEL_EXPORTER_OTLP_PROTOCOL = "http/protobuf"; - OTEL_RESOURCE_ATTRIBUTES = "service.name=hyperhive-claude-login,swarm=${ - if hyperhiveCfg.swarm.name == null then "unknown" else hyperhiveCfg.swarm.name - }"; - } - // lib.optionalAttrs (storeFiles ? claude-login-bao-server-ca) { - BAO_CACERT = credential "claude-login-bao-server-ca"; - } - // lib.optionalAttrs caTrust.useSelfSigned { - SWARM_CLAUDE_LOGIN_OIDC_CA_FILE = caTrust.bundlePathFor machine; - }; - serviceConfig = { - ExecStart = "${cfg.package}/bin/swarm-claude-login"; - User = unit; - Group = unit; - # Each login's scratch directory, gone with the unit. - PrivateTmp = true; - UMask = "0077"; - # Bare ids: the system credentials nspawn handed in. - LoadCredential = lib.attrNames storeFiles; - Restart = "on-failure"; - RestartSec = "10s"; - SyslogIdentifier = unit; - }; - }; - }; - }; + default = "host"; + description = '' + Where the claude login minter runs: `host` in its `swarm-claude-login` + container, or `k8s` as a pod in this host's cluster + ({option}`services.hyperhive.deploy.k8s.enable`), which the host's + configuration must enable. A switch moves the service: the container + goes and its objects are applied, or the objects are deleted and the + container returns. The service keeps no state, so nothing moves with it; + each login's scratch directory is in the pod's `/tmp` volume and goes with the pod. + ''; }; + + options.services.hyperhive.deploy.claude-login.unit = lib.mkOption { + type = lib.types.raw; + readOnly = true; + internal = true; + default = + if onK8s then + podSystem.systemd.services.${unit} + else + config.containers.${machine}.config.systemd.services.${unit}; + description = '' + The service's evaluated systemd unit, which the cluster's manifests are + rendered from (./k8s/render.nix). It is the unit the + container runs, taken from the container's configuration, or from the + same configuration evaluated alone when the service runs in the cluster + and the host has no container. + ''; + }; + + config = lib.mkMerge [ + { + assertions = [ + { + assertion = !onK8s || hyperhiveCfg.deploy.k8s.enable; + message = '' + services.hyperhive.deploy.claude-login.runtime = "k8s" needs the cluster: set + services.hyperhive.deploy.k8s.enable on this host, or leave the + service's runtime at "host". + ''; + } + ]; + } + + (lib.mkIf cfg.enable { + assertions = [ + { + assertion = haveClientIdentity; + message = '' + services.hyperhive.deploy.claude-login.enable requires the minter's + swarm-secret-store client identity: set both + + services.hyperhive.deploy.bao.claudeLoginClientCertFile + services.hyperhive.deploy.bao.claudeLoginClientKeyFile + + The minter reads its queue client secret and writes agents' claude + credentials with it. + + Both default to the pair glue-bao-tls.nix mints when + services.hyperhive.deploy.bao.enable is true (the host that runs the + store). Otherwise copy claude-login.pem and claude-login-key.pem from + the store host's /var/lib/swarm-bao-pki/ and name them in those two + options. + ''; + } + { + assertion = lib.all oneWord (lib.attrValues storeFiles); + message = '' + The claude login minter's store identity paths must not contain + whitespace: nspawn's --load-credential receives them through + nixos-container's unquoted extra flags. + ''; + } + ]; + + # The container hangs off the bridge, and the cluster's pods reach the + # host's services through it. + services.hyperhive.network.enable = lib.mkDefault true; + }) + + (lib.mkIf (cfg.enable && !onK8s) { + # The container takes its address from the gateway's DHCP and resolves + # through its dnsmasq. + services.hyperhive.gateway.enable = lib.mkDefault true; + services.hyperhive.gateway.dns.enable = lib.mkDefault true; + + # nspawn fails to start the container when a credential source is + # missing, so where this host mints the leaf the container waits for it. + systemd.services."container@${machine}" = lib.mkMerge [ + caTrust.containerOrdering + (rootfsCheck.warnOnly machine) + { after = [ "swarm-bao-pki.service" ]; } + ]; + + containers.${machine} = { + autoStart = true; + # The daemon keeps no state; each login's scratch directory is under + # PrivateTmp. + ephemeral = false; + # No `--link-journal=host`: the journal stays in the guest, where the + # container's own forwarder reads it (../container-modules/swarm-journal-forwarder.nix). + extraFlags = lib.mapAttrsToList (id: source: "--load-credential=${id}:${source}") storeFiles; + inherit privateNetwork; + hostBridge = networkCfg.bridgeName; + bindMounts = caTrust.bindMount; + + config = containerConfig; + }; + }) + ]; } diff --git a/nix/host-modules/swarm-review-classifier.nix b/nix/host-modules/swarm-review-classifier.nix index cfb05df5d..13165c2fc 100644 --- a/nix/host-modules/swarm-review-classifier.nix +++ b/nix/host-modules/swarm-review-classifier.nix @@ -5,6 +5,8 @@ # the queue, and the daemon runs `claude`, or `opencode` on the ACP runtime, # once per classification and replies. # docs/4-implementation/swarm/review-classifier.md has the design. +# With `deploy.review-classifier.runtime = "k8s"` it runs as a pod instead, +# rendered from the unit below (docs/4-implementation/k8s/runtime.md). # # ⚠️ Its own network namespace on the bridge, as ./swarm-ci-runner.nix has: a # PR's diff and commit messages are model input, so the container reaches the @@ -110,6 +112,119 @@ let }; privateNetwork = true; + + onK8s = cfg.runtime == "k8s"; + + # The module the container runs: its own NixOS system on the host's bridge, + # or, with the service on the cluster, only the source of the unit the pod is + # rendered from (`deploy.review-classifier.unit`). + containerConfig = + { pkgs, lib, ... }: + { + imports = [ + ../container-modules/swarm-container.nix + ../container-modules/swarm-journal-forwarder.nix + caBundleModule + ]; + + services.hyperhive.swarmContainer = { + inherit privateNetwork; + writesOwnResolvConf = false; + }; + networking.nameservers = [ networkCfg.bridgeIp ]; + networking.interfaces.eth0.useDHCP = true; + + # `claude-code` is unfree, and this container evaluates its own + # nixpkgs, so the host's allowance does not reach here. + nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "claude-code" ]; + + # Authenticated as the daemon's own OIDC client, whose secret its + # store identity already reads: one identity for the whole container. + # That identity is a system credential, so the fetch unit loads it + # too; the daemon's `LoadCredential` below is that unit's alone. + services.hyperhive.swarmJournalForwarder = { + enable = true; + serviceName = "review-classifier"; + endpoint = forwarderEndpoint; + inherit clientId; + tokenUrl = "${autheliaCfg.url}/api/oidc/token"; + telemetryPort = forwarderTelemetryPort; + otlpMetricsPort = forwarderOtlpPort; + store = { + package = baoDeploy.package; + addr = "https://${baoCfg.domain}:${toString baoCfg.port}"; + secretPath = "secret/swarm/services/${clientId}/oidc/client"; + clientCertFile = forwarderCredential "review-classifier-bao-cert"; + clientKeyFile = forwarderCredential "review-classifier-bao-key"; + caFile = + if storeFiles ? review-classifier-bao-server-ca then + forwarderCredential "review-classifier-bao-server-ca" + else + null; + }; + }; + systemd.services.${forwarderUnit}.serviceConfig.LoadCredential = lib.attrNames storeFiles; + + systemd.services.${unit} = { + description = "answer the swarm UI's review try-outs"; + wantedBy = [ "multi-user.target" ]; + wants = [ "network-online.target" ]; + after = [ "network-online.target" ]; + environment = { + BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}"; + BAO_CLIENT_CERT = credential "review-classifier-bao-cert"; + BAO_CLIENT_KEY = credential "review-classifier-bao-key"; + SWARM_REVIEW_CLASSIFIER_NATS_URL = "tls://${natsCfg.domain}:${toString natsCfg.port}"; + SWARM_REVIEW_CLASSIFIER_OIDC_TOKEN_ENDPOINT = "${autheliaCfg.url}/api/oidc/token"; + SWARM_REVIEW_CLASSIFIER_OIDC_CLIENT_ID = clientId; + SWARM_REVIEW_CLASSIFIER_CLAUDE = "${claudeCode pkgs.claude-code}/bin/claude"; + SWARM_REVIEW_CLASSIFIER_OPENCODE = "${pkgs.opencode}/bin/opencode"; + } + // lib.optionalAttrs (storeFiles ? review-classifier-bao-server-ca) { + BAO_CACERT = credential "review-classifier-bao-server-ca"; + } + // lib.optionalAttrs caTrust.useSelfSigned { + SWARM_REVIEW_CLASSIFIER_OIDC_CA_FILE = caTrust.bundlePathFor machine; + } + // otelEnv; + serviceConfig = { + ExecStart = "${cfg.package}/bin/swarm-review-classifier"; + DynamicUser = true; + # Each classification's scratch directory, gone with the unit. + PrivateTmp = true; + # Bare ids: the system credentials nspawn handed in. + LoadCredential = lib.attrNames storeFiles; + Restart = "on-failure"; + RestartSec = "10s"; + SyslogIdentifier = unit; + }; + }; + }; + + # The container's system, evaluated as `containers..config` evaluates + # it, for a host that has no such container. + podSystem = + (import "${pkgs.path}/nixos/lib/eval-config.nix" { + modules = [ + ( + { options, ... }: + { + config = { + nixpkgs = + if options.nixpkgs ? hostPlatform then + { inherit (pkgs.stdenv) hostPlatform; } + else + { localSystem = pkgs.stdenv.hostPlatform; }; + boot.isNspawnContainer = true; + networking.hostName = lib.mkDefault machine; + networking.useDHCP = false; + }; + } + ) + containerConfig + ]; + system = null; + }).config; in { options.services.hyperhive.swarm.review-classifier.clientId = lib.mkOption { @@ -130,150 +245,124 @@ in description = "The review classifier daemon package."; }; - config = lib.mkIf cfg.enable { - assertions = [ - { - assertion = haveClientIdentity; - message = '' - services.hyperhive.deploy.review-classifier.enable requires the - classifier's swarm-secret-store client identity: set both - - services.hyperhive.deploy.bao.reviewClassifierClientCertFile - services.hyperhive.deploy.bao.reviewClassifierClientKeyFile - - The classifier reads its queue client secret and its runtime keys with it. - - Both default to the pair glue-bao-tls.nix mints when - services.hyperhive.deploy.bao.enable is true (the host that runs the - store). Otherwise copy review-classifier.pem and - review-classifier-key.pem from the store host's /var/lib/swarm-bao-pki/ - and name them in those two options. - ''; - } - { - assertion = lib.all oneWord (lib.attrValues storeFiles); - message = '' - The review classifier's store identity paths must not contain - whitespace: nspawn's --load-credential receives them through - nixos-container's unquoted extra flags. - ''; - } + options.services.hyperhive.deploy.review-classifier.runtime = lib.mkOption { + type = lib.types.enum [ + "host" + "k8s" ]; - - # The container hangs off the bridge, takes its address from the gateway's - # DHCP and resolves through its dnsmasq. - services.hyperhive.network.enable = lib.mkDefault true; - services.hyperhive.gateway.enable = lib.mkDefault true; - services.hyperhive.gateway.dns.enable = lib.mkDefault true; - - # nspawn fails to start the container when a credential source is - # missing, so where this host mints the leaf the container pulls the - # minting unit in and waits for it. Ordering alone is not enough: the - # container starts from `machines.target`, outside the transaction that - # runs the minting unit from `multi-user.target`. Ignored elsewhere, where - # the unit does not exist. - systemd.services."container@${machine}" = lib.mkMerge [ - caTrust.containerOrdering - (rootfsCheck.warnOnly machine) - { - after = [ "swarm-bao-pki.service" ]; - wants = [ "swarm-bao-pki.service" ]; - } - ]; - - containers.${machine} = { - autoStart = true; - # The daemon keeps no state; its scratch directories are under PrivateTmp. - ephemeral = false; - # No `--link-journal=host`: the journal stays in the guest, where the - # container's own forwarder reads it (../container-modules/swarm-journal-forwarder.nix). - extraFlags = lib.mapAttrsToList (id: source: "--load-credential=${id}:${source}") storeFiles; - inherit privateNetwork; - hostBridge = networkCfg.bridgeName; - bindMounts = caTrust.bindMount; - - config = - { pkgs, lib, ... }: - { - imports = [ - ../container-modules/swarm-container.nix - ../container-modules/swarm-journal-forwarder.nix - caBundleModule - ]; - - services.hyperhive.swarmContainer = { - inherit privateNetwork; - writesOwnResolvConf = false; - }; - networking.nameservers = [ networkCfg.bridgeIp ]; - networking.interfaces.eth0.useDHCP = true; - - # `claude-code` is unfree, and this container evaluates its own - # nixpkgs, so the host's allowance does not reach here. - nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "claude-code" ]; - - # Authenticated as the daemon's own OIDC client, whose secret its - # store identity already reads: one identity for the whole container. - # That identity is a system credential, so the fetch unit loads it - # too; the daemon's `LoadCredential` below is that unit's alone. - services.hyperhive.swarmJournalForwarder = { - enable = true; - serviceName = "review-classifier"; - endpoint = forwarderEndpoint; - inherit clientId; - tokenUrl = "${autheliaCfg.url}/api/oidc/token"; - telemetryPort = forwarderTelemetryPort; - otlpMetricsPort = forwarderOtlpPort; - store = { - package = baoDeploy.package; - addr = "https://${baoCfg.domain}:${toString baoCfg.port}"; - secretPath = "secret/swarm/services/${clientId}/oidc/client"; - clientCertFile = forwarderCredential "review-classifier-bao-cert"; - clientKeyFile = forwarderCredential "review-classifier-bao-key"; - caFile = - if storeFiles ? review-classifier-bao-server-ca then - forwarderCredential "review-classifier-bao-server-ca" - else - null; - }; - }; - systemd.services.${forwarderUnit}.serviceConfig.LoadCredential = lib.attrNames storeFiles; - - systemd.services.${unit} = { - description = "answer the swarm UI's review try-outs"; - wantedBy = [ "multi-user.target" ]; - wants = [ "network-online.target" ]; - after = [ "network-online.target" ]; - environment = { - BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}"; - BAO_CLIENT_CERT = credential "review-classifier-bao-cert"; - BAO_CLIENT_KEY = credential "review-classifier-bao-key"; - SWARM_REVIEW_CLASSIFIER_NATS_URL = "tls://${natsCfg.domain}:${toString natsCfg.port}"; - SWARM_REVIEW_CLASSIFIER_OIDC_TOKEN_ENDPOINT = "${autheliaCfg.url}/api/oidc/token"; - SWARM_REVIEW_CLASSIFIER_OIDC_CLIENT_ID = clientId; - SWARM_REVIEW_CLASSIFIER_CLAUDE = "${claudeCode pkgs.claude-code}/bin/claude"; - SWARM_REVIEW_CLASSIFIER_OPENCODE = "${pkgs.opencode}/bin/opencode"; - } - // lib.optionalAttrs (storeFiles ? review-classifier-bao-server-ca) { - BAO_CACERT = credential "review-classifier-bao-server-ca"; - } - // lib.optionalAttrs caTrust.useSelfSigned { - SWARM_REVIEW_CLASSIFIER_OIDC_CA_FILE = caTrust.bundlePathFor machine; - } - // otelEnv; - serviceConfig = { - ExecStart = "${cfg.package}/bin/swarm-review-classifier"; - DynamicUser = true; - # Each classification's scratch directory, gone with the unit. - PrivateTmp = true; - # Bare ids: the system credentials nspawn handed in. - LoadCredential = lib.attrNames storeFiles; - Restart = "on-failure"; - RestartSec = "10s"; - SyslogIdentifier = unit; - }; - }; - }; - }; + default = "host"; + description = '' + Where the review classifier runs: `host` in its `swarm-review-classifier` + container, or `k8s` as a pod in this host's cluster + ({option}`services.hyperhive.deploy.k8s.enable`), which the host's + configuration must enable. A switch moves the service: the container + goes and its objects are applied, or the objects are deleted and the + container returns. The service keeps no state, so nothing moves with it; + each classification's scratch directory is in the pod's `/tmp` volume and goes with the pod. + ''; }; + + options.services.hyperhive.deploy.review-classifier.unit = lib.mkOption { + type = lib.types.raw; + readOnly = true; + internal = true; + default = + if onK8s then + podSystem.systemd.services.${unit} + else + config.containers.${machine}.config.systemd.services.${unit}; + description = '' + The service's evaluated systemd unit, which the cluster's manifests are + rendered from (./k8s/render.nix). It is the unit the + container runs, taken from the container's configuration, or from the + same configuration evaluated alone when the service runs in the cluster + and the host has no container. + ''; + }; + + config = lib.mkMerge [ + { + assertions = [ + { + assertion = !onK8s || hyperhiveCfg.deploy.k8s.enable; + message = '' + services.hyperhive.deploy.review-classifier.runtime = "k8s" needs the cluster: set + services.hyperhive.deploy.k8s.enable on this host, or leave the + service's runtime at "host". + ''; + } + ]; + } + + (lib.mkIf cfg.enable { + assertions = [ + { + assertion = haveClientIdentity; + message = '' + services.hyperhive.deploy.review-classifier.enable requires the + classifier's swarm-secret-store client identity: set both + + services.hyperhive.deploy.bao.reviewClassifierClientCertFile + services.hyperhive.deploy.bao.reviewClassifierClientKeyFile + + The classifier reads its queue client secret and its runtime keys with it. + + Both default to the pair glue-bao-tls.nix mints when + services.hyperhive.deploy.bao.enable is true (the host that runs the + store). Otherwise copy review-classifier.pem and + review-classifier-key.pem from the store host's /var/lib/swarm-bao-pki/ + and name them in those two options. + ''; + } + { + assertion = lib.all oneWord (lib.attrValues storeFiles); + message = '' + The review classifier's store identity paths must not contain + whitespace: nspawn's --load-credential receives them through + nixos-container's unquoted extra flags. + ''; + } + ]; + + # The container hangs off the bridge, and the cluster's pods reach the + # host's services through it. + services.hyperhive.network.enable = lib.mkDefault true; + }) + + (lib.mkIf (cfg.enable && !onK8s) { + # The container takes its address from the gateway's DHCP and resolves + # through its dnsmasq. + services.hyperhive.gateway.enable = lib.mkDefault true; + services.hyperhive.gateway.dns.enable = lib.mkDefault true; + + # nspawn fails to start the container when a credential source is + # missing, so where this host mints the leaf the container pulls the + # minting unit in and waits for it. Ordering alone is not enough: the + # container starts from `machines.target`, outside the transaction that + # runs the minting unit from `multi-user.target`. Ignored elsewhere, where + # the unit does not exist. + systemd.services."container@${machine}" = lib.mkMerge [ + caTrust.containerOrdering + (rootfsCheck.warnOnly machine) + { + after = [ "swarm-bao-pki.service" ]; + wants = [ "swarm-bao-pki.service" ]; + } + ]; + + containers.${machine} = { + autoStart = true; + # The daemon keeps no state; its scratch directories are under PrivateTmp. + ephemeral = false; + # No `--link-journal=host`: the journal stays in the guest, where the + # container's own forwarder reads it (../container-modules/swarm-journal-forwarder.nix). + extraFlags = lib.mapAttrsToList (id: source: "--load-credential=${id}:${source}") storeFiles; + inherit privateNetwork; + hostBridge = networkCfg.bridgeName; + bindMounts = caTrust.bindMount; + + config = containerConfig; + }; + }) + ]; }