From 217b122c676f84d87c9b2f6383083e8138c7663d Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Tue, 22 Sep 2026 09:58:48 -0400 Subject: [PATCH] buzz working --- configs/pki/ABOUTME.md | 23 ++++-- hosts/hl-minipc1.nix | 42 +++++++---- modules/nixos/server/ABOUTME.md | 2 + modules/nixos/server/acme/ABOUTME.md | 54 ++++++++++++++ modules/nixos/server/acme/default.nix | 99 ++++++++++++++++++++++++++ modules/nixos/server/default.nix | 1 + secrets.nix | 3 + secrets/buzz-env.age | Bin 1202 -> 1207 bytes secrets/cloudflare-dns.age | Bin 0 -> 401 bytes 9 files changed, 204 insertions(+), 20 deletions(-) create mode 100644 modules/nixos/server/acme/ABOUTME.md create mode 100644 modules/nixos/server/acme/default.nix create mode 100644 secrets/cloudflare-dns.age diff --git a/configs/pki/ABOUTME.md b/configs/pki/ABOUTME.md index d2ab17a..b22da60 100644 --- a/configs/pki/ABOUTME.md +++ b/configs/pki/ABOUTME.md @@ -6,11 +6,11 @@ managed with agenix. ## Contents -| File | Description | -|---|---| -| `lab-ca.crt` | Root CA cert (`CN=Skyg Lab CA`). Trusted fleet-wide via `modules/core/lab-ca.nix` (`security.pki.certificateFiles`). | -| `lab-ca.srl` | OpenSSL serial counter, written on first `ca issue`. Needed for unique serials — commit it. | -| `.crt` | Leaf certs issued for services (e.g. `buzz.app.internal.crt`). | +| File | Description | +| -------------- | -------------------------------------------------------------------------------------------------------------------- | +| `lab-ca.crt` | Root CA cert (`CN=Skyg Lab CA`). Trusted fleet-wide via `modules/core/lab-ca.nix` (`security.pki.certificateFiles`). | +| `lab-ca.srl` | OpenSSL serial counter, written on first `ca issue`. Needed for unique serials — commit it. | +| `.crt` | Leaf certs issued for services (e.g. `buzz.app.internal.crt`). | ## Managed by `skyg ca` (bin/lib/cmds.nu) @@ -31,3 +31,16 @@ Never hand-edit or commit keys here. Re-issuing: delete the leaf cert and re-run `ca issue`. Rotating the CA itself: delete `lab-ca.crt` + `secrets/lab-ca-key.age`, re-run `ca init`, re-issue all leaves, and rebuild every host (fleet trust comes from the committed root cert). + +## When to use what + +- **Lab CA (this dir)**: internal-only services whose clients all use the + system trust store (fleet trust via `modules/core/lab-ca.nix`). +- **Public Let's Encrypt cert** (`skyg.server.acme`, see + `modules/nixos/server/acme/`): anything reached by clients that only trust + public CAs — e.g. the Buzz desktop app, whose WS client bundles Mozilla + roots and ignores the system store. Buzz uses a public cert for + `buzz.home.sosnovsky.ca` (Cloudflare DNS-01); the zone lives in + `~/Projects/Homelab/homelab` pending migration into this repo. + `buzz.app.internal.crt` here is now unused by the buzz stack and kept only + as a lab-CA example. diff --git a/hosts/hl-minipc1.nix b/hosts/hl-minipc1.nix index be5b22f..3c5f5db 100644 --- a/hosts/hl-minipc1.nix +++ b/hosts/hl-minipc1.nix @@ -34,6 +34,8 @@ let # Buzz stack buzz = { domain = "buzz.app.internal"; + # Public name with a Let's Encrypt cert (skyg.server.acme) — what clients use. + publicDomain = "buzz.home.sosnovsky.ca"; port = 3000; # Caddy's macvlan IP — buzz.app.internal resolves here; the relay is internal-only. ip = "10.0.101.3"; @@ -264,8 +266,17 @@ in # Buzz — relay + postgres + redis + minio (S3). age.secrets.buzz-env.file = ../secrets/buzz-env.age; - # TLS key for buzz.app.internal, minted by `skyg ca issue` (lab CA). - age.secrets.buzz-tls-key.file = ../secrets/buzz-tls-key.age; + + age.secrets.cloudflare-dns.file = ../secrets/cloudflare-dns.age; + skyg.server.acme = { + enable = true; + credentialsFile = config.age.secrets.cloudflare-dns.path; + certs."${buzz.publicDomain}" = { + postRun = "${pkgs.docker}/bin/docker restart buzz-caddy-1"; + orderBefore = [ "container-services-buzz" ]; + }; + }; + skyg.nixos.common.container-services.buzz = { enable = true; autoUpdate.enable = true; @@ -298,10 +309,10 @@ in BUZZ_BIND_ADDR = "0.0.0.0:${toString buzz.port}"; BUZZ_HEALTH_PORT = "8080"; BUZZ_METRICS_PORT = "9102"; - RELAY_URL = "wss://${buzz.domain}"; - BUZZ_MEDIA_BASE_URL = "https://${buzz.domain}/media"; - BUZZ_MEDIA_SERVER_DOMAIN = buzz.domain; - BUZZ_CORS_ORIGINS = "https://${buzz.domain}"; + RELAY_URL = "wss://${buzz.publicDomain}"; + BUZZ_MEDIA_BASE_URL = "https://${buzz.publicDomain}/media"; + BUZZ_MEDIA_SERVER_DOMAIN = buzz.publicDomain; + BUZZ_CORS_ORIGINS = "https://${buzz.publicDomain}"; BUZZ_S3_ENDPOINT = "http://minio:9000"; BUZZ_S3_REGION = "us-east-1"; BUZZ_S3_ADDRESSING_STYLE = "path"; @@ -324,10 +335,6 @@ in }; }; - # TLS terminator — owns buzz.app.internal (443/80), proxies to the relay - # over the internal network. Cert minted by `skyg ca issue` (lab CA). - # Also proxies plain :3000 (Hermes guest can't trust the CA yet), :8080 - # health and :9102 metrics so everything stays on 10.0.101.3. caddy = { image = buzz.images.caddy; extraConfig.depends_on.relay.condition = "service_healthy"; @@ -335,14 +342,19 @@ in internal = { }; lan = { ipv4_address = buzz.ip; }; }; - dns.names = [ buzz.domain ]; + dns.names = [ buzz.domain buzz.publicDomain ]; files."/etc/caddy/Caddyfile" = '' - ${buzz.domain} { - tls /etc/caddy/tls/cert.pem /etc/caddy/tls/key.pem + ${buzz.publicDomain} { + tls /etc/caddy/tls/fullchain.pem /etc/caddy/tls/key.pem encode zstd gzip reverse_proxy relay:${toString buzz.port} } + # Old internal name -> public one (plain :3000 below still serves it). + http://${buzz.domain} { + redir https://${buzz.publicDomain}{uri} permanent + } + :${toString buzz.port} { reverse_proxy relay:${toString buzz.port} } @@ -356,8 +368,8 @@ in } ''; volumes = [ - "${../configs/pki/buzz.app.internal.crt}:/etc/caddy/tls/cert.pem:ro" - "${config.age.secrets.buzz-tls-key.path}:/etc/caddy/tls/key.pem:ro" + "/var/lib/acme/${buzz.publicDomain}/fullchain.pem:/etc/caddy/tls/fullchain.pem:ro" + "/var/lib/acme/${buzz.publicDomain}/key.pem:/etc/caddy/tls/key.pem:ro" ]; }; diff --git a/modules/nixos/server/ABOUTME.md b/modules/nixos/server/ABOUTME.md index cda35a6..0a2f20e 100644 --- a/modules/nixos/server/ABOUTME.md +++ b/modules/nixos/server/ABOUTME.md @@ -12,6 +12,7 @@ server/ ├── admin.nix # skyg.server.admin — www-data group/user for shared web data ├── exporters.nix # skyg.server.exporters — Prometheus node exporter (:9100) ├── timers.nix # skyg.server.timers — generic attrset → systemd timer+oneshot service +├── acme/ # skyg.server.acme — Let's Encrypt certs via DNS-01 (lego), no internet exposure ├── arrs/ # *arr media-automation stack (prowlarr/sonarr/radarr) + postgres ├── dns/ # Local DNS resolver, certbot, local CA ├── k3s/ # Single-binary K3s cluster role @@ -36,6 +37,7 @@ server/ skyg.server.admin.enable → admin.nix skyg.server.exporters.enable → exporters.nix skyg.server.timers. → timers.nix +skyg.server.acme.* → acme/ skyg.server.arrs.* → arrs/ skyg.server.dns.* → dns/ skyg.nixos.server.k3s.* → k3s/ diff --git a/modules/nixos/server/acme/ABOUTME.md b/modules/nixos/server/acme/ABOUTME.md new file mode 100644 index 0000000..fe5a83d --- /dev/null +++ b/modules/nixos/server/acme/ABOUTME.md @@ -0,0 +1,54 @@ +# modules/nixos/server/acme/ + +Let's Encrypt certificates via **DNS-01**, wrapped around NixOS +`security.acme` (lego) with this repo's conventions. Domain ownership is +proven through DNS records at the provider, so **nothing needs to be +reachable from the internet** — no port forwards, no public listeners. + +## Options (`skyg.server.acme.*`) + +| Option | Default | Purpose | +| ------------------------ | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `enable` | `false` | Master switch, per host. | +| `email` | `admin@sosnovsky.ca` | Let's Encrypt account contact (expiry notices). | +| `dnsProvider` | `"cloudflare"` | lego DNS provider name (see lego docs). | +| `credentialsFile` | — | Env file with provider API credentials (e.g. `CLOUDFLARE_DNS_API_TOKEN=...`). Point at an agenix secret — never the Nix store. | +| `certs.""` | `{ }` | One block per certificate; the attribute name is the primary domain. | +| `certs.*.extraDomains` | `[ ]` | Extra names on the same cert (SANs). | +| `certs.*.reloadServices` | `[ ]` | Native systemd services reloaded after each renewal. | +| `certs.*.postRun` | `""` | Shell command after each renewal (e.g. `docker restart ` for compose stacks). | +| `certs.*.orderBefore` | `[ ]` | `systemd.services` attribute names (no `.service` suffix) that start only after the cert exists (first boot — otherwise Docker bind-mounts empty dirs over the missing cert files). | + +## Usage + +```nix +age.secrets.cloudflare-dns.file = ../secrets/cloudflare-dns.age; +skyg.server.acme = { + enable = true; + credentialsFile = config.age.secrets.cloudflare-dns.path; + certs."app.home.sosnovsky.ca" = { + postRun = "${pkgs.docker}/bin/docker restart my-stack-caddy-1"; + orderBefore = [ "container-services-my-stack" ]; + }; +}; +``` + +Certs land in `/var/lib/acme//` (`fullchain.pem`, `key.pem`, …), +renewed automatically by the `acme-.service` timer; hooks run after +each renewal. The secret contains one line: `CLOUDFLARE_DNS_API_TOKEN=` +(token scope: Zone → DNS → Edit, restricted to the one zone). + +## ⚠ Cloudflare zone lives elsewhere (for now) + +The `sosnovsky.ca` zone and the Cloudflare account/API token are managed in +`~/Projects/Homelab/homelab` (Terraform `tf-dns.tf`, cert-manager) until +DNS/SSL management migrates into this repo. When that migration happens, only +the token's provenance changes — this module is unaffected. + +## Notes + +- Supersedes `skyg.server.dns.certbot` (HTTP-01 + nginx, disabled) for hosts + that are not publicly reachable. The certbot module is left untouched. +- For internal-only services whose clients all use the system trust store, + the lab CA (`skyg ca`, see `configs/pki/ABOUTME.md`) is the simpler option — + no public name needed. diff --git a/modules/nixos/server/acme/default.nix b/modules/nixos/server/acme/default.nix new file mode 100644 index 0000000..0c3a902 --- /dev/null +++ b/modules/nixos/server/acme/default.nix @@ -0,0 +1,99 @@ +{ config, lib, ... }: + +with lib; + +let + cfg = config.skyg.server.acme; +in +{ + # Let's Encrypt certificates via DNS-01 (NixOS security.acme / lego). + # Proves domain ownership through DNS records at the provider — nothing + # needs to be reachable from the internet. + options.skyg.server.acme = { + enable = mkEnableOption "Let's Encrypt certificates via DNS-01"; + + email = mkOption { + type = types.str; + default = "admin@sosnovsky.ca"; + description = "Contact email for the Let's Encrypt account (expiry notices)."; + }; + + dnsProvider = mkOption { + type = types.str; + default = "cloudflare"; + description = "lego DNS provider used to prove ownership (see lego docs for names)."; + }; + + credentialsFile = mkOption { + type = types.path; + description = '' + File with the DNS provider's API credentials in lego's env-var format + (e.g. CLOUDFLARE_DNS_API_TOKEN=... for cloudflare). Point at an agenix + secret so the token never enters the Nix store. + ''; + }; + + certs = mkOption { + type = types.attrsOf (types.submodule { + options = { + extraDomains = mkOption { + type = types.listOf types.str; + default = [ ]; + description = "Extra names covered by the same certificate (SANs)."; + }; + reloadServices = mkOption { + type = types.listOf types.str; + default = [ ]; + description = "Native systemd services reloaded after each renewal."; + }; + postRun = mkOption { + type = types.str; + default = ""; + description = "Shell command run after each renewal (e.g. restart a container)."; + }; + orderBefore = mkOption { + type = types.listOf types.str; + default = [ ]; + description = '' + systemd.services attribute names (without the .service suffix) + that must start only after this cert exists (first boot). + ''; + }; + }; + }); + default = { }; + description = "Certificates to issue; the attribute name is the primary domain."; + }; + }; + + config = mkIf cfg.enable { + security.acme = { + acceptTerms = true; + defaults.email = cfg.email; + certs = mapAttrs + (domain: certCfg: { + inherit (cfg) dnsProvider; + environmentFile = cfg.credentialsFile; + extraDomainNames = certCfg.extraDomains; + reloadServices = certCfg.reloadServices; + postRun = certCfg.postRun; + }) + cfg.certs; + }; + + # First-boot ordering: consumers start only after the cert exists. + systemd.services = mkMerge (flatten ( + mapAttrsToList + (domain: certCfg: + map + (unit: { + ${unit} = { + after = [ "acme-${domain}.service" ]; + wants = [ "acme-${domain}.service" ]; + }; + }) + certCfg.orderBefore) + cfg.certs + )); + }; +} diff --git a/modules/nixos/server/default.nix b/modules/nixos/server/default.nix index 18f4591..c5569ba 100644 --- a/modules/nixos/server/default.nix +++ b/modules/nixos/server/default.nix @@ -3,6 +3,7 @@ imports = [ ./exporters.nix ./admin.nix + ./acme ./arrs ./k8s ./k3s diff --git a/secrets.nix b/secrets.nix index 8ee6d38..08486c1 100644 --- a/secrets.nix +++ b/secrets.nix @@ -28,6 +28,9 @@ in # Buzz stack env (minipc1) "secrets/buzz-env.age".publicKeys = [ ari minipc1 ]; + # Cloudflare DNS API token for ACME DNS-01 (skyg.server.acme) + "secrets/cloudflare-dns.age".publicKeys = [ ari minipc1 ]; + # stack compose definition — minipc2 "secrets/stack1.age".publicKeys = [ ari minipc2 ]; "secrets/stack2.age".publicKeys = [ ari minipc2 ]; diff --git a/secrets/buzz-env.age b/secrets/buzz-env.age index 97f4f4d95fa01be1c913f664eefcc252b15e2dac..f77159f5fe32130d7a1900ec5eba1004ee852da2 100644 GIT binary patch delta 1179 zcmV;M1Z4ZN3AYK5EPrTHdNp`=ba#48QbSg6VrWS=aAit!b~rJ0a(a4BV@*|QN_sar zLU(v;FbXnoZALaYGd4?RWNl_jacVO$seO zAaiqQEoEdfH8n9gAWU^iPEkQ1S!;B1dPg%$IZj4aO?5e8XTsApO zyOfb16OwSwU{>VlHe(82d@9({dj@i|Q@$j@w&YFtW`B8I8#^H6W5QYJ6+`0RTuj>S z$CVuma&wCySCypuj!|_$mYknbwUEXQ{8Ht7Elh&=Zeww4P_p7dLd!Xf#-EEn5I<`w zbO#d0Gx$b)O?==!mh#ysy14hvFLvPlHMA2|DVb)~!xe=CzoP&p@D@jZ?4h8jIODBv zY(`$0;(v5jiS+IyDg{>Cnu-n4>Q2Lm5a^veT5MtU9;`Jni235G=Nu%|f5a1M%swQ_ zQ%8;`0(9qqgDTYY7efx-9YCtB;52d4>(|K&mh0M{XUx8;F&Rqbi<7|+kBHz(=O6P} zqpsj&TVnM**9`>i7*$X-(J1vr&Di`(;mTE`mwzoIRy=B(-=1;1|G`W4Wog{Da%`e5 zFhYOLfI6ioMhb~ZzMj9*j6Ycxz9bq@NvZcXW6=Q@1sSXcs)2x*lwQJ)5hJ&)vVUtb zSfS);eN-_lTYWws6J(K6ldIDYudlMr=LP&Yvx2yF2g(mwqZkRxq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j8Y|Q3iXtmQTJN=xSKFk6m?A08M6P>2{hCZvX%Q diff --git a/secrets/cloudflare-dns.age b/secrets/cloudflare-dns.age new file mode 100644 index 0000000000000000000000000000000000000000..e693c87cf382e7c71f4691054a0929d4d02f7f2c GIT binary patch literal 401 zcmYdHPt{G$OD?J`D9Oyv)5|YP*Do{V(zR14F3!+RO))YxHMCT4_6#z}byP^o)ArFX z%<%Dw$n^Kk$kNX?);AByD5yxRuqX`mHZlsxHcj()%`3|YcjfX<)h>>5_sI+K^o+{% z_6*VYtaMFuEAey6v?$6<)b~se^fC1JODfNGEJwG^r_{?Y&{4r8FvY{OBG@R@BB~%m zJIp8DE!nXsuh`7gvb@Ac-`FxF*fl>i$SpZP*OjX$Rl