diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..e72b074 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,185 @@ +# AGENTS.md + +## Mission & Purpose + +This repository manages the declarative NixOS and Home Manager configurations for `desktop` and `laptop` hosts. + +AI agents working in this repository must maintain architectural integrity, avoid configuration sprawl, respect pre-existing work, and adhere strictly to the invariants defined in `architecture.md`. + +--- + +## The Golden Rule: Build, Never Switch + +> [!CAUTION] +> **Agents are strictly forbidden from executing `sudo nixos-rebuild switch`, `boot`, `test`, or mutating the running host system.** +> An agent may modify the *declaration* of the system and verify compilation. It must *never* autonomously alter the running state of the host machine. + +### Security Boundary Model + +```text +Repository + │ + ├── Agent may READ + ├── Agent may MODIFY + └── Agent may VALIDATE + │ + ▼ + Nix Derivation (Dry Build) + │ + ═══════╪══════════════════════════════════════ [STRICT BARRIER] + ▼ + Running Host System + (Mutations strictly prohibited without explicit user instruction) +``` + +### Permitted Verification Commands +```bash +# Validate flake metadata, inputs, and derivations +nix flake check + +# Build desktop host dryly (evaluates configuration without switching) +nix build .#nixosConfigurations.desktop.config.system.build.toplevel --no-link + +# Build laptop host dryly +nix build .#nixosConfigurations.laptop.config.system.build.toplevel --no-link +``` + +### Prohibited Live-System Commands (Unless Explicitly Requested) +- `nixos-rebuild switch` / `boot` / `test` +- `systemctl [start|stop|restart|reload]` +- `udevadm *` +- `mount` / `umount` +- `reboot` / `shutdown` +- `nix-store --delete` or manual modifications to `/nix/var/nix/profiles/` + +--- + +## Operating Rules & Constraints + +### 1. Request Interpretation +- **Narrow Interpretation**: Interpret the user's request as narrowly as reasonably possible. +- **No Inferred Authorization**: Do not infer permission for unrelated cleanup, dependency upgrades, directory reorganization, or live-system mutations. + +### 2. Preserve Pre-Existing Uncommitted Work +- **Inspect First**: Before making any edits, check `git status` to identify pre-existing uncommitted changes. +- **Never Overwrite**: Never overwrite, discard, reset, stash (`git checkout .`, `git reset`, `git stash`), or manipulate user changes that existed prior to your task. +- **Distinguish Attribution**: Clearly separate pre-existing modifications from agent modifications in the completion report. + +### 3. Do Not Expand Scope & Minimal Change Principle +- **No Spontaneous Refactoring**: An agent must not turn a feature request into a refactoring project. +- **Report Architectural Issues Separately**: If a requested change exposes an architectural smell or problem, report it to the user rather than silently attempting to refactor surrounding code (unless strictly required for correctness). +- **Smallest Viable Change**: Make the minimal change that satisfies the request. Do not introduce single-use abstractions, single-option wrapper modules, or arbitrary file reorganizations. + +### 4. Package Placement Rules +- **Shared User Applications**: Place in `modules/user/` (with `apps.nix` as the default central application module). +- **Host-Specific User Tools**: Place in `hosts//home.nix` (e.g., Hyprland screen recording utilities on desktop). +- **System-Wide Packages**: Place in `modules/system/base.nix` *only* if required for root maintenance, core networking, or systemd daemons. + +### 5. Host Isolation & Desktop Specialisation +- Keep machine-specific homelab infrastructure (`homeserver.nix`, `traefik-deployments.nix`, `dynu.nix`) under `hosts/desktop/`. Never import them into `hosts/laptop/`. +- Protect the desktop `specialisation.server`. Any changes to graphical or audio stacks must preserve the headless server boot profile. + +### 6. Hardware Configurations +- `hardware-configuration.nix` files are generated by `nixos-generate-config`. Do not manually restructure, clean up, or refactor them. Hardware additions (e.g., kernel parameters, disk mounts) require explicit justification. + +### 7. Secrets & Key Safety (SOPS) +- **Never write or commit plaintext secrets** into `.nix`, `.yaml`, or temporary files. +- **Never decrypt secrets into tracked or persistent working-tree files**. +- **Never add, modify, or rotate SOPS/age keys** unless explicitly commanded by the user. +- **Never expose decrypted secret material** through `git diff`, logs, or terminal outputs. +- Reference secrets only via `/run/secrets/` or `config.sops.secrets..path`. + +### 8. Flake Dependencies +- Do not introduce a new flake input if standard `nixpkgs` provides the required package or module. +- Do not update unrelated flake inputs or regenerate `flake.lock` for feature changes. +- Pin `inputs.nixpkgs.follows = "nixpkgs"` on new flake inputs whenever applicable. + +--- + +## Risk Classification Framework + +| Tier | Category | Examples | Required Action & Workflow | +| :--- | :--- | :--- | :--- | +| **LOW** | User Configuration | User packages in `apps.nix`, shell aliases, Neovim lua config, Tmux settings, docs. | **Autonomous**: Agent plans, edits, and validates via `nix flake check` + affected host build. | +| **MEDIUM** | Shared System & Desktop | `modules/system/*`, greetd/display manager, firewall rules, user groups, new flake inputs. | **Autonomous**: Agent plans, edits, and validates via `nix flake check` + builds **both** `desktop` and `laptop`. | +| **HIGH** | Core & Infrastructure | Bootloader, filesystems, SOPS keys/secrets setup, `specialisation.server`, Traefik, Docker networking. | **Stop & Wait**: Agent must present the plan and **wait for user approval before editing**. | +| **PROHIBITED** | Live System Mutators | `nixos-rebuild switch`, destructive disk ops, editing `/etc` outside Nix, rotating age keys, plaintext secrets. | **Blocked**: Cannot execute unless explicitly commanded by the user in the current prompt. | + +--- + +## Change Protocol + +Every modification by an agent must follow this lifecycle: + +```text +REQUEST + │ + ▼ +1. UNDERSTAND + ├── Identify affected host(s) and trace consumer import chain + ├── Check `git status` to identify pre-existing uncommitted work + └── Read architectural boundaries in architecture.md + │ + ▼ +2. PLAN + ├── Formulate minimal viable change + ├── Verify no architectural invariants are violated + └── IF HIGH RISK: Stop and present plan for user approval before proceeding + │ + ▼ +3. EDIT + ├── Make targeted modifications only + └── Respect package and secret placement boundaries + │ + ▼ +4. VALIDATE + ├── Run `nix flake check` + └── Build all downstream affected hosts (`--no-link`) + │ + ▼ +5. REVIEW + ├── Inspect `git diff` for unintended changes or exposed secrets + └── Ensure no pre-existing user work was disturbed + │ + ▼ +6. REPORT + └── Provide completion report with pre-existing work status and build outputs +``` + +--- + +## Host-Aware Validation Matrix + +> [!NOTE] +> The matrix below is a default heuristic. When the import graph indicates additional downstream consumers, validate those configurations as well. + +| Modified Path | Affected Consumers | Required Build Validation | +| :--- | :--- | :--- | +| `modules/system/*` | `desktop`, `laptop` | `nix flake check`
`nix build .#nixosConfigurations.desktop.config.system.build.toplevel --no-link`
`nix build .#nixosConfigurations.laptop.config.system.build.toplevel --no-link` | +| `modules/user/*` or `home.nix` | `desktop`, `laptop` | `nix flake check`
`nix build .#nixosConfigurations.desktop.config.system.build.toplevel --no-link`
`nix build .#nixosConfigurations.laptop.config.system.build.toplevel --no-link` | +| `hosts/desktop/*` | `desktop` only | `nix flake check`
`nix build .#nixosConfigurations.desktop.config.system.build.toplevel --no-link` | +| `hosts/laptop/*` | `laptop` only | `nix flake check`
`nix build .#nixosConfigurations.laptop.config.system.build.toplevel --no-link` | +| `flake.nix` / `flake.lock` | `desktop`, `laptop` | `nix flake check` + build both hosts | + +--- + +## Documentation Hygiene + +Maintain clear ownership of documentation: +- **`README.md`**: High-level repository overview and quick start. +- **`architecture.md`**: Structural topology, layer boundaries, and architectural invariants. +- **`AGENTS.md`**: Agent constitution, change policies, risk matrix, and validation rules. +- **`docs/*.md`**: Step-by-step procedural guides for humans (maintenance, secrets, Antigravity, Dynu). + +*Rule: Do not duplicate architectural descriptions into procedural docs or README.* + +--- + +## Completion Report Format + +When reporting back after a task, summarize: +1. **Pre-Existing Changes Detected**: [Yes / No (with details if preserved)] +2. **Files Modified by Agent**: List of exact files changed. +3. **Architectural Assessment**: Verification that invariants were respected and boundaries maintained. +4. **Validation Executed**: Output of `nix flake check` and `nix build ... --no-link`. +5. **Pending Manual Actions**: Any user-required steps (e.g. running `sudo nixos-rebuild switch` or committing changes). diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..a8e4b8e --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,10 @@ +# CLAUDE.md + +Read and follow [`AGENTS.md`](./AGENTS.md). + +Read [`architecture.md`](./architecture.md) when making architectural, shared-module, host, service, secret, dependency, or infrastructure changes. + +- [`AGENTS.md`](./AGENTS.md) is the authoritative source for agent behavior, safety guardrails, and validation protocols. +- [`architecture.md`](./architecture.md) is the authoritative source for repository architecture, layers, and invariants. + +Do not modify the running system (`nixos-rebuild switch`, `systemctl`, etc.) unless the user explicitly requests that operation. diff --git a/architecture.md b/architecture.md new file mode 100644 index 0000000..69e0e0b --- /dev/null +++ b/architecture.md @@ -0,0 +1,159 @@ +# System Architecture + +## Core Principles + +This repository manages a declarative NixOS configuration for multiple host targets (`desktop`, `laptop`) and shared user environments via Home Manager. + +1. **Git is the Source of Truth**: Every persistent system configuration must be declaratively defined within this repository. +2. **Runtime State Separation**: Runtime state (Docker volumes, dynamic IP caches, `/run/secrets/`, mutable state) must not become configuration state. +3. **Layered Isolation**: System modules provide reusable machine infrastructure; user modules configure user environments; host directories bind hardware, secrets, and machine-specific services together. + +--- + +## Topology & Directory Structure + +```text +Config/ +├── flake.nix # Flake inputs and nixosConfigurations public entrypoint +├── flake.lock # Pinned dependencies +├── home.nix # Root Home Manager shared entrypoint +├── .sops.yaml # SOPS encryption keys and path rules +│ +├── hosts/ # Machine-specific configurations +│ ├── desktop/ +│ │ ├── configuration.nix # Desktop NixOS configuration & server specialization +│ │ ├── hardware-configuration.nix # Generated hardware configuration +│ │ ├── home.nix # Desktop-specific Home Manager profile (Hyprland dotfiles) +│ │ ├── secrets.yaml # Encrypted desktop secrets (SOPS) +│ │ ├── dynu.nix # Dynu DDNS update service +│ │ ├── homeserver.nix # Core homelab runtime services +│ │ └── traefik-deployments.nix # Traefik edge reverse proxy +│ │ +│ └── laptop/ +│ ├── configuration.nix # Laptop NixOS configuration (Niri compositor) +│ ├── hardware-configuration.nix # Generated hardware configuration +│ ├── home.nix # Laptop-specific Home Manager profile +│ └── secrets.yaml # Encrypted laptop secrets (SOPS) +│ +├── modules/ +│ ├── system/ # Reusable system-level NixOS modules (shared across hosts) +│ │ ├── base.nix # Core OS settings, networking, audio, docker, bluetooth +│ │ └── graphical.nix # Shared display manager (greetd / DMS) & graphical daemons +│ │ +│ └── user/ # Reusable Home Manager modules (shared across hosts) +│ ├── base.nix # Core shell, utilities, Git, common environment +│ ├── apps.nix # Centralized user applications & CLI tools +│ ├── graphical.nix # Graphical user tools & Neovim configuration +│ ├── terminal.nix # Terminal emulators (Alacritty), multiplexers (Tmux), prompt +│ ├── shell/ # Modular shell scripts, aliases, and functions +│ ├── scripts/ # Standalone helper scripts +│ └── config/ # Managed application dotfiles +│ +└── docs/ # Procedural and operational documentation +``` + +--- + +## Layer Responsibilities + +```mermaid +graph TD + A[flake.nix] --> B[nixosConfigurations.desktop] + A --> C[nixosConfigurations.laptop] + + subgraph Desktop Host ["hosts/desktop/"] + B --> D[configuration.nix] + D --> D1[hardware-configuration.nix] + D --> D2[dynu.nix] + D --> D3[traefik-deployments.nix] + D --> D4[homeserver.nix] + D --> D5["specialisation.server"] + end + + subgraph Laptop Host ["hosts/laptop/"] + C --> L[configuration.nix] + L --> L1[hardware-configuration.nix] + end + + subgraph Shared System ["modules/system/"] + D --> S1[base.nix] + D --> S2[graphical.nix] + L --> S1 + L --> S2 + end + + subgraph Shared User HM ["home.nix -> modules/user/"] + B --> H[home.nix] + C --> H + H --> U1[base.nix] + H --> U2[apps.nix] + H --> U3[graphical.nix] + H --> U4[terminal.nix] + end + + subgraph Host-Specific HM ["hosts//home.nix"] + B --> DH[desktop/home.nix] + C --> LH[laptop/home.nix] + end +``` + +### 1. `flake.nix` +- **Role**: Public entry point for package set pinning, external flake inputs, and host output wiring (`nixosConfigurations`). +- **Contains**: `inputs` definitions (`nixpkgs`, `home-manager`, `dms`, `sops-nix`, `antigravity`, etc.) and system module composition via `specialArgs`. +- **Must Not Contain**: Package lists, user configurations, shell scripts, or host-specific options. + +### 2. `hosts//` +- **Role**: Contains everything specific to an individual physical machine. +- **Includes**: + - `hardware-configuration.nix`: Generated hardware and kernel parameters (managed by `nixos-generate-config`). + - `configuration.nix`: Hostname, bootloader, host-specific users, GPU/display drivers, and specialisations. + - `home.nix`: User packages and dotfiles tied to the host's desktop environment. + - `secrets.yaml`: SOPS-encrypted secrets for that machine. + - Host infrastructure services (e.g., `homeserver.nix`, `dynu.nix`, `traefik-deployments.nix` on `desktop`). + +### 3. `modules/system/` +- **Role**: Reusable system configuration shared by multiple hosts. +- **Rule**: A module must only be placed here when its assumptions are valid for every host importing it. If a configuration is meaningful only for one host, keep it under `hosts//`. + +### 4. `modules/user/` +- **Role**: Reusable Home Manager modules managing user packages, shell environments, and dotfiles. +- **Subcomponents**: + - Shared user applications belong under `modules/user/`. `apps.nix` is the current central application module. + - `base.nix`: Git, shell baseline, session variables. + - `graphical.nix`: User-level graphical tools and modular Neovim configuration. + - `terminal.nix`: Multiplexers (`tmux`), terminal emulators (`alacritty`), prompt (`starship`). + - `shell/`, `scripts/`, `config/`: Shell utility files and structured dotfiles. + +--- + +## Placement Guide: Where Things Belong + +| Concern | Target Location | Placement Rule | +| :--- | :--- | :--- | +| **External Dependencies** | `flake.nix` | Flake inputs only; lock `inputs.nixpkgs.follows` where applicable. | +| **Shared System Services** | `modules/system/` | Systemd services, system packages, or OS features valid for all importing hosts. | +| **Host System Settings** | `hosts//configuration.nix` | Hostname, bootloader, user groups, machine-specific daemons. | +| **Hardware / Disks** | `hosts//hardware-configuration.nix` | Generated configuration. Do not manually restructure or refactor. | +| **Shared User Apps** | `modules/user/` (`apps.nix`) | Default location for CLI and GUI user software common across hosts. | +| **Host-Specific User Apps** | `hosts//home.nix` | Tools relevant only to that host (e.g., Hyprland screen recorders on desktop). | +| **Shared Shell Tools** | `modules/user/shell/` | Modular bash/zsh helpers, aliases, and functions. | +| **Standalone Scripts** | `modules/user/scripts/` | Executable user shell scripts. | +| **Application Dotfiles** | `modules/user/config/` | Managed static configuration files linked into `$HOME/.config/`. | +| **Encrypted Secrets** | `hosts//secrets.yaml` | Encrypted with SOPS/age; referenced via `config.sops.secrets.*.path`. | +| **Desktop Homelab / DDNS** | `hosts/desktop/` | Desktop-specific homelab infrastructure, reverse proxy, and DDNS monitor. | +| **How-To Guides** | `docs/` | Operational procedures and maintenance workflows. | + +--- + +## Architectural Invariants + +These rules define the repository boundaries and must not be violated: + +1. **Flake Public Entrypoint Invariant**: `flake.nix` is the sole public entrypoint for building the repository's NixOS host configurations. +2. **Package Ownership Invariant**: Shared user applications belong under `modules/user/` (with `apps.nix` as the central application module). Do not introduce arbitrary package lists in `modules/system/` unless they are strictly required for system-wide root maintenance or core OS services. +3. **Host Isolation Invariant**: Desktop-specific homelab infrastructure (`homeserver.nix`, `traefik-deployments.nix`, `dynu.nix`) must remain isolated under `hosts/desktop/` and must never be imported into `hosts/laptop/`. +4. **Desktop Server Specialisation Invariant**: `hosts/desktop/configuration.nix` defines `specialisation.server.configuration`. The server specialization must remain valid, bootable, and strictly headless (disabling display managers, PipeWire, Bluetooth, and desktop sleep targets). +5. **Secrets Security Invariant**: Secrets must stay encrypted in Git via SOPS. Plaintext secrets must never be committed. Configurations must refer to secrets at runtime via `/run/secrets/` or `config.sops.secrets..path`. Never decrypt secrets into tracked or persistent working-tree files. Never add, modify, or rotate SOPS/age keys unless explicitly requested. +6. **Hardware Configuration Invariant**: `hardware-configuration.nix` is generated configuration. Do not manually restructure, clean up, or refactor it. Hardware-specific changes should normally be produced by `nixos-generate-config`; deliberate manual additions require explicit justification. +7. **Consumer Validation Invariant**: Changes to shared modules (`modules/system/` or `modules/user/`) or composed host configurations must be evaluated against all downstream consumers in the import graph (`desktop` and `laptop`). +8. **State Compatibility Invariant**: `system.stateVersion` and `home.stateVersion` (`26.05`) are compatibility declarations for stateful data and must not be bumped casually as part of routine maintenance or package updates.