Files
nixos-core/AGENTS.md
T
kiskaadee a966244f08
Core Architecture & Invariant CI / validate (push) Successful in 23s
docs: reconcile ~/Workloads, ADR-0005 grants, and P4.5 completion
- Replace stale ~/Sites references with ~/Workloads across README.md, AGENTS.md, docs/architecture.md, docs/operations.md, and ROADMAP.md (ADR-0004)

- Document homelab.workloadGrants, /etc/homelab/grants.json, scripts/workload_grants.py, and nixos/instance.example.nix (ADR-0005)

- Link ADR-0004 and ADR-0005 in ROADMAP.md and record Milestone P4.5 completion in nixos-core
2026-11-21 13:22:07 -05:00

8.5 KiB
Raw Blame History

Canopy NixOS Core — Agent Guidelines

1. Project Overview & Architectural Boundaries

Canopy NixOS Core (canopy-homelab/nixos-core) defines the declarative NixOS host foundation, shared platform infrastructure services, security boundaries, and the appctl operational control plane for the Canopy ecosystem. Independent application workloads live in ~/Workloads/<app> and integrate through declarative app.yaml manifests and workload-scoped Docker Compose files.

Core vs. ~/Workloads Ownership

  • Core owns: NixOS host configuration (nixos/instance.example.nix template for /etc/nixos/homelab-instance.nix), shared platform services (edge ingress, SSO/LDAP, mail, log/update monitors), platform-level ports and security policy (including homelab.workloadGrants -> /etc/homelab/grants.json), and orchestration/deployment mechanics (appctl, gitops_dispatcher.py, workload_grants.py).
  • ~/Workloads owns: Independently deployed application workloads, workload-specific docker-compose.yml topologies, workload intent (app.yaml), and deployments.yaml.
  • Boundary Rule: Never hardcode workload-specific domains, ports, containers, environment variables, or deployment policies in Core (scripts/appctl*, GitOps, or Core docker-compose.yml), and never move Core platform services into ~/Workloads. When ownership is ambiguous, consult docs/architecture.md before changing the boundary.

Context Discovery Hierarchy

Before proposing or implementing changes, establish context in this order:

  1. Architectural Decisions (docs/decisions/): Check docs/decisions/README.md and local ADRs for binding design commitments.
  2. Runtime Contracts (docs/): Consult docs/architecture.md, docs/appctl.md, docs/gitops.md, and docs/secrets-and-state.md for current behavior.
  3. Trajectory & Active Work (ROADMAP.md, todo.txt, git log): Verify whether a capability belongs to the current baseline or a planned milestone before modifying code.

When a requested change conflicts with an architectural invariant or accepted ADR, stop and surface the conflict before implementing a workaround.


2. Build, Lint & Verification Commands

The Python environment (python3, pytest, pydantic, pyyaml, types-pyyaml) and static analysis tools (ruff, pyright) are provisioned hermetically by flake.nix and automatically symlinked to .venv via the Nix devShell hook (.envrc). Do not create separate uv or pip virtual environments that split from the Nix store environment.

Every implementation step must close with the automated verification gate before proposing a commit:

# Full verification gate: Ruff + Pyright + Pytest (124+ tests) + nix flake check + NixOS drv eval
./scripts/run-tests

# Targeted fast iteration commands (inside direnv / nix develop, or via .venv)
ruff check .
pyright
.venv/bin/pytest tests/

When modifying container or Compose definitions, also validate Compose syntax:

docker compose config -q

3. Code Style & Contract Guidelines

  • Fail-Closed Validation (scripts/core_manifest.py): All external inputs (app.yaml manifests, webhook payloads, CLI arguments, daemon payloads) must be validated through strict Pydantic v2 models (ConfigDict(extra="forbid")).
  • Explicit Error Handling: Never swallow diagnostic stderr silently (e.g., avoid blind 2>/dev/null on engine or manifest commands). Surface actionable validation errors while redacting sensitive material.
  • Platform Genericism: Keep all code, templates, tests, and documentation strictly instance-agnostic:
    • Inject routing and service identities at runtime via environment variables (HOMELAB_DOMAIN, DOMAIN_SUFFIX).
    • Use RFC 2606 placeholders (example.com, example.org, example.test) in tests and configuration examples—never personal domains or operator usernames.
    • In historical documentation or milestone records, use descriptive phrasing (e.g., "the configured smtp.<domain> endpoint") rather than fabricating placeholder domains as historical facts.

