nuci

nuci is a declarative OpenWrt UCI configuration compiler and SSH deployer that describes configuration in Nix and safely pushes it to the target device.

Nix config ──► uci.json ──► nuci CLI ──► SSH ──► OpenWrt target

CLI Quick Reference

nuci compile ./uci.json --no-sops                    # Compile UCI only
nuci diff    ./uci.json --target root@192.168.1.1    # Read-only diff
nuci deploy  ./uci.json --target root@192.168.1.1    # Deploy with 60s watchdog
nix run .#example -- "root@192.168.1.1" --force      # Flake one-shot deploy

Documentation

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: writeUci evaluates your configuration and produces uci.json; buildFirmware can bake the configuration into firmware.
  • CLI layer: compile generates UCI commands, diff performs a read-only comparison, deploy compiles 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 diff shows 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

Nix Module Options

Index

  1. Core
  2. UCI Configuration
  3. Packages
  4. Files and Keys
  5. Secrets and Raw Commands
  6. Library Functions

Core

OptionTypeDefaultDescription
uci.packageManagerenum [ "opkg" "apk" ]"opkg"Package manager backend. Use apk on OpenWrt 25.12+, opkg on older releases.
uci.watchdogTimeoutint60Rollback watchdog timeout in seconds.
uci.verboseboolfalsePass --verbose to deploy (stage banners + remote set -x). A CLI -v takes precedence.
uci.portnullOr intnullSSH port injected into deploy; transport-only, ignored by compile/diff. A CLI --port takes precedence.
uci.identitynullOr strnullSSH identity path injected as --identity, shell-quoted; a string (not a path) so the key is not copied into the Nix store. A CLI -i takes precedence.

UCI Configuration

OptionTypeDefaultDescription
uci.settings(pkgs.formats.json {}).type{}UCI configuration (config → section → option).

Packages

OptionTypeDefaultDescription
uci.packageslistOf str[]Packages to install; prefix - to remove (e.g. -tcpdump).
uci.packageSources.opkgFeedslistOf str[]opkg feeds (src/gz <name> <url>), written to /etc/opkg/customfeeds.conf.
uci.packageSources.apkRepositorieslistOf str[]apk repository URLs, written to /etc/apk/repositories.d/customfeeds.list.
uci.packageSources.localPackageslistOf (either str path)[]Local .ipk / .apk files, streamed to the target /tmp/.

opkgFeeds and apkRepositories are mutually exclusive and must match packageManager.

Files and Keys

OptionTypeDefaultDescription
uci.fileslistOf (submodule)[]Custom files; see submodule below.
uci.sshKeyslistOf str[]Keys deployed to /etc/dropbear/authorized_keys (0600).
- sshKeys entries may carry an OpenSSH command="..." restriction, but the value must not contain shell metacharacters (;, &, `, `` ``, $(), $(, etc.). is_valid_authorized_keyrejects such keys as a conservative hardening measure — acommand= that embeds shell syntax is treated as a potential injection vector, not a literal. If you need restricted keys, keep the command a single quoted program path plus arguments with no shell operators.

uci.files.*

OptionTypeDefaultDescription
pathstrrequiredAbsolute destination path on the target.
contentnullOr strnullText content; empty string creates a zero-byte file. Mutually exclusive with base64.
base64nullOr strnullBase64 binary content. Mutually exclusive with content.
checksumnullOr strnullExpected SHA256; skips the write if the target hash matches.
executableboolfalsetrue sets 0755, false sets 0644.

Secrets and Raw Commands

OptionTypeDefaultDescription
uci.secrets.sops.fileslistOf path[]SOPS-encrypted files, decrypted in memory at compile time to fill @name@ placeholders.
uci.rawUcilistOf str[]Raw UCI command lines; must start with "uci ".

@name@ placeholder scope: settings option values, opkgFeeds / apkRepositories, rawUci, files.content, sshKeys. Not supported in packages, localPackages, or files.base64.

Library Functions (nix/default.nix)

writeUci configuration

Returns json (a derivation producing uci.json) and command (a wrapper script running nuci compile / nuci deploy).

buildFirmware { configuration, profile, release ? null }

Bakes compiled UCI directives into /etc/uci-defaults/99-nuci-bootstrap and inherits settings, packages, packageSources, sshKeys, files, rawUci.

Configuration Examples

Network and Firewall

{
  uci.settings = {
    system.system = [ { _type = "system"; hostname = "rauter"; } ];
    network = {
      lan = { _type = "interface"; device = "br-lan"; proto = "dhcp"; };
      wan = { _type = "interface"; proto = "pppoe"; username = "@wan_user@"; password = "@wan_pass@"; };
    };
    firewall.guest = { _type = "zone"; name = "guest"; network = [ "guest" ]; input = "REJECT"; output = "ACCEPT"; };
  };
  uci.watchdogTimeout = 120;
}

Wireless

Secrets use @name@ placeholders (this project's syntax, not sops-nix's {{name}}), resolved from decrypted SOPS values in memory at compile time.

{
  uci.settings.wireless = {
    radio0 = { _type = "wifi-device"; type = "mac80211"; channel = "auto"; band = "2g"; };
    default_radio0 = {
      _type = "wifi-iface"; device = "radio0"; network = "lan";
      mode = "ap"; ssid = "home-2.4"; encryption = "sae-mixed"; key = "@wifi_password@";
    };
  };
  uci.secrets.sops.files = [ ./secrets.yml ];
}

Packages

opkgFeeds and apkRepositories are mutually exclusive and must match packageManager.

{
  uci.packageManager = "opkg"; # or "apk"
  uci.packages = [ "-tcpdump" "htop" ];
  uci.packageSources = {
    opkgFeeds = [ "src/gz custom https://dl.openwrt.org/packages" ];
    # apkRepositories = [ "https://dl.openwrt.org/packages" ];
    localPackages = [ "./packages/luci-app-custom_1.0_all.ipk" ];
  };
}

Custom Files

{
  uci.files = [
    { path = "/etc/config.txt"; content = "key=value\n"; }
    {
      path = "/usr/bin/blob";
      base64 = "aGVsbG8=";
      executable = true;
      checksum = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824";
    }
  ];
}

Raw UCI Commands

{
  uci.rawUci = [
    "uci rename network.lan=lan0"
    "uci reorder wireless.@wifi-iface[0]=1"
  ];
}

Firmware Build

exampleFirmware = uci.buildFirmware {
  configuration = ./example.nix;
  profile = "linksys_e8450-ubi";
};