Files
nixos/modules/pxe-infra/report-sink.nix
T
oqyude bc62b6f03d feat(pxe-infra): add HTTP report sink — L6
The last layer of the boot-and-report pipeline. Per
docs/ARCHITECTURE.md §L6, the report sink must survive the VM
disappearing — anything inside the guest is gone the moment the
VM tears down. This module provides that place: a minimal HTTP
service bound to the bridge IP that accepts run reports and
writes them to disk atomically.

- modules/options.nix: new options.pxe-infra.reportSink
  - enable: master switch (default true)
  - port: TCP port (default 8080; unprivileged so DynamicUser
    can run the daemon without CAP_NET_BIND_SERVICE)

- modules/pxe-infra/report-sink.py: new file, the actual
  service. Python stdlib only (no third-party deps, no version
  drift, no extra security surface). Endpoints:
    POST /report  — body is JSON with at least `run_id`; the
                    file is written atomically (temp + rename)
                    to /var/lib/pxe-infra/reports/<run_id>.json
    GET  /health  — returns "ok" for liveness checks
  A 1 MB body cap is plenty for any structured run report
  per RUNNER-DESIGN.md. Path-traversal hardening: run_id must
  match [A-Za-z0-9_-]+.

- modules/pxe-infra/report-sink.nix: new file, the Nix module
  that wraps report-sink.py. systemd service with:
    - DynamicUser + StateDirectory="pxe-infra/reports": no
      persistent system-user state, the daemon writes into
      /var/lib/pxe-infra/reports/ as a throwaway UID
    - NoNewPrivileges, ProtectSystem=strict, ProtectHome,
      PrivateTmp, RestrictAddressFamilies=AF_INET{,6}: only
      needs to bind a TCP socket and write one file
    - Environment sets REPORTS_BIND to the bridge IP, so the
      daemon never accidentally listens on 0.0.0.0

- modules/pxe-infra/default.nix: imports the new module.

Refs: docs/ARCHITECTURE.md §L6, docs/RUNNER-DESIGN.md
"Result object".
2026-10-13 02:48:30 +03:00

67 lines
2.2 KiB
Nix

# pxe-infra/report-sink — minimal HTTP service that accepts
# run reports from the L4 VM and stores them on disk.
#
# Per docs/ARCHITECTURE.md §L6, the report sink must survive
# the VM's disappearance. The VM pushes (over HTTP to the
# bridge IP) and this service writes the report under
# /var/lib/pxe-infra/reports/, which is the only on-host
# location a report is guaranteed to land.
#
# Design notes:
# - Python stdlib only — no extra dependency, no version
# drift, no additional security surface. The script lives
# in ./report-sink.py, read at eval time.
# - DynamicUser + StateDirectory: the daemon runs as a
# throwaway UID with write access to
# /var/lib/pxe-infra/reports. No persistent system-user
# state to maintain.
# - Hardening: ProtectSystem=strict, NoNewPrivileges,
# PrivateTmp, RestrictAddressFamilies. We only need to
# bind a TCP socket and write a file; everything else is
# denied.
{
config,
lib,
pkgs,
...
}:
let
script = pkgs.writeText "report-sink.py" (builtins.readFile ./report-sink.py);
in
{
config = lib.mkIf (config.pxe-infra.enable && config.pxe-infra.reportSink.enable) {
systemd.services.pxe-infra-report-sink = {
description = "pxe-infra: report sink (L6)";
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
serviceConfig = {
Type = "simple";
Restart = "always";
RestartSec = "5s";
DynamicUser = true;
StateDirectory = "pxe-infra/reports";
# Bind to the bridge IP only — never 0.0.0.0.
Environment = [
"REPORTS_BIND=${config.pxe-infra.bridge.address}"
"REPORTS_PORT=${toString config.pxe-infra.reportSink.port}"
"REPORTS_DIR=/var/lib/pxe-infra/reports"
"PYTHONUNBUFFERED=1"
];
# Hardening.
NoNewPrivileges = true;
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
ReadWritePaths = [ "/var/lib/pxe-infra" ];
};
script = ''
exec ${pkgs.python3}/bin/python3 ${script}
'';
};
};
}