feat(box): the box's NixOS system as a module, and lib.mkBox from a few names
check / check (push) Canceled after 0s

The box within reach of the couch: a Raspberry Pi 3B with the AIY Voice
HAT, from the first household's configuration, made generic.

- box/body: the button service, in Python: press and talk, turns by
  voice, a press interrupts; the look signal
- box/*.js: the ir and look capabilities, behind gates, for the brain;
  the ask client, the box's side of a conversation
- box/module.nix: the services, each confined to the devices and
  addresses it needs; the private network as an option (a tailnet by
  default); the Pico found by its USB IDs, as /dev/pdtv26-ir
- box/voicehat.nix: the Voice HAT's device tree overlay and ALSA setup
- lib.mkBox: a box's whole system from its name, the home server's,
  the Wi-Fi network's, and who looks after it
- checks: box-js, box-py (doctests and pyflakes), and box-system (an
  example box's system evaluates)

Generated-by: Claude Opus 5.5
This commit is contained in:
dckc committed 2026-12-02 16:18:17 -06:00
1 parent 2e7156dc36
commit 521998b666
35 files changed
+2165 -3

No files matched your search

+172
View File
@@ -0,0 +1,172 @@
# The box's services: the button, and the ir and look capabilities for the brain.
# Each runs with only the devices and addresses it needs. Keys and the camera's URL live in
# /etc/pdtv26, root's alone (make keys); systemd hands each service a copy (LoadCredential).
{ config, lib, pkgs, ... }:
let
cfg = config.pdtv26.box;
inherit (lib) mkOption mkEnableOption mkIf mkDefault types;
keys = "/etc/pdtv26";
node = "${pkgs.nodejs-slim}/bin/node";
boxJs = "${cfg.packages.boxJs}/lib/node_modules/pdtv26-box";
speechJs = "${cfg.packages.speechJs}/lib/node_modules/pdtv26-speech";
script = name: path: pkgs.writeShellScript name ''exec ${node} ${path} "$@"'';
# What every service gets: no new privileges, a read-only system, no homes, its own /tmp.
confined = {
Restart = "on-failure";
RestartSec = 5;
NoNewPrivileges = true;
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
IPAddressDeny = "any";
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
};
in
{
options.pdtv26.box = {
enable = mkEnableOption "the box's button, ir, and look services";
speechUrl = mkOption {
type = types.str;
example = "ws://home:8024/";
description = "The speech capability's server: the box's ears and voice.";
};
askUrl = mkOption {
type = types.str;
example = "ws://home:8025/";
description = "The brain's ask capability.";
};
statusUrl = mkOption {
type = types.nullOr types.str;
default = null;
example = "http://home:8027";
description = "A status page, told of each conversation, look, and IR press, with a heartbeat; on the private network.";
};
ports = {
ir = mkOption { type = types.port; default = 8026; description = "Where the ir capability listens."; };
look = mkOption { type = types.port; default = 8028; description = "Where the look capability listens."; };
};
network = {
interface = mkOption {
type = types.str;
default = "tailscale0";
description = "The private network's interface, where the brain reaches ir and look.";
};
ranges = mkOption {
type = types.listOf types.str;
default = [ "100.64.0.0/10" "fd7a:115c:a1e0::/48" ]; # a tailnet's, MagicDNS (100.100.100.100) included
description = "The private network's addresses: the only ones the box's services reach, or answer.";
};
};
look = {
enable = mkEnableOption "the look capability: one frame from a camera on the LAN, aimed at the TV" // { default = true; };
ranges = mkOption {
type = types.listOf types.str;
default = [ "10.0.0.0/8" "172.16.0.0/12" "192.168.0.0/16" ];
description = "Where the camera may be: look reaches these, besides the private network.";
};
};
packages = {
speechJs = mkOption { type = types.package; default = pkgs.callPackage ../speech/default.nix { }; description = "The speech clients."; };
boxJs = mkOption { type = types.package; default = pkgs.callPackage ./default.nix { }; description = "The ir and look servers, and the ask client."; };
earcons = mkOption { type = types.package; default = pkgs.callPackage ./earcons.nix { }; description = "The chimes that say whose turn it is."; };
};
};
config = mkIf cfg.enable {
services.tailscale.enable = mkDefault (cfg.network.interface == "tailscale0");
networking.firewall.interfaces.${cfg.network.interface}.allowedTCPPorts =
[ cfg.ports.ir ] ++ lib.optional cfg.look.enable cfg.ports.look;
# GPIO access is per chip, not per line: the gpio group gets gpiochip0, and only the button user is in it.
# Lines the kernel holds (the amp's SD_MODE on 16) stay busy; the button service requests 23 and 25 alone.
# The Pico, under CircuitPython, by its USB IDs: its console (interface 0) is /dev/pdtv26-ir.
users.groups.gpio = { };
services.udev.extraRules = ''
SUBSYSTEM=="gpio", KERNEL=="gpiochip0", GROUP="gpio", MODE="0660"
SUBSYSTEM=="tty", ATTRS{idVendor}=="239a", ATTRS{idProduct}=="80f4", ENV{ID_USB_INTERFACE_NUM}=="00", SYMLINK+="pdtv26-ir"
'';
# The button: a press starts a conversation; turns are by voice, a press interrupts (body/box.py).
users.users.button = { isSystemUser = true; group = "button"; extraGroups = [ "gpio" "audio" ]; };
users.groups.button = { };
# look-server connects here to say it's looking; the button service taps and flashes while it does.
# The socket's group is the whole of who may: look-server, and no one else.
users.groups.look-signal = { };
systemd.sockets.pdtv26-button = {
wantedBy = [ "sockets.target" ];
socketConfig = {
ListenStream = "/run/pdtv26-look.sock";
SocketUser = "button";
SocketGroup = "look-signal";
SocketMode = "0660";
};
};
systemd.services.pdtv26-button = {
description = "the box's button: press and talk";
wantedBy = [ "multi-user.target" ];
requires = [ "pdtv26-button.socket" ];
after = [ "pdtv26-button.socket" ];
serviceConfig = confined // {
User = "button";
StateDirectory = "pdtv26-button";
StateDirectoryMode = "0700";
LoadCredential = [ "speech-key:${keys}/speech-key" "ask-key:${keys}/ask-key" ];
# -E -s, not -I: box.py imports its siblings in body/, from the store.
ExecStart = lib.escapeShellArgs [
"${pkgs.python3.withPackages (ps: [ ps.libgpiod ])}/bin/python3" "-E" "-s" "${./body}/box.py"
"${pkgs.alsa-utils}/bin/aplay" "${pkgs.alsa-utils}/bin/arecord" "${pkgs.espeak-ng}/bin/espeak-ng"
(script "hear-client" "${speechJs}/hear-client.js") (script "speak-client" "${speechJs}/speak-client.js")
cfg.speechUrl
(script "ask-client" "${boxJs}/ask-client.js") cfg.askUrl
cfg.packages.earcons "/var/lib/pdtv26-button"
] + lib.optionalString (cfg.statusUrl != null) " ${lib.escapeShellArg cfg.statusUrl}";
# The private network alone: enough to reach speech and the brain, and nothing else.
IPAddressAllow = cfg.network.ranges;
DevicePolicy = "closed";
DeviceAllow = [ "/dev/gpiochip0 rw" "char-alsa rw" ];
};
};
# The ir capability: learn and send IR codes through the Pico (../ir-pico), for the brain.
# DeviceAllow's char-ttyACM is looked up in /proc/devices when the service starts; without cdc_acm loaded
# by then (it loads when the Pico enumerates, maybe after boot starts the service), the tty gets EPERM.
boot.kernelModules = [ "cdc_acm" ];
systemd.services.pdtv26-ir = {
description = "the box's IR remote, as a capability";
wantedBy = [ "multi-user.target" ];
serviceConfig = confined // {
DynamicUser = true;
SupplementaryGroups = [ "dialout" ];
LoadCredential = [ "ir-key:${keys}/ir-key" ];
ExecStart =
let pico = pkgs.writeShellScript "pico" ''
exec ${pkgs.python3.withPackages (ps: [ ps.pyserial ])}/bin/python3 -I ${../ir-pico/pico_tty.py} /dev/pdtv26-ir "$@"
'';
in lib.escapeShellArgs ([ node "${boxJs}/ir-server.js" "::" (toString cfg.ports.ir) pico ]
++ lib.optional (cfg.statusUrl != null) cfg.statusUrl);
IPAddressAllow = [ "localhost" ] ++ cfg.network.ranges;
DevicePolicy = "closed";
DeviceAllow = [ "char-ttyACM rw" ];
};
};
# The look capability: one frame from the camera aimed at the TV (../look.js), for the brain.
# It reaches the camera on the LAN and the button service's socket, and won't look unless the button shows it.
systemd.services.pdtv26-look = mkIf cfg.look.enable {
description = "the box's eyes, as a capability";
wantedBy = [ "multi-user.target" ];
after = [ "pdtv26-button.socket" ];
serviceConfig = confined // {
DynamicUser = true;
SupplementaryGroups = [ "look-signal" ];
# rtsp-url: the camera's stream, password and all.
LoadCredential = [ "look-key:${keys}/look-key" "rtsp-url:${keys}/rtsp-url" ];
ExecStart = lib.escapeShellArgs [ node "${boxJs}/look-server.js" "::" (toString cfg.ports.look)
"/run/pdtv26-look.sock" "${pkgs.ffmpeg-headless}/bin/ffmpeg" ];
PrivateDevices = true;
IPAddressAllow = [ "localhost" ] ++ cfg.network.ranges ++ cfg.look.ranges;
MemoryMax = "300M";
};
};
};
}