From b6cfd45bc0ff615723df57ee73f0a529eddffbaf Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 31 Oct 2026 23:58:40 -0400 Subject: [PATCH 1/2] nix2cf: goal-file schema_version 1 -> 4 (supervisor host input, nixos passthrough host, version CI check) supervisor is a required host input (CLI --supervisor, no default) written into device-trust.entries.supervisor; a disagreeing supplied document is refused. A nixos host refuses any selected service and renders supervision deliberately-unmanaged (projects to {"vars":{}}); nixos is refused below v4. Goldens regenerated, nixos golden added, tendcf cross-file checks run in the tests, scripts/check_schema_version.py runs in CI before the tests. Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/check.yml | 5 + README.md | 26 ++- docs/DESIGN.md | 17 +- examples/site/device-trust.json | 8 +- nix2cf/cli.py | 26 ++- nix2cf/compile.py | 210 ++++++++++++++--- scripts/check_schema_version.py | 42 ++++ tests/golden/example-site_mac.goal-file.json | 2 +- tests/golden/nixos-get.goal-file.json | 1 + .../golden/tendcf-examples_mac.goal-file.json | 2 +- tests/test_compile.py | 219 +++++++++++++++++- 11 files changed, 502 insertions(+), 56 deletions(-) create mode 100644 scripts/check_schema_version.py create mode 100644 tests/golden/nixos-get.goal-file.json diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 10e2dfd..bc0b76c 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -22,6 +22,11 @@ jobs: - uses: astral-sh/setup-uv@v5 + # A tendcf schema_version bump that nix2cf has not followed fails here + # with one clear line, not as a wall of "was expected" test failures. + - name: GOAL_FILE_SCHEMA_VERSION equals tendcf's schema const + run: uv run python scripts/check_schema_version.py "$GITHUB_WORKSPACE/tendcf" + - name: Tests env: TENDCF_DIR: ${{ github.workspace }}/tendcf diff --git a/README.md b/README.md index 03c3c9b..390db12 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,18 @@ touching the host. ```sh export TENDCF_DIR=~/src/tendcf # schemas + reference projector uv run nix2cf buildfile \ - --host mac --platform macos \ + --host mac --platform macos --supervisor launchd \ --host-key ed25519:a1a1…a1 \ --device-trust examples/site/device-trust.json \ --site examples/site # services.yml, roles.yml, launchd-writers.yml ``` +`--supervisor` (`launchd`, `systemd`, `runit` or `nixos`) is required and has +no default. It is written into the goal file's `device-trust.entries.supervisor`; +a `--device-trust` document that already carries a different `supervisor` is +refused. The output is **schema_version 4** (tendcf's `goal-file.schema.json`); +CI fails when `GOAL_FILE_SCHEMA_VERSION` and tendcf's schema const differ. + That prints the host's **goal file**: the canonical (RFC 8785, no trailing newline) JSON document of guide §7 that a release carries, that consent binds to, and that `tendcf/schema/goal-file.schema.json` checks. Add `--project` to @@ -60,20 +66,28 @@ What the compile does, in order: checked, and (in the tests) projected through tendcf's reference projector with zero findings. +A `nixos` host gets no services: selecting any is refused ("host is +nixos-supervised; its units are the Nix configuration's"), and the goal file +carries `supervision: {"coverage": "deliberately-unmanaged"}` with the +`supervisor` entry, which tendcf's projector reads as `{"vars":{}}`. + `examples/site/` is tendcf's own fixtures with one edit, so an interlock renders. Fixture, not site data. ## Not here yet - **Inference** (`provides`/`requires` → ordering edges): waits until real - types exist on two platforms (guide §10, §18 Step 3), and goal-file v1 has no + types exist on two platforms (guide §10, §18 Step 3), and the goal file has no edge kind to render into. -- **Any supervisor but launchd**, and therefore any Linux or Android host - (Steps 1–2, 4). A non-macOS host is a compile error, not a guess. +- **Units for any supervisor but launchd** (Steps 1–2, 4, 6). A `systemd` or + `runit` host gets its `supervisor` entry, but a service selected for it is a + compile error, not a guess: the systemd renderer (and with it the v2 + `restart`/`wanted_by` fields) is step 6 proper. `nixos` needs no renderer, + as above. Android has no supervisor decided and is refused. - **Extra-entry reporting** (device-side, tendcf-agent) and `def.json`. - **Inventory, trust policy, peer actions** as Site Model inputs (Step 0 - remaining): the host key, platform and `device-trust` domain are passed on - the command line until those schemas exist. + remaining): the host key, platform, supervisor and `device-trust` domain are + passed on the command line until those schemas exist. - **Tombstones** from the Site Model (it cannot yet say "absent"). - **The Nix module frontend** (D12). `nix2cf eval-nix FILE` evaluates a plain Nix expression with `nix-instantiate --eval --strict --json`; the test for diff --git a/docs/DESIGN.md b/docs/DESIGN.md index 2169f8d..7ff135b 100644 --- a/docs/DESIGN.md +++ b/docs/DESIGN.md @@ -26,13 +26,18 @@ oracle for the one that exists is `tendcf/examples/host_specific.json`. - *Dependency inference* (`provides`/`requires` → ordering edges). Guide §10: "Inference does not start until real type definitions exist on two - platforms." Goal-file v1 also has no edge kind (reconciliation §8), so there + platforms." The goal file also has no edge kind (reconciliation §8), so there is nothing to render an edge into yet. `provides`/`requires` are used only by the conflict check (two providers of one token on one host) and to report unmatched `requires` as notes. -- *Every supervisor except launchd.* The goal file's `unit` admits launchd, - systemd and runit; only the launchd adapter exists (guide §4, §18 Step 1). - A linux/android host is a compile error here, not a guess. +- *Every supervisor's units except launchd's.* The goal file's `unit` admits + launchd, systemd and runit; only the launchd adapter exists (guide §4, §18 + Step 1). A systemd or runit host is stated (its `supervisor` entry) but any + service selected for it is a compile error here, not a guess. The systemd + renderer is linux-targets step 6 proper, so none of the v2 systemd fields + (`restart`, `wanted_by`) or the systemd argv/`working_dir` alphabet is + written by nix2cf yet. A `nixos` host is complete: it has no units to + render (see below). - *Android/Termux* (Step 2) — no Termux unit flavor exists in the goal file. - *Extra-entry reporting* — device-side, needs tendcf-agent. - *def.json* — MPF glue under the policy-tree digest, never per-host @@ -90,6 +95,10 @@ Each of these is marked `TODO(operator)` in `nix2cf/compile.py` at the spot. | Unit-writer registry | Every declared prefix is carried (the device runs extra-entry detection without the Site Model, reconciliation §8). | — | | Unmatched `requires` | A note, and `path:`/`secret:` kinds only counted (their providers are site registries, not Site Model records). | Guide §15 says a compile error with near-misses once the lookup exists. | | `device-trust`, host key, platform | Supplied by the caller (`--device-trust`, `--host-key`, `--platform`). | Inventory and trust-policy shapes are "Step 0 remaining". | +| `supervisor` | A required host input (`--supervisor launchd\|systemd\|runit\|nixos`) next to `platform`, **no default** (house rule; spec Q1/Q8). Written into `device-trust.entries.supervisor` as `{"host":{"name":X,"state":"present"}}`. A supplied `device-trust` document whose `supervisor` entry differs is refused; one that omits it is fine. | No inventory schema exists (spec Q8); the flag goes when it does. | +| Platform ↔ supervisor | `macos` has `launchd`; `linux` has `systemd`, `runit` or `nixos`; any other pairing, and `android`, is refused. | Not in the spec. Android/Termux is deferred (linux-targets ruling 2026-10-29), so `android` is refused rather than mapped to runit. | +| `nixos` host | Any selected service is refused ("host is nixos-supervised; its units are the Nix configuration's"). With none, the goal file carries `supervision: {"coverage": "deliberately-unmanaged"}` and no unit-writer registry; tendcf's projector reads it as `{"vars":{}}`. | Spec §6.2 item 3, Q3. | +| Older `schema_version` | Library only (`compile_host(..., schema_version=N)`): 3 renders without the `supervisor` entry; a `nixos` host is refused below 4; anything else is refused. The CLI always renders the current version, and an older file is not schema-validated (tendcf's schema is a const). | Spec §6.2 item 6. | | Tombstones | Cannot be rendered: the Site Model has no way to say "absent". | Goal file supports them; the Site Model does not yet. | ## Conflict check (stage 2) diff --git a/examples/site/device-trust.json b/examples/site/device-trust.json index 4873953..fccfedc 100644 --- a/examples/site/device-trust.json +++ b/examples/site/device-trust.json @@ -19,6 +19,12 @@ "state": "present" } }, + "supervisor": { + "host": { + "name": "launchd", + "state": "present" + } + }, "trust-policy": { "consent": { "local_yes_required": true, @@ -27,4 +33,4 @@ } } } -} \ No newline at end of file +} diff --git a/nix2cf/cli.py b/nix2cf/cli.py index e298103..5da9a52 100644 --- a/nix2cf/cli.py +++ b/nix2cf/cli.py @@ -1,7 +1,7 @@ """`nix2cf` command line. - nix2cf buildfile --host mac --host-key ed25519:… --device-trust trust.json \ - --site examples/site [--tendcf ~/src/tendcf] [--project] [-o OUT] + nix2cf buildfile --host mac --platform macos --supervisor launchd \ + --host-key ed25519:… --device-trust trust.json --site examples/site [--tendcf ~/src/tendcf] [--project] [-o OUT] Renders exactly what one host would receive — the Bcfg2 `buildfile` shape the guide (§4) and the paper (§6.2) say to build first — without touching the @@ -33,7 +33,7 @@ from typing import Any from . import __version__ -from .compile import CompileError, HostSpec, compile_host +from .compile import SUPERVISORS, CompileError, HostSpec, compile_host from .sitemodel import SITE_FILES, Schemas, SiteModel, SiteModelError, load_document, load_site_model, validate_site_model @@ -88,7 +88,13 @@ def cmd_buildfile(args: argparse.Namespace) -> int: print(f"nix2cf: {len(problems)} Site Model schema finding(s); not compiled", file=sys.stderr) return 1 - host = HostSpec(name=args.host, platform=args.platform, key=args.host_key, device_trust=trust_doc.data) + host = HostSpec( + name=args.host, + platform=args.platform, + supervisor=args.supervisor, + key=args.host_key, + device_trust=trust_doc.data, + ) try: result = compile_host(model, host, schemas) except CompileError as exc: @@ -151,7 +157,17 @@ def build_parser() -> argparse.ArgumentParser: b.add_argument("--host", required=True, help="host name as used in the Site Model (hosts:, roles)") b.add_argument("--host-key", required=True, help="device public key, ed25519:<64 hex>") b.add_argument("--platform", default="macos", choices=["macos", "linux", "android"]) - b.add_argument("--device-trust", required=True, help="JSON/YAML file holding the goal file's device-trust domain verbatim") + b.add_argument( + "--supervisor", + required=True, + choices=list(SUPERVISORS), + help="what runs the host's units; no default. Written into device-trust.entries.supervisor", + ) + b.add_argument( + "--device-trust", + required=True, + help="JSON/YAML file holding the goal file's device-trust domain; its supervisor entry, if any, must match --supervisor", + ) b.add_argument("--site", help="directory holding services.yml, roles.yml, launchd-writers.yml") b.add_argument("--services") b.add_argument("--roles") diff --git a/nix2cf/compile.py b/nix2cf/compile.py index 6fcae64..76dda6d 100644 --- a/nix2cf/compile.py +++ b/nix2cf/compile.py @@ -16,8 +16,8 @@ resource, every writer and location, the values, and a resolution. Never last-wins; 3. inference — NOT implemented. Guide §10: "Inference does not start until - real type definitions exist on two platforms", and goal-file - v1 carries no edge kind to render into (reconciliation §8); + real type definitions exist on two platforms", and the goal + file carries no edge kind to render into (reconciliation §8); 4. render — RFC 8785 (JCS) bytes, no trailing newline, no defaults, no prose, no floats, no nulls (reconciliation §2.1). @@ -37,7 +37,17 @@ from .sitemodel import GOAL_FILE_SCHEMA, Document, Schemas, SiteModel -GOAL_FILE_SCHEMA_VERSION = 1 +# Must equal the `const` of `tendcf/schema/goal-file.schema.json` +# `schema_version`; CI fails when it does not (.github/workflows/check.yml). +GOAL_FILE_SCHEMA_VERSION = 4 +# `supervisor` entered the device-trust domain at v4 (schema-v4 spec §2.1). +# The `nixos` host exists only from v4 on (spec §6.2 item 6): the one v3 +# spelling, a `nixos` unit-writer, described nothing. +SUPERVISOR_SCHEMA_VERSION = 4 +# Older versions this compiler can still state. The launchd shape did not +# change between v1 and v4; only v3 and v4 are claimed, because v3 is the one +# the spec names ("Non-nixos hosts may render at 3 by dropping the entry"). +SUPPORTED_SCHEMA_VERSIONS = (3, 4) # The one state domain the reference projector reads # (`tendcf/bin/projector.py:PROJECTING_DOMAIN`). The Site Model's own domain @@ -53,11 +63,26 @@ SUPERVISION_DOMAIN = "supervision" HOST_KEY = re.compile(r"^ed25519:[0-9a-f]{64}\Z") -# `launchd` is the only adapter that exists (guide §4: "It handles launchd +# The closed set of `device-trust.entries.supervisor.host.name` +# (goal-file.schema.json `supervisor_map`). Required input, no default (spec +# Q1, house rule "no defaults"): the compiler never guesses what runs units. +SUPERVISORS = ("launchd", "systemd", "runit", "nixos") +# Which supervisors a platform can have. A mismatch is a compile error, not a +# guess. TODO(operator): Android/Termux is deferred (linux-targets.md ruling +# 2026-10-29), so `android` has no row and is refused rather than mapped to +# runit; inventory (spec Q8) will carry both facts and make this table moot. +PLATFORM_SUPERVISORS = { + "macos": ("launchd",), + "linux": ("systemd", "runit", "nixos"), +} +# `launchd` is the only adapter that renders (guide §4: "It handles launchd # only"; §18 Step 1). The goal file's `unit` oneOf also spells systemd and -# runit, but nothing renders them yet, so a host on another platform is a -# compile error, not a guess. -PLATFORM_SUPERVISOR = {"macos": "launchd"} +# runit, but the systemd/runit renderer is step 6 proper, so a host on one of +# those with a selected service is a compile error, not a guess. (So nix2cf +# writes none of the v2 systemd `restart`/`wanted_by` fields or the systemd +# argv/working_dir alphabet: there is no systemd unit to put them in.) +RENDERED_SUPERVISORS = frozenset({"launchd"}) +NIXOS_REFUSAL = "host is nixos-supervised; its units are the Nix configuration's" # Per-supervisor default for the REQUIRED `working_dir` (goal-file schema: # "The compiler resolves the per-supervisor default ('/' for launchd and @@ -103,12 +128,17 @@ class HostSpec: name: str platform: str + # Host input next to `platform` (spec Q8), required: no default. Written + # into `device-trust.entries.supervisor`; one of SUPERVISORS. + supervisor: str key: str - # The goal file's `device-trust` domain, verbatim. The compiler cannot + # The goal file's `device-trust` domain, as supplied. The compiler cannot # derive it: the trust-policy shape is not yet in the Site Model (guide # §18 Step 0 remaining), and the digests (policy tree, agent binary) come - # from the release path (Step 6). Validated as part of the whole goal - # file against goal-file.schema.json. + # from the release path (Step 6). The compiler adds the `supervisor` + # entry from `supervisor` above; a supplied `supervisor` entry that + # differs is refused. Validated as part of the whole goal file against + # goal-file.schema.json. device_trust: dict[str, Any] @@ -322,22 +352,134 @@ def _coverage(domain: dict[str, Any]) -> str: return domain["opt_out_reason"] -def render_goal_file(model: SiteModel, host: HostSpec, selected: list[Selected]) -> tuple[dict[str, Any], list[str]]: - """Build the goal-file document. Returns (document, errors).""" +def _supplied_supervisor_error(host: HostSpec) -> str | None: + """A supervisor entry in the supplied device-trust document that is not exactly + the one this host input implies: two spellings of one fact, one of them wrong.""" + entries = host.device_trust.get("entries") if isinstance(host.device_trust, dict) else None + if not isinstance(entries, dict) or "supervisor" not in entries: + return None + wanted = {"host": {"name": host.supervisor, "state": "present"}} + if entries["supervisor"] == wanted: + return None + return ( + f"the supplied device-trust document carries supervisor entry " + f"{entries['supervisor']!r}, but the host input says {host.supervisor!r} " + f"(expected {wanted!r}). Resolution: make the document and --supervisor agree, " + "or drop the entry from the document" + ) + + +def host_errors(host: HostSpec, schema_version: int = GOAL_FILE_SCHEMA_VERSION) -> list[str]: + """Refusals that depend on the host's own facts, not on the Site Model.""" errors: list[str] = [] - supervisor = PLATFORM_SUPERVISOR.get(host.platform) - if supervisor is None: - return {}, [ - f"host {host.name!r} is {host.platform!r}; the only supervisor adapter " - f"that exists is launchd (macos). No goal file rendered" + if not HOST_KEY.match(host.key): + errors.append(f"host key {host.key!r} is not `ed25519:` + 64 lowercase hex") + if schema_version not in SUPPORTED_SCHEMA_VERSIONS: + errors.append( + f"schema_version {schema_version!r} is not one this compiler renders " + f"(supported: {list(SUPPORTED_SCHEMA_VERSIONS)})" + ) + if host.supervisor not in SUPERVISORS: + errors.append( + f"host {host.name!r}: supervisor {host.supervisor!r} is not one of " + f"{list(SUPERVISORS)}. Resolution: state the host's supervisor; there is no default" + ) + else: + supplied = _supplied_supervisor_error(host) + if supplied is not None: + errors.append(supplied) + allowed = PLATFORM_SUPERVISORS.get(host.platform) + if allowed is None: + errors.append( + f"host {host.name!r} is {host.platform!r}; no supervisor is decided for " + f"that platform (known: {sorted(PLATFORM_SUPERVISORS)})" + ) + elif host.supervisor not in allowed: + errors.append( + f"host {host.name!r} is {host.platform!r} with supervisor " + f"{host.supervisor!r}; a {host.platform} host has one of {list(allowed)}. " + "Resolution: fix --platform or --supervisor" + ) + if host.supervisor == "nixos" and schema_version < SUPERVISOR_SCHEMA_VERSION: + errors.append( + f"host {host.name!r} has supervisor 'nixos', which schema_version " + f"{schema_version} cannot state (it exists from " + f"{SUPERVISOR_SCHEMA_VERSION}; the only older spelling, an `org.nixos.*` " + "unit-writer, described nothing). Resolution: render at " + f"schema_version {SUPERVISOR_SCHEMA_VERSION}" + ) + return errors + + +def supervisor_refusals(host: HostSpec, selected: list[Selected]) -> list[str]: + """Selected services this host's supervisor cannot be given a unit for.""" + if not selected or host.supervisor in RENDERED_SUPERVISORS: + return [] + names = ", ".join(f"{s.record['name']!r} ({s.where})" for s in selected) + if host.supervisor == "nixos": + return [ + f"host {host.name!r}: {NIXOS_REFUSAL}; refusing selected service(s): {names}. " + "Resolution: move the service off this host, or declare it in the NixOS configuration" ] + return [ + f"host {host.name!r} has supervisor {host.supervisor!r}, whose unit renderer is " + f"not built (tendcf linux-targets step 6); refusing selected service(s): {names}" + ] + - domains: dict[str, Any] = {"device-trust": copy.deepcopy(host.device_trust)} +def _trust_with_supervisor(host: HostSpec, schema_version: int) -> tuple[dict[str, Any], list[str]]: + """The device-trust domain with the host's `supervisor` entry stated. + + A disagreeing supplied entry is refused up front by host_errors(); here + it is refused again so render_goal_file stays safe on its own. Below v4 + the entry does not exist, so it is dropped (a matching one only). + """ + trust = copy.deepcopy(host.device_trust) + entries = trust.get("entries") if isinstance(trust, dict) else None + if not isinstance(entries, dict): + return trust, ["the supplied device-trust document has no `entries` object"] + supplied = _supplied_supervisor_error(host) + if supplied is not None: + return trust, [supplied] + wanted = {"host": {"name": host.supervisor, "state": "present"}} + if schema_version >= SUPERVISOR_SCHEMA_VERSION: + entries["supervisor"] = wanted + else: + entries.pop("supervisor", None) + return trust, [] + + +def render_goal_file( + model: SiteModel, + host: HostSpec, + selected: list[Selected], + schema_version: int = GOAL_FILE_SCHEMA_VERSION, +) -> tuple[dict[str, Any], list[str]]: + """Build the goal-file document. Returns (document, errors). + + Expects `host_errors(host, schema_version)` to be empty (compile_host checks). + """ + errors: list[str] = supervisor_refusals(host, selected) + if errors: + return {}, errors + supervisor = host.supervisor + + trust, errs = _trust_with_supervisor(host, schema_version) + if errs: + return {}, errs + domains: dict[str, Any] = {"device-trust": trust} doc: dict[str, Any] = { - "schema_version": GOAL_FILE_SCHEMA_VERSION, + "schema_version": schema_version, "host": host.key, "domains": domains, } + if supervisor == "nixos": + # Q3: the NixOS configuration is the one unit writer; tendcf reports + # and refuses there. `deliberately-unmanaged` with no entries is what + # the schema's nixos member requires, and what the projector reads as + # `{"vars":{}}`. + domains[SUPERVISION_DOMAIN] = {"coverage": "deliberately-unmanaged"} + return doc, errors if not selected: return doc, errors @@ -498,16 +640,27 @@ def canonical_bytes(doc: dict[str, Any]) -> bytes: raise CompileError([f"goal file carries a value JCS cannot represent: {type(exc).__name__}: {exc}"]) from exc -def compile_host(model: SiteModel, host: HostSpec, schemas: Schemas | None = None) -> CompileResult: - """The whole path for one host. Raises CompileError; never emits a wrong file.""" - errors: list[str] = [] +def compile_host( + model: SiteModel, + host: HostSpec, + schemas: Schemas | None = None, + schema_version: int = GOAL_FILE_SCHEMA_VERSION, +) -> CompileResult: + """The whole path for one host. Raises CompileError; never emits a wrong file. + + `schema_version` is the library's only way to ask for an older file (spec + §6.2 item 6): the CLI always renders the current one. `schemas` validates + against tendcf's current schema only, so an older version is not + schema-checked here. + """ + errors: list[str] = host_errors(host, schema_version) diagnostics: list[str] = [] - if not HOST_KEY.match(host.key): - errors.append(f"host key {host.key!r} is not `ed25519:` + 64 lowercase hex") selected, diag, errs = select_services(model, host) diagnostics += diag errors += errs + if host.supervisor in SUPERVISORS: + errors += supervisor_refusals(host, selected) if errors: raise CompileError(errors) @@ -517,7 +670,7 @@ def compile_host(model: SiteModel, host: HostSpec, schemas: Schemas | None = Non if errors: raise CompileError(errors) - doc, errs = render_goal_file(model, host, selected) + doc, errs = render_goal_file(model, host, selected, schema_version) errors += errs if errors: raise CompileError(errors) @@ -526,7 +679,12 @@ def compile_host(model: SiteModel, host: HostSpec, schemas: Schemas | None = Non if bad: raise CompileError([f"not NFC-normalized (JCS cannot see this; reconciliation §2.1): {p}" for p in bad]) - if schemas is not None: + if schemas is not None and schema_version != GOAL_FILE_SCHEMA_VERSION: + diagnostics.append( + f"rendered at schema_version {schema_version}: NOT validated, the tendcf " + f"schema in hand is version {GOAL_FILE_SCHEMA_VERSION}" + ) + elif schemas is not None: problems = schemas.errors(GOAL_FILE_SCHEMA, doc) if problems: raise CompileError( diff --git a/scripts/check_schema_version.py b/scripts/check_schema_version.py new file mode 100644 index 0000000..2a4edb2 --- /dev/null +++ b/scripts/check_schema_version.py @@ -0,0 +1,42 @@ +"""Fail unless nix2cf renders the goal-file version tendcf's schema requires. + + uv run python scripts/check_schema_version.py TENDCF_DIR + +`nix2cf.compile.GOAL_FILE_SCHEMA_VERSION` must equal the `const` of +`schema_version` in `TENDCF_DIR/schema/goal-file.schema.json`. A tendcf bump +that nix2cf has not followed otherwise shows up only as a wall of +"/schema_version: N was expected" test failures. +""" + +from __future__ import annotations + +import json +import sys +from pathlib import Path + +from nix2cf.compile import GOAL_FILE_SCHEMA_VERSION + + +def main(argv: list[str]) -> int: + if len(argv) != 2: + print("usage: check_schema_version.py TENDCF_DIR", file=sys.stderr) + return 2 + path = Path(argv[1]) / "schema" / "goal-file.schema.json" + try: + const = json.loads(path.read_text())["properties"]["schema_version"]["const"] + except (OSError, ValueError, KeyError) as exc: + print(f"check_schema_version: cannot read the schema_version const from {path}: {exc!r}", file=sys.stderr) + return 2 + if const != GOAL_FILE_SCHEMA_VERSION: + print( + f"check_schema_version: FAIL: nix2cf renders schema_version {GOAL_FILE_SCHEMA_VERSION} " + f"but {path} requires {const}", + file=sys.stderr, + ) + return 1 + print(f"check_schema_version: ok: both are {const}") + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/tests/golden/example-site_mac.goal-file.json b/tests/golden/example-site_mac.goal-file.json index bd1ec37..2ba357e 100644 --- a/tests/golden/example-site_mac.goal-file.json +++ b/tests/golden/example-site_mac.goal-file.json @@ -1 +1 @@ -{"domains":{"device-trust":{"coverage":"comprehensive","entries":{"advisor-key":{"ed25519:b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2":{"state":"present"}},"agent":{"tendcf-agent":{"sha256":"sha256:d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4","state":"present","version":"ops-v1.0.0"}},"policy-tree":{"tree":{"sha256":"sha256:c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3","state":"present"}},"trust-policy":{"consent":{"local_yes_required":true,"state":"present","tier":"consented"}}}},"supervision":{"coverage":"comprehensive","entries":{"interlock":{"tailscale-authenticated-before-lockdown":{"blocks":"enclosing-bundle","bundle":"fleet-vpn","defines_class":"tailscale_authenticated","pre_action":{"command":["tailscale","status","--json"],"expect_exit":0,"timeout_seconds":15},"report":true,"state":"present"}},"service":{"com.djbclark.caddy":{"bundle":"edge-http","command":["/opt/homebrew/bin/caddy","run","--config","/etc/caddy/Caddyfile"],"run_as":"djbclark","state":"present","unit":{"launchd":{"keep_alive":true,"run_at_load":true}},"working_dir":"/"},"com.djbclark.litellm":{"bundle":"edge-http","command":["/opt/homebrew/bin/litellm","--config","/etc/litellm/config.yaml"],"env":{"LITELLM_MASTER_KEY":"LITELLM_MASTER_KEY","OPENAI_API_KEY":"OPENAI_API_KEY"},"run_as":"djbclark","state":"present","unit":{"launchd":{"keep_alive":false,"run_at_load":false}},"working_dir":"/"},"org.tendcf.tailscaled":{"bundle":"fleet-vpn","command":["/usr/local/bin/tailscaled"],"run_as":"root","state":"present","unit":{"launchd":{"keep_alive":false,"run_at_load":false}},"working_dir":"/"}},"unit-writer":{"com.apple.*":{"state":"present","writer":"apple"},"com.djbclark.*":{"state":"present","writer":"cfengine"},"dev.mise.*":{"state":"present","writer":"mise"},"org.nixos.*":{"state":"present","writer":"nix-darwin"},"org.tendcf.*":{"state":"present","writer":"cfengine"}}}}},"host":"ed25519:a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1","schema_version":1} \ No newline at end of file +{"domains":{"device-trust":{"coverage":"comprehensive","entries":{"advisor-key":{"ed25519:b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2":{"state":"present"}},"agent":{"tendcf-agent":{"sha256":"sha256:d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4","state":"present","version":"ops-v1.0.0"}},"policy-tree":{"tree":{"sha256":"sha256:c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3","state":"present"}},"supervisor":{"host":{"name":"launchd","state":"present"}},"trust-policy":{"consent":{"local_yes_required":true,"state":"present","tier":"consented"}}}},"supervision":{"coverage":"comprehensive","entries":{"interlock":{"tailscale-authenticated-before-lockdown":{"blocks":"enclosing-bundle","bundle":"fleet-vpn","defines_class":"tailscale_authenticated","pre_action":{"command":["tailscale","status","--json"],"expect_exit":0,"timeout_seconds":15},"report":true,"state":"present"}},"service":{"com.djbclark.caddy":{"bundle":"edge-http","command":["/opt/homebrew/bin/caddy","run","--config","/etc/caddy/Caddyfile"],"run_as":"djbclark","state":"present","unit":{"launchd":{"keep_alive":true,"run_at_load":true}},"working_dir":"/"},"com.djbclark.litellm":{"bundle":"edge-http","command":["/opt/homebrew/bin/litellm","--config","/etc/litellm/config.yaml"],"env":{"LITELLM_MASTER_KEY":"LITELLM_MASTER_KEY","OPENAI_API_KEY":"OPENAI_API_KEY"},"run_as":"djbclark","state":"present","unit":{"launchd":{"keep_alive":false,"run_at_load":false}},"working_dir":"/"},"org.tendcf.tailscaled":{"bundle":"fleet-vpn","command":["/usr/local/bin/tailscaled"],"run_as":"root","state":"present","unit":{"launchd":{"keep_alive":false,"run_at_load":false}},"working_dir":"/"}},"unit-writer":{"com.apple.*":{"state":"present","writer":"apple"},"com.djbclark.*":{"state":"present","writer":"cfengine"},"dev.mise.*":{"state":"present","writer":"mise"},"org.nixos.*":{"state":"present","writer":"nix-darwin"},"org.tendcf.*":{"state":"present","writer":"cfengine"}}}}},"host":"ed25519:a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1","schema_version":4} \ No newline at end of file diff --git a/tests/golden/nixos-get.goal-file.json b/tests/golden/nixos-get.goal-file.json new file mode 100644 index 0000000..48453ed --- /dev/null +++ b/tests/golden/nixos-get.goal-file.json @@ -0,0 +1 @@ +{"domains":{"device-trust":{"coverage":"comprehensive","entries":{"advisor-key":{"ed25519:b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2":{"state":"present"}},"agent":{"tendcf-agent":{"sha256":"sha256:d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4","state":"present","version":"ops-v1.0.0"}},"policy-tree":{"tree":{"sha256":"sha256:c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3","state":"present"}},"supervisor":{"host":{"name":"nixos","state":"present"}},"trust-policy":{"consent":{"local_yes_required":true,"state":"present","tier":"consented"}}}},"supervision":{"coverage":"deliberately-unmanaged"}},"host":"ed25519:a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1","schema_version":4} \ No newline at end of file diff --git a/tests/golden/tendcf-examples_mac.goal-file.json b/tests/golden/tendcf-examples_mac.goal-file.json index d84ca3e..53c41e4 100644 --- a/tests/golden/tendcf-examples_mac.goal-file.json +++ b/tests/golden/tendcf-examples_mac.goal-file.json @@ -1 +1 @@ -{"domains":{"device-trust":{"coverage":"comprehensive","entries":{"advisor-key":{"ed25519:b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2":{"state":"present"}},"agent":{"tendcf-agent":{"sha256":"sha256:d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4","state":"present","version":"ops-v1.0.0"}},"policy-tree":{"tree":{"sha256":"sha256:c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3","state":"present"}},"trust-policy":{"consent":{"local_yes_required":true,"state":"present","tier":"consented"}}}},"supervision":{"coverage":"comprehensive","entries":{"service":{"com.djbclark.caddy":{"bundle":"edge-http","command":["/opt/homebrew/bin/caddy","run","--config","/etc/caddy/Caddyfile"],"run_as":"djbclark","state":"present","unit":{"launchd":{"keep_alive":true,"run_at_load":true}},"working_dir":"/"},"com.djbclark.litellm":{"bundle":"edge-http","command":["/opt/homebrew/bin/litellm","--config","/etc/litellm/config.yaml"],"env":{"LITELLM_MASTER_KEY":"LITELLM_MASTER_KEY","OPENAI_API_KEY":"OPENAI_API_KEY"},"run_as":"djbclark","state":"present","unit":{"launchd":{"keep_alive":false,"run_at_load":false}},"working_dir":"/"}},"unit-writer":{"com.apple.*":{"state":"present","writer":"apple"},"com.djbclark.*":{"state":"present","writer":"cfengine"},"dev.mise.*":{"state":"present","writer":"mise"},"org.nixos.*":{"state":"present","writer":"nix-darwin"},"org.tendcf.*":{"state":"present","writer":"cfengine"}}}}},"host":"ed25519:a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1","schema_version":1} \ No newline at end of file +{"domains":{"device-trust":{"coverage":"comprehensive","entries":{"advisor-key":{"ed25519:b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2":{"state":"present"}},"agent":{"tendcf-agent":{"sha256":"sha256:d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4","state":"present","version":"ops-v1.0.0"}},"policy-tree":{"tree":{"sha256":"sha256:c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3","state":"present"}},"supervisor":{"host":{"name":"launchd","state":"present"}},"trust-policy":{"consent":{"local_yes_required":true,"state":"present","tier":"consented"}}}},"supervision":{"coverage":"comprehensive","entries":{"service":{"com.djbclark.caddy":{"bundle":"edge-http","command":["/opt/homebrew/bin/caddy","run","--config","/etc/caddy/Caddyfile"],"run_as":"djbclark","state":"present","unit":{"launchd":{"keep_alive":true,"run_at_load":true}},"working_dir":"/"},"com.djbclark.litellm":{"bundle":"edge-http","command":["/opt/homebrew/bin/litellm","--config","/etc/litellm/config.yaml"],"env":{"LITELLM_MASTER_KEY":"LITELLM_MASTER_KEY","OPENAI_API_KEY":"OPENAI_API_KEY"},"run_as":"djbclark","state":"present","unit":{"launchd":{"keep_alive":false,"run_at_load":false}},"working_dir":"/"}},"unit-writer":{"com.apple.*":{"state":"present","writer":"apple"},"com.djbclark.*":{"state":"present","writer":"cfengine"},"dev.mise.*":{"state":"present","writer":"mise"},"org.nixos.*":{"state":"present","writer":"nix-darwin"},"org.tendcf.*":{"state":"present","writer":"cfengine"}}}}},"host":"ed25519:a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1","schema_version":4} \ No newline at end of file diff --git a/tests/test_compile.py b/tests/test_compile.py index c859cac..60a6210 100644 --- a/tests/test_compile.py +++ b/tests/test_compile.py @@ -10,6 +10,7 @@ from __future__ import annotations import copy +import importlib.util import json import os import shutil @@ -21,7 +22,7 @@ import yaml from nix2cf.cli import load_projector, main -from nix2cf.compile import CompileError, HostSpec, compile_host +from nix2cf.compile import GOAL_FILE_SCHEMA_VERSION, CompileError, HostSpec, compile_host from nix2cf.sitemodel import Schemas, SiteModel, load_document, load_site_model, validate_site_model REPO = Path(__file__).resolve().parent.parent @@ -55,13 +56,51 @@ def projector(tendcf: Path): return load_projector(tendcf) +@pytest.fixture(scope="session") +def lint(tendcf: Path): + """tendcf's schema_lint, for its goal-file cross-file rules (no copy here).""" + spec = importlib.util.spec_from_file_location("tendcf_schema_lint", tendcf / "bin" / "schema_lint.py") + assert spec is not None and spec.loader is not None + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def _cross_file_findings(lint, doc: dict) -> list: + """Run tendcf's own goal cross-file checks over one rendered goal file.""" + with lint.capture_findings() as found: + lint.check_goal_file_cross_file({"goal-file.json": doc}, "goal-file.json") + return list(found) + + def _model(site: Path) -> SiteModel: return load_site_model(site / "services.yml", site / "roles.yml", site / "launchd-writers.yml") -def _host(site: Path, name: str = "mac", platform: str = "macos") -> HostSpec: +def _trust(site: Path, supervisor: str | None = "launchd") -> dict: + """The example device-trust domain, with its supervisor entry set, or removed if None.""" trust = load_document(site / "device-trust.json").data - return HostSpec(name=name, platform=platform, key=HOST_KEY, device_trust=trust) + if supervisor is None: + del trust["entries"]["supervisor"] + else: + trust["entries"]["supervisor"] = {"host": {"name": supervisor, "state": "present"}} + return trust + + +def _host( + site: Path, + name: str = "mac", + platform: str = "macos", + supervisor: str = "launchd", + trust: dict | None = None, +) -> HostSpec: + if trust is None: + trust = _trust(site, supervisor) + return HostSpec(name=name, platform=platform, supervisor=supervisor, key=HOST_KEY, device_trust=trust) + + +def _nixos_host(name: str = "get", trust: dict | None = None) -> HostSpec: + return _host(SITE, name=name, platform="linux", supervisor="nixos", trust=trust or _trust(SITE, None)) def _check_golden(name: str, raw: bytes) -> None: @@ -81,8 +120,9 @@ def test_site_model_inputs_validate(schemas: Schemas) -> None: def test_example_site_mac_golden(schemas: Schemas) -> None: result = compile_host(_model(SITE), _host(SITE), schemas) doc = result.goal_file - assert doc["schema_version"] == 1 + assert doc["schema_version"] == 4 assert doc["host"] == HOST_KEY + assert doc["domains"]["device-trust"]["entries"]["supervisor"] == {"host": {"name": "launchd", "state": "present"}} sup = doc["domains"]["supervision"] assert sup["coverage"] == "comprehensive" assert set(sup["entries"]["service"]) == { @@ -130,6 +170,22 @@ def test_output_validates_and_is_canonical(schemas: Schemas) -> None: assert rfc8785.dumps(json.loads(result.canonical)) == result.canonical +def test_schema_version_matches_tendcf(tendcf: Path) -> None: + """The renderer's version is tendcf's schema const (CI checks this too).""" + raw = json.loads((tendcf / "schema" / "goal-file.schema.json").read_text()) + assert GOAL_FILE_SCHEMA_VERSION == raw["properties"]["schema_version"]["const"] + + +def test_rendered_files_pass_tendcf_cross_file_checks(tendcf: Path, schemas: Schemas, lint) -> None: + docs = [ + compile_host(_model(SITE), _host(SITE), schemas).goal_file, + compile_host(_model(tendcf / "examples"), _host(SITE), schemas).goal_file, + compile_host(_model(SITE), _nixos_host(), schemas).goal_file, + ] + for doc in docs: + assert _cross_file_findings(lint, doc) == [] + + def test_projects_through_tendcf_projector(schemas: Schemas, projector) -> None: """The device-side projection accepts what the compiler rendered.""" result = compile_host(_model(SITE), _host(SITE), schemas) @@ -152,7 +208,7 @@ def test_pure_and_order_independent(schemas: Schemas) -> None: def test_role_main_receives_backup_does_not(schemas: Schemas) -> None: model = _model(SITE) - vps = HostSpec(name="vps-1", platform="macos", key=HOST_KEY, device_trust=_host(SITE).device_trust) + vps = _host(SITE, name="vps-1") result = compile_host(model, vps, schemas) assert "supervision" not in result.goal_file["domains"] # nothing selected assert any("backup/peer of role 'llm-gateway'" in d for d in result.diagnostics) @@ -166,19 +222,128 @@ def test_non_cfengine_service_is_skipped_loudly(schemas: Schemas) -> None: assert any("managed_by 'external'" in d for d in result.diagnostics) -def test_unknown_platform_refused(schemas: Schemas) -> None: +def test_service_platform_mismatch_refused(schemas: Schemas) -> None: with pytest.raises(CompileError) as exc: - compile_host(_model(SITE), _host(SITE, platform="linux"), schemas) + compile_host(_model(SITE), _host(SITE, platform="linux", supervisor="systemd"), schemas) assert "platform is 'macos' and the host is 'linux'" in str(exc.value) def test_bad_host_key_refused(schemas: Schemas) -> None: - host = HostSpec(name="mac", platform="macos", key="ed25519:short", device_trust=_host(SITE).device_trust) + host = HostSpec( + name="mac", platform="macos", supervisor="launchd", key="ed25519:short", device_trust=_trust(SITE) + ) with pytest.raises(CompileError) as exc: compile_host(_model(SITE), host, schemas) assert "64 lowercase hex" in str(exc.value) +# --- supervisor (schema v4) ----------------------------------------------------------- + +def test_supervisor_is_written_when_the_document_omits_it(schemas: Schemas) -> None: + host = _host(SITE, trust=_trust(SITE, None)) + assert "supervisor" not in host.device_trust["entries"] + doc = compile_host(_model(SITE), host, schemas).goal_file + assert doc["domains"]["device-trust"]["entries"]["supervisor"] == {"host": {"name": "launchd", "state": "present"}} + assert "supervisor" not in host.device_trust["entries"] # the input is not mutated + + +def test_supplied_supervisor_that_disagrees_is_refused(schemas: Schemas) -> None: + host = _host(SITE, trust=_trust(SITE, "systemd")) # document says systemd, host input says launchd + with pytest.raises(CompileError) as exc: + compile_host(_model(SITE), host, schemas) + assert "carries supervisor entry" in str(exc.value) and "'systemd'" in str(exc.value) + + +def test_supplied_malformed_supervisor_is_refused(schemas: Schemas) -> None: + trust = _trust(SITE) + trust["entries"]["supervisor"] = {"main": {"name": "launchd", "state": "present"}} # id is not `host` + with pytest.raises(CompileError, match="carries supervisor entry"): + compile_host(_model(SITE), _host(SITE, trust=trust), schemas) + + +def test_unknown_supervisor_refused(schemas: Schemas) -> None: + with pytest.raises(CompileError) as exc: + compile_host(_model(SITE), _host(SITE, platform="linux", supervisor="openrc"), schemas) + assert "supervisor 'openrc' is not one of" in str(exc.value) + assert "there is no default" in str(exc.value) + + +@pytest.mark.parametrize( + ("platform", "supervisor"), + [("macos", "systemd"), ("macos", "nixos"), ("linux", "launchd"), ("android", "runit")], +) +def test_platform_supervisor_mismatch_refused(schemas: Schemas, platform: str, supervisor: str) -> None: + with pytest.raises(CompileError) as exc: + compile_host(_model(SITE), _host(SITE, platform=platform, supervisor=supervisor, trust=_trust(SITE, None)), schemas) + assert "Resolution: fix --platform or --supervisor" in str(exc.value) or "no supervisor is decided" in str(exc.value) + + +def _declare_on(model: SiteModel, host: str) -> None: + """Make every service of the example site apply to `host` on any platform.""" + for rec in model.services.data["services"]: + rec["platform"] = "any" + rec["hosts"] = [host] + rec.pop("role", None) + + +def test_nixos_host_without_services_is_deliberately_unmanaged(schemas: Schemas, projector, lint) -> None: + result = compile_host(_model(SITE), _nixos_host(), schemas) + doc = result.goal_file + assert doc["schema_version"] == 4 + assert doc["domains"]["supervision"] == {"coverage": "deliberately-unmanaged"} + assert doc["domains"]["device-trust"]["entries"]["supervisor"] == {"host": {"name": "nixos", "state": "present"}} + assert set(doc["domains"]) == {"device-trust", "supervision"} # no unit-writer registry travels + assert schemas.errors("goal-file.schema.json", doc) == [] + assert _cross_file_findings(lint, doc) == [] + raw = projector.project(doc) + assert raw == b'{"vars":{}}' + assert projector.validate_projection(raw) == [] + _check_golden("nixos-get.goal-file.json", result.canonical) + + +def test_nixos_host_refuses_any_selected_service(schemas: Schemas) -> None: + model = _model(SITE) + _declare_on(model, "get") + with pytest.raises(CompileError) as exc: + compile_host(model, _nixos_host(), schemas) + msg = str(exc.value) + assert "host is nixos-supervised; its units are the Nix configuration's" in msg + assert "'caddy'" in msg and "services.yml:" in msg + + +@pytest.mark.parametrize("supervisor", ["systemd", "runit"]) +def test_unrendered_supervisor_refuses_selected_services_but_not_none(schemas: Schemas, supervisor: str) -> None: + host = _host(SITE, name="get", platform="linux", supervisor=supervisor, trust=_trust(SITE, None)) + model = _model(SITE) + _declare_on(model, "get") + with pytest.raises(CompileError, match="unit renderer is not built"): + compile_host(model, host, schemas) + # With nothing selected it states the supervisor and nothing else. + doc = compile_host(_model(SITE), host, schemas).goal_file + assert doc["domains"]["device-trust"]["entries"]["supervisor"]["host"]["name"] == supervisor + assert "supervision" not in doc["domains"] + assert schemas.errors("goal-file.schema.json", doc) == [] + + +def test_nixos_refused_below_schema_v4() -> None: + for version in (3, 2, 1): + with pytest.raises(CompileError, match="cannot state"): + compile_host(_model(SITE), _nixos_host(), None, schema_version=version) + + +def test_schema_v3_drops_the_supervisor_entry_for_other_hosts() -> None: + result = compile_host(_model(SITE), _host(SITE), None, schema_version=3) + doc = result.goal_file + assert doc["schema_version"] == 3 + assert "supervisor" not in doc["domains"]["device-trust"]["entries"] + assert not any("rendered at schema_version" in d for d in result.diagnostics) # no schema given: only the no-schema note + + +def test_unsupported_schema_version_refused() -> None: + with pytest.raises(CompileError, match="not one this compiler renders"): + compile_host(_model(SITE), _host(SITE), None, schema_version=5) + + # --- conflict check ------------------------------------------------------------- def test_duplicate_entry_id_names_both_writers(schemas: Schemas) -> None: @@ -263,7 +428,7 @@ def _run(args: list[str], tendcf: Path) -> subprocess.CompletedProcess[bytes]: def test_cli_buildfile_matches_library(tendcf: Path, schemas: Schemas) -> None: proc = _run( - ["buildfile", "--host", "mac", "--host-key", HOST_KEY, "--device-trust", str(SITE / "device-trust.json"), "--site", str(SITE)], + ["buildfile", "--host", "mac", "--supervisor", "launchd", "--host-key", HOST_KEY, "--device-trust", str(SITE / "device-trust.json"), "--site", str(SITE)], tendcf, ) assert proc.returncode == 0, proc.stderr.decode() @@ -272,7 +437,7 @@ def test_cli_buildfile_matches_library(tendcf: Path, schemas: Schemas) -> None: def test_cli_project_matches_projector(tendcf: Path, schemas: Schemas, projector) -> None: proc = _run( - ["buildfile", "--host", "mac", "--host-key", HOST_KEY, "--device-trust", str(SITE / "device-trust.json"), "--site", str(SITE), "--project"], + ["buildfile", "--host", "mac", "--supervisor", "launchd", "--host-key", HOST_KEY, "--device-trust", str(SITE / "device-trust.json"), "--site", str(SITE), "--project"], tendcf, ) assert proc.returncode == 0, proc.stderr.decode() @@ -280,6 +445,36 @@ def test_cli_project_matches_projector(tendcf: Path, schemas: Schemas, projector assert proc.stdout == expected +def test_cli_requires_supervisor(tendcf: Path) -> None: + proc = _run( + ["buildfile", "--host", "mac", "--host-key", HOST_KEY, "--device-trust", str(SITE / "device-trust.json"), "--site", str(SITE)], + tendcf, + ) + assert proc.returncode == 2 and b"--supervisor" in proc.stderr and proc.stdout == b"" + + +def test_cli_nixos_host_projects_to_empty_vars(tendcf: Path, schemas: Schemas, projector, tmp_path: Path) -> None: + trust = tmp_path / "device-trust.json" + trust.write_text(json.dumps(_trust(SITE, None))) + args = ["buildfile", "--host", "get", "--platform", "linux", "--supervisor", "nixos", "--host-key", HOST_KEY, + "--device-trust", str(trust), "--site", str(SITE)] + proc = _run(args, tendcf) + assert proc.returncode == 0, proc.stderr.decode() + assert proc.stdout == compile_host(_model(SITE), _nixos_host(), schemas).canonical + projected = _run([*args, "--project"], tendcf) + assert projected.returncode == 0, projected.stderr.decode() + assert projected.stdout == projector.project(json.loads(proc.stdout)) == b'{"vars":{}}' + + +def test_cli_supervisor_disagreeing_with_document_exits_1(tendcf: Path) -> None: + proc = _run( + ["buildfile", "--host", "mac", "--supervisor", "systemd", "--platform", "linux", "--host-key", HOST_KEY, + "--device-trust", str(SITE / "device-trust.json"), "--site", str(SITE)], + tendcf, + ) + assert proc.returncode == 1 and b"carries supervisor entry" in proc.stderr and proc.stdout == b"" + + def test_cli_refusal_exits_1(tendcf: Path, tmp_path: Path) -> None: site = tmp_path / "site" shutil.copytree(SITE, site) @@ -287,7 +482,7 @@ def test_cli_refusal_exits_1(tendcf: Path, tmp_path: Path) -> None: doc["services"][1]["launchd"]["label"] = doc["services"][0]["launchd"]["label"] (site / "services.yml").write_text(yaml.safe_dump(doc)) proc = _run( - ["buildfile", "--host", "mac", "--host-key", HOST_KEY, "--device-trust", str(site / "device-trust.json"), "--site", str(site)], + ["buildfile", "--host", "mac", "--supervisor", "launchd", "--host-key", HOST_KEY, "--device-trust", str(site / "device-trust.json"), "--site", str(site)], tendcf, ) assert proc.returncode == 1 @@ -301,7 +496,7 @@ def test_cli_schema_invalid_input_exits_1(tendcf: Path, tmp_path: Path) -> None: doc["domains"]["macos-user-daemons"].pop("opt_out_reason") # opt-out with no reason (site / "services.yml").write_text(yaml.safe_dump(doc)) proc = _run( - ["buildfile", "--host", "mac", "--host-key", HOST_KEY, "--device-trust", str(site / "device-trust.json"), "--site", str(site)], + ["buildfile", "--host", "mac", "--supervisor", "launchd", "--host-key", HOST_KEY, "--device-trust", str(site / "device-trust.json"), "--site", str(site)], tendcf, ) assert proc.returncode == 1 From 0d798834404ca65850fd6f21dd7df4d50e76a6d7 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sun, 1 Nov 2026 00:11:11 -0400 Subject: [PATCH 2/2] nix2cf: apply Fable review of PR #2 (F1-F7) render_goal_file re-checks host_errors and supervisor_refusals; SUPPORTED_SCHEMA_VERSIONS derives the current version; v3-with-schemas test; nixos golden pinned to tendcf's passthrough fixture; DESIGN.md Termux wording; --platform required, android dropped from choices; per-row expected messages. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 6 ++-- docs/DESIGN.md | 4 ++- nix2cf/cli.py | 2 +- nix2cf/compile.py | 12 ++++++-- tests/test_compile.py | 69 ++++++++++++++++++++++++++++++++++++------- 5 files changed, 74 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 390db12..5c641f2 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,8 @@ uv run nix2cf buildfile \ --site examples/site # services.yml, roles.yml, launchd-writers.yml ``` -`--supervisor` (`launchd`, `systemd`, `runit` or `nixos`) is required and has -no default. It is written into the goal file's `device-trust.entries.supervisor`; +`--platform` (`macos` or `linux`) and `--supervisor` (`launchd`, `systemd`, +`runit` or `nixos`) are both required and have no default. It is written into the goal file's `device-trust.entries.supervisor`; a `--device-trust` document that already carries a different `supervisor` is refused. The output is **schema_version 4** (tendcf's `goal-file.schema.json`); CI fails when `GOAL_FILE_SCHEMA_VERSION` and tendcf's schema const differ. @@ -83,7 +83,7 @@ renders. Fixture, not site data. `runit` host gets its `supervisor` entry, but a service selected for it is a compile error, not a guess: the systemd renderer (and with it the v2 `restart`/`wanted_by` fields) is step 6 proper. `nixos` needs no renderer, - as above. Android has no supervisor decided and is refused. + as above. Android has no supervisor decided: it is not a `--platform` choice. - **Extra-entry reporting** (device-side, tendcf-agent) and `def.json`. - **Inventory, trust policy, peer actions** as Site Model inputs (Step 0 remaining): the host key, platform, supervisor and `device-trust` domain are diff --git a/docs/DESIGN.md b/docs/DESIGN.md index 7ff135b..bc08a9b 100644 --- a/docs/DESIGN.md +++ b/docs/DESIGN.md @@ -38,7 +38,9 @@ oracle for the one that exists is `tendcf/examples/host_specific.json`. (`restart`, `wanted_by`) or the systemd argv/`working_dir` alphabet is written by nix2cf yet. A `nixos` host is complete: it has no units to render (see below). -- *Android/Termux* (Step 2) — no Termux unit flavor exists in the goal file. +- *Android/Termux* (Step 2) — deferred (linux-targets ruling 2026-10-29). `android` + is not a `--platform` choice and the platform↔supervisor table refuses it; + the `runit` flavour exists in the goal file but has no renderer here. - *Extra-entry reporting* — device-side, needs tendcf-agent. - *def.json* — MPF glue under the policy-tree digest, never per-host (reconciliation §9). diff --git a/nix2cf/cli.py b/nix2cf/cli.py index 5da9a52..7a2c564 100644 --- a/nix2cf/cli.py +++ b/nix2cf/cli.py @@ -156,7 +156,7 @@ def build_parser() -> argparse.ArgumentParser: b = sub.add_parser("buildfile", help="render what one host would receive") b.add_argument("--host", required=True, help="host name as used in the Site Model (hosts:, roles)") b.add_argument("--host-key", required=True, help="device public key, ed25519:<64 hex>") - b.add_argument("--platform", default="macos", choices=["macos", "linux", "android"]) + b.add_argument("--platform", required=True, choices=["macos", "linux"], help="no default; android is not offered until Termux is decided") b.add_argument( "--supervisor", required=True, diff --git a/nix2cf/compile.py b/nix2cf/compile.py index 76dda6d..b547e7a 100644 --- a/nix2cf/compile.py +++ b/nix2cf/compile.py @@ -47,7 +47,10 @@ # Older versions this compiler can still state. The launchd shape did not # change between v1 and v4; only v3 and v4 are claimed, because v3 is the one # the spec names ("Non-nixos hosts may render at 3 by dropping the entry"). -SUPPORTED_SCHEMA_VERSIONS = (3, 4) +# The current version is derived, so a bump of GOAL_FILE_SCHEMA_VERSION cannot +# leave the renderer refusing its own version. The literal 3 is NOT derived: +# re-justify it (is v3 still a shape a device may hold?) at every bump. +SUPPORTED_SCHEMA_VERSIONS = (3, GOAL_FILE_SCHEMA_VERSION) # The one state domain the reference projector reads # (`tendcf/bin/projector.py:PROJECTING_DOMAIN`). The Site Model's own domain @@ -457,9 +460,12 @@ def render_goal_file( ) -> tuple[dict[str, Any], list[str]]: """Build the goal-file document. Returns (document, errors). - Expects `host_errors(host, schema_version)` to be empty (compile_host checks). + Re-checks `host_errors` and `supervisor_refusals` itself, so a direct call + cannot emit a file compile_host would refuse (a nixos host below v4, say). """ - errors: list[str] = supervisor_refusals(host, selected) + errors: list[str] = host_errors(host, schema_version) + if not errors: + errors = supervisor_refusals(host, selected) if errors: return {}, errors supervisor = host.supervisor diff --git a/tests/test_compile.py b/tests/test_compile.py index 60a6210..7b02a90 100644 --- a/tests/test_compile.py +++ b/tests/test_compile.py @@ -268,14 +268,24 @@ def test_unknown_supervisor_refused(schemas: Schemas) -> None: assert "there is no default" in str(exc.value) +PAIRING = "Resolution: fix --platform or --supervisor" + + @pytest.mark.parametrize( - ("platform", "supervisor"), - [("macos", "systemd"), ("macos", "nixos"), ("linux", "launchd"), ("android", "runit")], + ("platform", "supervisor", "expected"), + [ + ("macos", "systemd", PAIRING), + ("macos", "nixos", PAIRING), + ("linux", "launchd", PAIRING), + ("android", "runit", "no supervisor is decided for that platform"), + ], ) -def test_platform_supervisor_mismatch_refused(schemas: Schemas, platform: str, supervisor: str) -> None: +def test_platform_supervisor_mismatch_refused(schemas: Schemas, platform: str, supervisor: str, expected: str) -> None: + host = _host(SITE, platform=platform, supervisor=supervisor, trust=_trust(SITE, None)) with pytest.raises(CompileError) as exc: - compile_host(_model(SITE), _host(SITE, platform=platform, supervisor=supervisor, trust=_trust(SITE, None)), schemas) - assert "Resolution: fix --platform or --supervisor" in str(exc.value) or "no supervisor is decided" in str(exc.value) + compile_host(_model(SITE), host, schemas) + assert expected in str(exc.value) + assert (PAIRING in str(exc.value)) == (expected == PAIRING) # the other row's message is not produced def _declare_on(model: SiteModel, host: str) -> None: @@ -286,8 +296,11 @@ def _declare_on(model: SiteModel, host: str) -> None: rec.pop("role", None) -def test_nixos_host_without_services_is_deliberately_unmanaged(schemas: Schemas, projector, lint) -> None: +def test_nixos_host_without_services_is_deliberately_unmanaged(tendcf: Path, schemas: Schemas, projector, lint) -> None: result = compile_host(_model(SITE), _nixos_host(), schemas) + # tendcf's own passthrough fixture is the oracle (the cross-file lint is a + # no-op on a file with no entries): drift in either repo shows up here. + assert result.canonical == (tendcf / "deploy" / "linux" / "passthrough" / "goal-file.json").read_bytes() doc = result.goal_file assert doc["schema_version"] == 4 assert doc["domains"]["supervision"] == {"coverage": "deliberately-unmanaged"} @@ -339,6 +352,40 @@ def test_schema_v3_drops_the_supervisor_entry_for_other_hosts() -> None: assert not any("rendered at schema_version" in d for d in result.diagnostics) # no schema given: only the no-schema note +def test_schema_v3_with_schema_is_noted_not_validated(schemas: Schemas) -> None: + result = compile_host(_model(SITE), _host(SITE), schemas, schema_version=3) + assert result.goal_file["schema_version"] == 3 + assert any("NOT validated" in d for d in result.diagnostics) + + +def test_render_goal_file_alone_refuses_nixos_below_v4() -> None: + from nix2cf.compile import render_goal_file + + doc, errors = render_goal_file(_model(SITE), _nixos_host(), [], schema_version=3) + assert doc == {} and any("cannot state" in e for e in errors) + + +def test_render_goal_file_alone_refuses_unrendered_services() -> None: + from nix2cf.compile import render_goal_file, select_services + + model = _model(SITE) + _declare_on(model, "get") + host = _nixos_host() + selected, _, _ = select_services(model, host) + assert selected + doc, errors = render_goal_file(model, host, selected) + assert doc == {} and any("nixos-supervised" in e for e in errors) + + +def test_cli_platform_is_required_and_android_not_offered(tendcf: Path) -> None: + base = ["buildfile", "--host", "mac", "--supervisor", "launchd", "--host-key", HOST_KEY, + "--device-trust", str(SITE / "device-trust.json"), "--site", str(SITE)] + missing = _run(base, tendcf) + assert missing.returncode == 2 and b"--platform" in missing.stderr and missing.stdout == b"" + android = _run([*base, "--platform", "android"], tendcf) + assert android.returncode == 2 and b"invalid choice" in android.stderr + + def test_unsupported_schema_version_refused() -> None: with pytest.raises(CompileError, match="not one this compiler renders"): compile_host(_model(SITE), _host(SITE), None, schema_version=5) @@ -428,7 +475,7 @@ def _run(args: list[str], tendcf: Path) -> subprocess.CompletedProcess[bytes]: def test_cli_buildfile_matches_library(tendcf: Path, schemas: Schemas) -> None: proc = _run( - ["buildfile", "--host", "mac", "--supervisor", "launchd", "--host-key", HOST_KEY, "--device-trust", str(SITE / "device-trust.json"), "--site", str(SITE)], + ["buildfile", "--host", "mac", "--platform", "macos", "--supervisor", "launchd", "--host-key", HOST_KEY, "--device-trust", str(SITE / "device-trust.json"), "--site", str(SITE)], tendcf, ) assert proc.returncode == 0, proc.stderr.decode() @@ -437,7 +484,7 @@ def test_cli_buildfile_matches_library(tendcf: Path, schemas: Schemas) -> None: def test_cli_project_matches_projector(tendcf: Path, schemas: Schemas, projector) -> None: proc = _run( - ["buildfile", "--host", "mac", "--supervisor", "launchd", "--host-key", HOST_KEY, "--device-trust", str(SITE / "device-trust.json"), "--site", str(SITE), "--project"], + ["buildfile", "--host", "mac", "--platform", "macos", "--supervisor", "launchd", "--host-key", HOST_KEY, "--device-trust", str(SITE / "device-trust.json"), "--site", str(SITE), "--project"], tendcf, ) assert proc.returncode == 0, proc.stderr.decode() @@ -447,7 +494,7 @@ def test_cli_project_matches_projector(tendcf: Path, schemas: Schemas, projector def test_cli_requires_supervisor(tendcf: Path) -> None: proc = _run( - ["buildfile", "--host", "mac", "--host-key", HOST_KEY, "--device-trust", str(SITE / "device-trust.json"), "--site", str(SITE)], + ["buildfile", "--host", "mac", "--platform", "macos", "--host-key", HOST_KEY, "--device-trust", str(SITE / "device-trust.json"), "--site", str(SITE)], tendcf, ) assert proc.returncode == 2 and b"--supervisor" in proc.stderr and proc.stdout == b"" @@ -482,7 +529,7 @@ def test_cli_refusal_exits_1(tendcf: Path, tmp_path: Path) -> None: doc["services"][1]["launchd"]["label"] = doc["services"][0]["launchd"]["label"] (site / "services.yml").write_text(yaml.safe_dump(doc)) proc = _run( - ["buildfile", "--host", "mac", "--supervisor", "launchd", "--host-key", HOST_KEY, "--device-trust", str(site / "device-trust.json"), "--site", str(site)], + ["buildfile", "--host", "mac", "--platform", "macos", "--supervisor", "launchd", "--host-key", HOST_KEY, "--device-trust", str(site / "device-trust.json"), "--site", str(site)], tendcf, ) assert proc.returncode == 1 @@ -496,7 +543,7 @@ def test_cli_schema_invalid_input_exits_1(tendcf: Path, tmp_path: Path) -> None: doc["domains"]["macos-user-daemons"].pop("opt_out_reason") # opt-out with no reason (site / "services.yml").write_text(yaml.safe_dump(doc)) proc = _run( - ["buildfile", "--host", "mac", "--supervisor", "launchd", "--host-key", HOST_KEY, "--device-trust", str(site / "device-trust.json"), "--site", str(site)], + ["buildfile", "--host", "mac", "--platform", "macos", "--supervisor", "launchd", "--host-key", HOST_KEY, "--device-trust", str(site / "device-trust.json"), "--site", str(site)], tendcf, ) assert proc.returncode == 1