# cc-ci on Hetzner Cloud — the canonical STANDALONE CI-server host.
# Hardware + networking + host identity only; every cc-ci service comes from the shared
# `nixosModules.cc-ci-server` module (nix/modules/default.nix), which flake.nix adds to this
# host. The same module builds the combined CI-server + orchestrator host declared in
# recipe-maintainers/cc-ci-orchestrator (`#cc-ci`), which is where cc-ci is moving (2026-09).
#
# To apply after `terraform apply` + nixos-infect:
#   git clone --recursive https://git.autonomic.zone/recipe-maintainers/cc-ci.git /etc/cc-ci
#   install -m600 <age-private-key> /var/lib/sops-nix/key.txt
#   nixos-rebuild switch --flake 'git+file:///etc/cc-ci?submodules=1#cc-ci'
{ pkgs, ... }:
{
  imports = [
    ./hardware.nix
    ./networking.nix
  ];

  # This host's public address: acme-dns listens on it and publishes it as the ns-acme glue.
  cc-ci.publicIPv4 = "91.98.47.73";
  # Built from a --recursive clone, so the sops file is the default (the secrets/ submodule).

  # Tailscale — keeps the orchestrator→cc-ci access path unchanged (direct peer).
  # On the Hetzner host the auth key is also seeded via /etc/ts-auth-key.
  services.tailscale = {
    enable = true;
    authKeyFile = "/etc/ts-auth-key";
    extraUpFlags = [ "--hostname=cc-ci" ];
  };

  # SSH — allow root login over tailscale (same as Incus host).
  services.openssh = {
    enable = true;
    settings.PermitRootLogin = "yes";
  };

  # Root SSH authorized keys — preserved across nixos-rebuild switches.
  users.users.root.openssh.authorizedKeys.keys = [
    "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOk8NaeBdPbS2gfUvbny8h0AkZlVjGYHzx4QPXSJ38gd claude@claude-vm"
    "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJVlfoLBPseQ9fA9534KmRg2KWcksKZGzAJIpHJ2JpsI mfowler.email@protonmail.com"
    "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAcyTGb/wVgdhg5oBCZZvBaR1RuUQRY/3WHnOQpNDCsp claude-cc-ci-sandbox@20260526"
  ];

  # Firewall — Hetzner has a public IP, so open 80+443 for Traefik.
  # Tailscale interface is trusted (no port restrictions for orchestrator access).
  # Plan §6: v1 keeps the sops wildcard cert; evaluate ACME-on-public-IP as follow-up.
  networking.firewall = {
    enable = true;
    trustedInterfaces = [ "tailscale0" ];
    allowedTCPPorts = [ 22 80 443 ];
  };

  # The recipe-test tool set (ccciRuntimeTools) is installed by the cc-ci-server module; the ssh
  # client is a host-only addition (not part of the recipe-test tool set).
  environment.systemPackages = [ pkgs.openssh ];

  system.stateVersion = "24.11";
}
