diff --git a/hosts/hl-minipc1.nix b/hosts/hl-minipc1.nix index a427c28..15646a2 100644 --- a/hosts/hl-minipc1.nix +++ b/hosts/hl-minipc1.nix @@ -53,11 +53,11 @@ let # Docker's `local` driver calls mount(2) itself, so it cannot resolve a # `host:/path` device: the server belongs in `o = addr=...` (the daemon # resolves that name) and `device` must be the bare `:/export/path`. - nfsVolume = subpath: { + nfsVolume = exportPath: { driver_opts = { type = "nfs"; o = "addr=tnas1.lab.internal,rw,nfsvers=4.0,nolock,hard,noatime"; - device = ":/mnt/SmallG/buzz/${subpath}"; + device = ":${exportPath}"; }; }; # Buzz management CLI — upstream deploy/compose/run.sh ported to this @@ -91,7 +91,7 @@ in skyg.networkDrives = { enable = true; }; - services.tailscale.enable = true; + services.tailscale.enable = false; services.tailscale.disableTaildrop = true; services.tailscale.useRoutingFeatures = "both"; services.tailscale.openFirewall = true; @@ -108,11 +108,15 @@ in # Audiobookshelf — containerised, own macvlan IP, reachable as # http://audiobooks.app.internal skyg.nixos.common.container-services.audiobookshelf = { - enable = false; + enable = true; autoUpdate.enable = true; networks.lan = config.skyg.nixos.common.containers.networks.ipvlanLab.compose; + volumes = { + books = nfsVolume "/mnt/EightTerra/DownloadedTorrents/books"; + }; + services.audiobookshelf = { image = audiobookshelf.image; networks.lan.ipv4_address = audiobookshelf.ip; @@ -123,7 +127,7 @@ in volumes = [ "${audiobookshelf.dataDir}/config:/config" "${audiobookshelf.dataDir}/metadata:/metadata" - "${audiobookshelf.dataDir}/audiobooks:/audiobooks" + "books:/audiobooks" "${audiobookshelf.dataDir}/podcasts:/podcasts" ]; }; @@ -286,10 +290,10 @@ in }; volumes = { - buzz-postgres = nfsVolume "postgres"; - buzz-redis = nfsVolume "redis"; - s3 = nfsVolume "s3"; - buzz-git = nfsVolume "git"; + buzz-postgres = nfsVolume "/mnt/SmallG/buzz/postgres"; + buzz-redis = nfsVolume "/mnt/SmallG/buzz/redis"; + s3 = nfsVolume "/mnt/SmallG/buzz/s3"; + buzz-git = nfsVolume "/mnt/SmallG/buzz/git"; }; services = { @@ -466,16 +470,6 @@ in # Buzz management CLI. environment.systemPackages = [ buzz-manage ]; - # Certbot TLS service - skyg.server.dns.certbot = { - enable = false; - email = "admin@skyg.ca"; - publicDomains = [ - ".*skyg.ca" - ".*home.sosnovsky.ca" - ]; - }; - # Firewall networking.firewall.allowedUDPPorts = openPorts; networking.firewall.allowedTCPPorts = openPorts; diff --git a/hosts/hl-minipc3.nix b/hosts/hl-minipc3.nix index af82608..327e76a 100644 --- a/hosts/hl-minipc3.nix +++ b/hosts/hl-minipc3.nix @@ -14,6 +14,16 @@ enable = true; envPath = "/opt/k3s/k3s.env"; }; + # Portainer Edge Agent (k3s deployment) + age.secrets.portainer-agent-minipc3.file = ../secrets/portainer-agent-minipc3.age; + skyg.nixos.server.portainer = { + enable = true; + mode = "agent"; + agent.k3s = { + enable = true; + edgeKeySecretName = "portainer-agent-minipc3"; + }; + }; # firmware updater services.fwupd.enable = true; # Bootloader. diff --git a/modules/nixos/common/containers/networks.nix b/modules/nixos/common/containers/networks.nix index f1c8897..647b7c4 100644 --- a/modules/nixos/common/containers/networks.nix +++ b/modules/nixos/common/containers/networks.nix @@ -240,7 +240,7 @@ in }; systemd.services = lib.mkIf (cfg.enable && allNetworks != [ ]) { - container-networks = { + container-networks-setup = { description = "Create configured container networks"; after = [ runtimeService "network-online.target" ]; requires = [ runtimeService ]; @@ -251,9 +251,6 @@ in RemainAfterExit = true; }; script = '' - # Prune unused networks so stale ones don't linger. - ${runtimeBin} network prune -f 2>/dev/null || true - ${lib.concatStringsSep "\n" (map (net: '' ${runtimeBin} network inspect ${net.name} >/dev/null 2>&1 \ || ${runtimeBin} ${createArgs net} diff --git a/modules/nixos/server/portainer.nix b/modules/nixos/server/portainer.nix index 940b707..46b5f74 100644 --- a/modules/nixos/server/portainer.nix +++ b/modules/nixos/server/portainer.nix @@ -4,6 +4,87 @@ with lib; let cfg = config.skyg.nixos.server.portainer; + + # Kubernetes manifest for the Portainer Agent (without the EDGE_KEY secret — + # that's created at runtime from the age secret). + agentManifest = pkgs.writeText "portainer-agent-k8s.yml" '' + apiVersion: v1 + kind: Namespace + metadata: + name: portainer + --- + apiVersion: v1 + kind: ServiceAccount + metadata: + name: portainer-agent + namespace: portainer + --- + apiVersion: rbac.authorization.k8s.io/v1 + kind: ClusterRoleBinding + metadata: + name: portainer-agent + roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: cluster-admin + subjects: + - kind: ServiceAccount + name: portainer-agent + namespace: portainer + --- + apiVersion: apps/v1 + kind: Deployment + metadata: + name: portainer-agent + namespace: portainer + spec: + replicas: 1 + selector: + matchLabels: + app: portainer-agent + template: + metadata: + labels: + app: portainer-agent + spec: + serviceAccountName: portainer-agent + containers: + - name: portainer-agent + image: portainer/agent:2.45.0 + env: + - name: EDGE + value: "1" + - name: EDGE_INSECURE_POLL + value: "1" + - name: EDGE_KEY + valueFrom: + secretKeyRef: + name: portainer-agent-edge-key + key: EDGE_KEY + - name: EDGE_ID + valueFrom: + secretKeyRef: + name: portainer-agent-edge-key + key: EDGE_ID + - name: KUBERNETES_POD_IP + valueFrom: + fieldRef: + fieldPath: status.podIP + volumeMounts: + - name: data + mountPath: /data + livenessProbe: + httpGet: + path: /health + port: 9001 + readinessProbe: + httpGet: + path: /health + port: 9001 + volumes: + - name: data + emptyDir: {} + ''; in { options.skyg.nixos.server.portainer = { @@ -72,13 +153,28 @@ in Example: "portainer-agent-minipc1" ''; }; + + k3s = { + enable = mkEnableOption "deploy the Portainer Agent as a Kubernetes deployment on k3s"; + + edgeKeySecretName = mkOption { + type = types.nullOr types.str; + default = null; + description = '' + Name of the age secret containing the EDGE_KEY for the k3s agent. + The corresponding age.secrets..file must be declared in the + host file. Example: "portainer-agent-minipc3" + ''; + }; + }; }; }; config = mkIf cfg.enable { - skyg.nixos.common.container-services.portainer = - if cfg.mode == "server" - then { + # Server mode — Docker container via container-services + # Agent mode (Docker) — Docker container via container-services + skyg.nixos.common.container-services.portainer = mkMerge [ + (mkIf (cfg.mode == "server") { enable = true; autoUpdate.enable = true; services.portainer = { @@ -104,8 +200,8 @@ in name = "portainer_data"; }; networks.lan = config.skyg.nixos.common.containers.networks.ipvlanLab.compose; - } - else { + }) + (mkIf (cfg.mode == "agent" && !cfg.agent.k3s.enable) { enable = true; autoUpdate.enable = true; services.agent = { @@ -125,6 +221,50 @@ in volumes.portainer_agent_data = { name = "portainer_data"; }; + }) + ]; + + # Agent mode — Kubernetes deployment on k3s + systemd.services.portainer-agent-k3s = mkIf (cfg.mode == "agent" && cfg.agent.k3s.enable) { + description = "Portainer Edge Agent on k3s"; + after = [ "k3s.service" ]; + wants = [ "k3s.service" ]; + wantedBy = [ "multi-user.target" ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + Environment = "KUBECONFIG=/etc/rancher/k3s/k3s.yaml"; + ExecStart = "${pkgs.kubectl}/bin/kubectl apply -f ${agentManifest}"; }; + }; + + systemd.services.portainer-agent-k3s-secret = mkIf (cfg.mode == "agent" && cfg.agent.k3s.enable && cfg.agent.k3s.edgeKeySecretName != null) { + description = "Portainer Edge Agent k3s secret"; + after = [ "k3s.service" ]; + wants = [ "k3s.service" ]; + before = [ "portainer-agent-k3s.service" ]; + wantedBy = [ "portainer-agent-k3s.service" ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + Environment = "KUBECONFIG=/etc/rancher/k3s/k3s.yaml"; + }; + script = '' + set -eu + EDGE_KEY_FILE="${config.age.secrets.${cfg.agent.k3s.edgeKeySecretName}.path}" + if [ ! -f "$EDGE_KEY_FILE" ]; then + echo "EDGE_KEY file not found: $EDGE_KEY_FILE" >&2 + exit 1 + fi + # shellcheck source=/dev/null + . "$EDGE_KEY_FILE" + ${pkgs.kubectl}/bin/kubectl create namespace portainer --dry-run=client -o yaml | ${pkgs.kubectl}/bin/kubectl apply -f - + ${pkgs.kubectl}/bin/kubectl delete secret portainer-agent-edge-key --namespace portainer --ignore-not-found + ${pkgs.kubectl}/bin/kubectl create secret generic portainer-agent-edge-key \ + --namespace portainer \ + --from-literal="EDGE_KEY=$EDGE_KEY" \ + --from-literal="EDGE_ID=$EDGE_ID" \ + ''; + }; }; } diff --git a/secrets.nix b/secrets.nix index 7fc2f2e..cf76f3e 100644 --- a/secrets.nix +++ b/secrets.nix @@ -38,6 +38,7 @@ in # portainer agent envs "secrets/portainer-agent-bigbox1.age".publicKeys = [ ari bigbox1 ]; "secrets/portainer-agent-minipc1.age".publicKeys = [ ari minipc1 ]; + "secrets/portainer-agent-minipc3.age".publicKeys = [ ari minipc3 ]; "secrets/lab-ca-key.age".publicKeys = [ ari ]; "secrets/buzz-tls-key.age".publicKeys = [ ari minipc1 ]; "secrets/portainer-tls-key.age".publicKeys = [ ari minipc2 ]; diff --git a/secrets/portainer-agent-minipc3.age b/secrets/portainer-agent-minipc3.age new file mode 100644 index 0000000..d43f888 Binary files /dev/null and b/secrets/portainer-agent-minipc3.age differ