4. Testing Instructions (TDD)

  • TDD-First: Write or update failing unit, structural, or security tests in tests/ before or alongside implementation changes.
  • No Tautological Tests: Every test must import and execute real production modules or scripts (core_manifest, appctl_engine, gitops_dispatcher, scripts/appctl) and assert on observable outcomes. Never write tests that only inspect local mock strings, hardcode expected outputs into the fixture under test, or continue passing when the production invariant is broken.
  • Protect Invariants: Do not bypass failing tests by weakening or deleting an invariant assertion unless the architecture itself has intentionally changed via an approved ADR.

5. Security & State Invariants

  1. Workload Manifests (app.yaml): Describe declarative workload identity, routing metadata, and deployment intent only. Never introduce arbitrary command execution, host filesystem paths, privileged flags, or Docker control capabilities into app.yaml.
  2. GitOps Trust Boundary (scripts/gitops_dispatcher.py):
    • Webhook admission requires cryptographic authentication (HMAC-SHA256).
    • Repository identity and filesystem paths must be resolved strictly through the trusted workloads registry (~/Workloads/deployments.yaml or HOMELAB_DEPLOYMENTS_FILE, templated by config/deployments.yaml)—never directly from webhook payloads.
    • Execution must remain allowlisted, serialized (flock), pre-validated by the Compose AST security checker, and free of shell interpolation (shell=False, no eval).
  3. Docker & Privilege Isolation:
    • socket-proxy is the only service permitted to mount /var/run/docker.sock (strictly :ro, read-only API capabilities only; POST=0, DELETE=0, BUILD=0, EXEC=0).
    • Never add privileged: true, host namespace sharing, or direct Docker socket mounts to workload or control-plane containers.
  4. Declarative Configuration vs. Secrets & Runtime State:
    • Git stores declarative configuration and reproducible definitions only. Never commit plaintext secrets, keys, password hashes, certificates, or runtime databases.
    • Host secrets are managed via SOPS (sops-nix) and projected to /run/secrets/.
    • Persistent mutable runtime state belongs in dedicated host state directories (~/var/lib/homelab/<app>/) or Docker volumes excluded from version control.

6. Documentation, Task Tracking & Git Workflow

  • Synchronize Documentation (docs/): Update repository documentation alongside code changes that alter behavior, contracts, or operations. Keep all Markdown formatted in GitHub-Flavored Markdown compatible with both Obsidian and the live documentation viewer.
  • Architecture Decision Records (docs/decisions/): Record binding repository-level architectural decisions in MADR 4.0 format (NNNN-slug.md) and register them in docs/decisions/README.md.
  • Live Task Tracking (todo.txt): Maintain a comment-free root todo.txt formatted for tuxedo / todo.sh ((A)–(C) priorities, +<project>, @<context>) reflecting the current milestone. Mark verified deliverables completed (x YYYY-MM-DD ...) immediately.
  • Git & Commits:
    • Never commit directly to main/master.
    • Never run destructive Git commands (reset --hard, restore, clean -fd, push --force) without explicit user confirmation.
    • Stage atomic changes and use Conventional Commits (feat, fix, refactor, test, docs, chore), waiting for user approval before committing.
    • Preserve The Unlicense across the repository.

Organization Planning Sync Contract

  • Ecosystem Hub: Master roadmap and cross-repo plans live in canopy-homelab/.profile (ROADMAP.md, docs/plans/, todo.txt).
  • Organization Kanban Board: Canopy Platform (https://gitea.<domain>/canopy-homelab/-/projects/7, Project ID: 7).
  • Column IDs (Deep Signal): Backlog -> 30 (default), Ready -> 31, Work in Progress -> 32, Review -> 33, Completed -> 34.
  • Mirror Policy: Primary sovereign Gitea forge (canopy-homelab/nixos-core); GitHub (canopy-homelab/nixos-core) is an automated sync_on_commit push mirror.
  • Re-sync Trigger: Re-run /gitea-plan on scope shifts or before decomposing the next rolling milestone (P5/P6/P7).