From 92333b017c0c77cb93137c18fd7da77306cc09fe Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Fri, 20 Nov 2026 13:43:14 -0500 Subject: [PATCH] docs(adr): add ADR-0005 for host-granted workload capabilities - Record ADR-0005 ('Compose Requests, Nix Grants') amending ADR-0002 - Define homelab.workloadGrants -> /etc/homelab/grants.json and nixos/instance.example.nix contract - Register ADR-0005 in docs/decisions/README.md --- .../0005-workload-capability-grants.md | 84 +++++++++++++++++++ docs/decisions/README.md | 10 +++ 2 files changed, 94 insertions(+) create mode 100644 docs/decisions/0005-workload-capability-grants.md diff --git a/docs/decisions/0005-workload-capability-grants.md b/docs/decisions/0005-workload-capability-grants.md new file mode 100644 index 0000000..2a77965 --- /dev/null +++ b/docs/decisions/0005-workload-capability-grants.md @@ -0,0 +1,84 @@ +--- +type: decision +status: accepted +project: homelab-core +date: 2026-11-20 +tags: [architecture, security, nixos, workloads, appctl, gitops] +amends: 0002-appctl-control-plane-architecture.md +--- + +# ADR 0005: Host-Granted Workload Capabilities ("Compose Requests, Nix Grants") + +## Context and Problem Statement + +Under [ADR-0002](0002-appctl-control-plane-architecture.md) (`DEC-04`, `DEC-05`) and [ADR-0004](0004-unified-workloads-monorepo.md), `validate_compose_security()` in `scripts/gitops_dispatcher.py` enforces a uniform deny-all pre-flight policy on Tier 2 workload `docker-compose.yml` files: + +* All `devices:` mounts are unconditionally blocked. +* All host bind mounts outside `~/Workloads/` and `~/var/lib/homelab/` are blocked. +* All host namespace sharing (`network_mode: host`, etc.) is blocked. + +While this prevents a Tier 2 `git push` from escalating privileges on the host, it creates real friction for legitimate single-operator workloads: + +1. **Hardware Acceleration (`jellyfin`)**: Using an integrated GPU (`/dev/dri`) for VAAPI transcoding and reading a dedicated media partition (`/media:ro`) are blocked unless the validator is relaxed globally or fragile host-level bind-mount workarounds are maintained. +2. **LAN Broadcast / Peer Services (e.g., `lg-smtv-mcp`)**: Workloads that send Wake-on-LAN broadcasts or discover LAN peers may require `network_mode: host` when bridged Docker networks cannot forward subnet broadcasts. +3. **Host-Agnostic Core Boundary**: Hardcoding `/dev/dri`, `/media`, or specific workload names inside `nixos-core` violates platform genericism and couples Core to one machine's hardware and app catalog. + +The central architectural question is: **How should Canopy NixOS Core permit hardware devices, external read-only paths, and host networking for specific workloads without hardcoding personal infrastructure into `nixos-core` or weakening the Tier 2 -> Tier 3 trust boundary?** + +## Decision Drivers + +* **Preserve the Trust Split**: A push to Tier 2 (`canopy-workloads`) must never be able to grant itself root-equivalent host access; capability grants must originate from root-level Tier 3 NixOS host configuration (`nixos-rebuild switch`). +* **Keep `nixos-core` Generic**: Core defines the capability schema and enforcement logic only, with zero hardcoded personal app names, device nodes, or media paths. +* **Keep `app.yaml` Free of Host Paths**: Workload manifests remain declarative routing/provenance metadata; `docker-compose.yml` already declares the requested `devices`, `volumes`, `group_add`, and `network_mode`. +* **Documented Instance Schema**: Provide a tracked `nixos/instance.example.nix` template so new hosts (or a future setup wizard) can configure `homelab.domain`, `homelab.operatorUser`, and `homelab.workloadGrants` cleanly in `/etc/nixos/homelab-instance.nix` (or gitignored `nixos/instance.nix`). +* **Fail-Closed Validation**: Absent, unreadable, or malformed grant configuration must default to zero grants (identical to today's strict baseline), and `privileged: true` and `/var/run/docker.sock` mounts remain permanently prohibited for all workloads. + +## Considered Options + +* **Option 1: Relax `validate_compose_security()` Globally**: Allow `/dev/dri` and arbitrary `:ro` paths for all workloads. +* **Option 2: Store Grants in `~/Workloads/grants.yaml`**: Allow `canopy-workloads` to declare its own exceptions alongside `deployments.yaml`. +* **Option 3: "Compose Requests, Nix Grants" via `homelab.workloadGrants` (Adopted)**: Define a typed `homelab.workloadGrants` option in `nixos-core` rendered to root-owned `/etc/homelab/grants.json`, validated with Pydantic v2 (`extra="forbid"`), and checked by `validate_compose_security()`. + +## Decision Outcome + +Chosen option: **Option 3: "Compose Requests, Nix Grants" via `homelab.workloadGrants`**, because it keeps `nixos-core` 100% hardware- and workload-agnostic while preserving the security boundary between Tier 2 Git pushes and Tier 3 `sudo nixos-rebuild switch`. + +### Summary of Architectural Commitments + +This ADR formally amends [ADR-0002](0002-appctl-control-plane-architecture.md) (`DEC-04`, `DEC-05`) with the following commitments: + +1. **Typed Host Grant Schema (`homelab.workloadGrants`)**: + * `nixos/modules/homeserver.nix` defines `homelab.workloadGrants` as an attribute set keyed by workload name, supporting four explicit grant fields: + * `devices` (list of exact device paths, e.g. `"/dev/dri"` or `"/dev/dri/renderD128"`). + * `groups` (list of supplementary group names/GIDs permitted in `group_add`). + * `readOnlyPaths` (list of host directories permitted for `:ro` bind mounts only). + * `hostNetwork` (boolean permitting `network_mode: host`). + * `privileged: true`, `/var/run/docker.sock`, `cap_add`, and `pid`/`ipc`/`uts`/`userns_mode: host` remain permanently prohibited and are not grantable. + +2. **Immutable Host Projection (`/etc/homelab/grants.json`)**: + * NixOS generates `/etc/homelab/grants.json` (root-owned, readable by `homelab-appctl` and `homelab-gitops`, configurable via `HOMELAB_GRANTS_FILE` for testing). + * If the grants file is absent, `validate_compose_security()` defaults to zero grants. If present but malformed, validation fails closed. + +3. **Exact-Match Pre-Flight Enforcement**: + * Every capability requested in `/docker-compose.yml` must match an explicit grant for `` in `/etc/homelab/grants.json`: + * `devices` entries must resolve canonically within the granted device path(s) (with no `..` traversal or container-side privilege escalation). + * `readOnlyPaths` grants permit mounts **only** when `:ro` is specified and `Path.resolve()` remains contained within the granted directory. + * `network_mode: host` is permitted **only** when `hostNetwork = true` for that workload. + +4. **Tracked Instance Template (`nixos/instance.example.nix`)**: + * `nixos-core` tracks `nixos/instance.example.nix` documenting `homelab.domain`, `homelab.operatorUser`, and `homelab.workloadGrants`. + * Live hosts keep their concrete configuration in `/etc/nixos/homelab-instance.nix` (or gitignored `nixos/instance.nix`) without build-time file translation between the two. + +## Pros and Cons of the Options + +### Option 1: Relax `validate_compose_security()` Globally +* Good, because it requires minimal code. +* Bad, because it hardcodes host hardware assumptions into `nixos-core` and cannot safely express `network_mode: host`. + +### Option 2: Store Grants in `~/Workloads/grants.yaml` +* Good, because grants live next to `deployments.yaml`. +* Bad, because Tier 2 would self-authorize Tier 3 host privileges on a single `git push`. + +### Option 3: "Compose Requests, Nix Grants" via `homelab.workloadGrants` (Adopted) +* Good, because `nixos-core` stays generic, `canopy-workloads` stays clean, and privilege elevation requires explicit host-level `nixos-rebuild switch`. +* Bad, because adding a new privileged capability for a workload requires editing `/etc/nixos/homelab-instance.nix` and rebuilding NixOS before `appctl up`. diff --git a/docs/decisions/README.md b/docs/decisions/README.md index a3b57b8..4496572 100644 --- a/docs/decisions/README.md +++ b/docs/decisions/README.md @@ -10,6 +10,7 @@ Repository-level architectural decisions are recorded in `docs/decisions/` using | Decision Record | Status | Date | Scope & One-Line Purpose | | :--- | :--- | :--- | :--- | +| [**ADR-0005: Host-Granted Workload Capabilities**](0005-workload-capability-grants.md) | `accepted` | 2026-11-20 | Amends ADR-0002 to introduce `homelab.workloadGrants` (`/etc/homelab/grants.json`) and `nixos/instance.example.nix` so NixOS hosts explicitly authorize `devices`, `groups`, `readOnlyPaths`, and `hostNetwork` per workload. | | [**ADR-0004: Unified ~/Workloads Monorepo & State Isolation**](0004-unified-workloads-monorepo.md) | `accepted` | 2026-11-16 | Amends ADR-0001 to adopt the unified `~/Workloads` Tier 2 monorepo (`canopy-workloads`), rename `~/Sites` to `~/Workloads` with `WORKLOADS_DIR` fallback, and enforce `~/var/lib/homelab//` state isolation. | | [**ADR-0003: Centralized Runtime Validation & Serialization via Pydantic v2**](0003-pydantic-v2-runtime-validation.md) | `accepted` | 2026-10-15 | Adopts Pydantic v2 as the single runtime schema and validation authority across workload manifests and daemon API to enforce fail-closed type safety, strict schema-based boundaries, and secret-safe serialization. | | [**ADR-0002: Homelab Operational Control Plane & Deployment Model Architecture**](0002-appctl-control-plane-architecture.md) | `accepted` | 2026-10-07 | Establishes `homelab-appctl.service` as the central operational control plane daemon, one logical command API (`POST /v1/operations`) across dual transports (UDS and HTTP), mandatory token auth with UDS `SO_PEERCRED` viewer fallback, dedicated service isolation, fail-closed audit logging, GitOps worker unification, and deterministic Git-backed declarative deployment state. | @@ -19,6 +20,15 @@ Repository-level architectural decisions are recorded in `docs/decisions/` using ## 🧭 Summaries of Governing Decisions +### [ADR-0005: Host-Granted Workload Capabilities ("Compose Requests, Nix Grants")](0005-workload-capability-grants.md) +* **Canonical Document**: [`0005-workload-capability-grants.md`](0005-workload-capability-grants.md) +* **Status**: `accepted` (Active — Amends ADR-0002) +* **Core Commitments**: + 1. **Typed Host Grant Schema (`homelab.workloadGrants`)**: NixOS exposes per-workload `devices`, `groups`, `readOnlyPaths`, and `hostNetwork` grants while keeping `privileged: true`, `docker.sock`, and `cap_add` permanently forbidden. + 2. **Immutable Host Projection (`/etc/homelab/grants.json`)**: Projected root-owned at rebuild time; absent file defaults to zero grants, malformed file fails closed. + 3. **Exact-Match Pre-Flight Enforcement**: `validate_compose_security()` allows requested capabilities only when canonically matched against the workload's Nix grant (and `:ro` for `readOnlyPaths`). + 4. **Tracked Instance Template (`nixos/instance.example.nix`)**: Documents `homelab.domain`, `homelab.operatorUser`, and `homelab.workloadGrants` for `/etc/nixos/homelab-instance.nix`. + ### [ADR-0004: Unified ~/Workloads Monorepo & State Isolation](0004-unified-workloads-monorepo.md) * **Canonical Document**: [`0004-unified-workloads-monorepo.md`](0004-unified-workloads-monorepo.md) * **Status**: `accepted` (Active — Amends ADR-0001)