Architecture
Project Overview
nuci is a declarative OpenWrt UCI configuration compiler and SSH deployer. You describe your router's network, wireless, firewall, packages, and files in Nix. nuci compiles them into UCI commands and safely pushes them to the target device over SSH.
Project Architecture
Nix config ──► uci.json ──► nuci CLI ──► SSH ──► OpenWrt target
- Nix layer:
writeUcievaluates your configuration and producesuci.json;buildFirmwarecan bake the configuration into firmware. - CLI layer:
compilegenerates UCI commands,diffperforms a read-only comparison,deploycompiles and deploys. - Target layer: applies configuration idempotently, handling backup, rollback, and service reloads.
Deployment lifecycle: backup /etc/config → arm rollback watchdog (guarded by an "apply done" sentinel) → apply UCI (per-command set -e, per-config atomic commit) → mark apply done → apply packages → on success, async reload → SSH handshake confirmation → automatic rollback on failure (watchdog fires only if the sentinel is absent).
Project Features and Advantages
Features
- Declarative management of UCI settings, packages, custom files, and SSH keys.
- SOPS secrets injected at compile time.
- Dual opkg / apk backend support with local packages.
- Day-1 firmware builds.
Advantages
- Idempotent: repeated deploys leave no duplicate or stale configuration.
- Watchdog cancellation: after the apply completes, the deployer confirms the SSH handshake; on success it explicitly cancels the rollback watchdog (
test -f /tmp/.uci-watchdog-pid && kill …). The "apply done" sentinel and the handshake cancellation are two halves of the same guard: the sentinel prevents the watchdog from firing on a completed apply, and the handshake cancellation prevents it from firing on a successful reconnect after the apply window. If the SSH session is lost before either happens, the watchdog reverts the config on timeout. - Previewable:
nuci diffshows changes read-only without touching the target.
Components Overview
Nix config ──► uci.json ──► nuci CLI ──► SSH ──► OpenWrt target
│ │
│ ├─ compile: generate UCI commands
│ ├─ diff: read-only comparison
│ └─ deploy: compile + deploy + rollback guard
│
└─ generated by writeUci / buildFirmware