Skip to content

feat(nix): add a Nix flake with a package and a NixOS module - #311

Draft
diferhe wants to merge 11 commits into
Python-roborock:mainfrom
diferhe:feat/nixos
Draft

diferhe wants to merge 11 commits into
Python-roborock:mainfrom
diferhe:feat/nixos

Conversation

@diferhe

@diferhe diferhe commented Nov 29, 2026

Copy link
Copy Markdown

What does this PR do?

Adds a Nix flake so the server can be installed on NixOS as a native systemd service, as a third installation method next to Docker Compose and the Home Assistant add-on.

Depends on #309 (secrets from _file keys) and #310 (dashboard toggles in the state directory). This branch contains both. Only the last two commits are new here.

  • Package (nix/package.nix):
    • Built with uv2nix from uv.lock, so it uses exactly the locked dependencies. The pinned versions of python-roborock, cryptography and gmpy2 differ from what nixpkgs ships.
    • Wrapped with Mosquitto, coturn, OpenSSL and curl.
    • The fixed acme.sh path of the container image is replaced with acme.sh from Nix.
    • The install check finds acme.sh and loads every native extension (gmpy2, pycryptodome, cryptography, Pillow).
  • NixOS module (services.roborock-local-server, in nix/module.nix):
    • Settings: non-secret settings go to config.toml in the Nix store. The settings the module relies on, such as ports, listener mode, broker mode and TURN mode, are typed options. Everything else is passed through to the server as is. The module refuses to build if a secret is put in the settings.
    • Secrets: passed as systemd credentials and referenced through the _file keys from feat(config): read secrets from files with a _file suffix #309.
    • Certificates: from files, or from security.acme through useACMEHost. Credentials are copies taken when the service starts, so the certificate watcher from feat(tls): reload listeners when provided cert/key files change #250 does not see a renewal. The service is restarted instead when security.acme renews the certificate.
    • Service: a hardened systemd service with a dynamic user. It may bind ports below 1024 only when one of its listeners needs that, including the embedded Mosquitto broker. Otherwise the capability bounding set is empty. Network-interface (netlink) access is only granted when the TURN relay runs.
    • Firewall: openFirewall opens the HTTPS and MQTT TLS ports, plus the TURN ports when the relay runs. Behind a reverse proxy (external_tls), the plain HTTP and MQTT ports stay closed.
  • VM test (nix/test.nix, run with nix flake check), three machines:
    • a server that terminates TLS
    • a server behind a reverse proxy, with unprivileged public ports and the embedded broker on a privileged port
    • a client that checks the firewall, allocates TURN relays on both servers, checks that dashboard toggles survive a restart, and checks that no secret reaches the generated config
  • Docs: docs/nixos.md, linked from the installation guide and the navigation.

The server's own Cloudflare certificate mode is not offered by the module. The docs show the same Cloudflare DNS-01 setup through security.acme, including the External Account Binding credentials that ZeroSSL and Actalis need. The package itself still supports that mode.

Nothing changes for Docker or Home Assistant add-on users.

Before you open this PR (optional, but very helpful)

  • I checked for duplicates. There is no other Nix or NixOS packaging in open PRs or issues.
  • I reviewed 1–2 other open PRs and tested whether they work on my vacuum.

PRs I reviewed:

Proof that it works

Tested on:

  • Vacuum model: not yet tested with a vacuum on NixOS
  • Firmware version: n/a
  • Server version / commit: 7c0e0d0
  • Install method (Docker / Home Assistant add-on / other): NixOS (VM test, x86_64-linux)

What I did and what happened:

  • nix build .#checks.x86_64-linux.nixos passes, with all subtests:
    • the embedded MQTT broker runs as a child of the service
    • the service runs unprivileged, keeps its state in the state directory, and has only CAP_NET_BIND_SERVICE in its bounding set
    • no secret is written to the generated config file
    • the TLS listeners are reachable through the firewall
    • an embedded broker on a privileged port can start
    • behind a reverse proxy, the plain listeners stay closed
    • a client can allocate a relay on both TURN servers
    • dashboard toggles survive a restart
  • nix build .#checks.x86_64-linux.package passes, including the install check.
  • nix flake check --no-build --all-systems passes. aarch64-linux is evaluated only, not built.
  • uv run pytest -q passes in full.
  • uv run mkdocs build --strict passes.

Not covered by the VM test: issuing and renewing a certificate through security.acme, which needs an ACME server.

Screenshots (required for any UI change)

No UI changes.

Every secret that the config holds inline can now be read from a separate
file: admin.password_hash, admin.session_secret, admin.protocol_login_pin_hash,
turn.password, network.localkey and network.mqtt_password. This keeps
secrets out of config files that are generated by tools or stored in
world-readable places, and works with Docker secrets, systemd credentials
and similar mechanisms. Inline values keep working unchanged.
The MITM launcher reads admin.session_secret to enable protocol session
sync. It now also reads admin.session_secret_file and reports a file that
cannot be read. Inline secrets that are not strings, such as a numeric MQTT
password, keep their previous value.
The admin dashboard used to rewrite config.toml in place when an admin
turned protocol auth or new connections on or off. That write was not
atomic, failed for read-only config files, and was undone on every restart
of the Home Assistant add-on, which regenerates config.toml from its options.

Dashboard changes are now stored atomically in state/admin_settings.json.
The config file provides the default, and a stored value is discarded once
the config value it overrode changes, so the most recent change wins. An
invalid settings file stops the server with a clear error instead of
silently reopening access.
Also describe the precedence rule of stored dashboard settings precisely.
Without syncing the directory, a power failure right after a change could
bring back the previous file and silently undo a restriction.
The shipped compose.yaml mounts config.toml read-only, which made the
toggles fail with HTTP 500 before the settings moved to the state
directory.
The package is built with uv2nix from uv.lock, so it uses exactly the
locked Python dependencies. It is wrapped with Mosquitto, coturn, OpenSSL
and curl, and the fixed acme.sh path of the container image is replaced
with acme.sh from Nix. Its install check finds acme.sh and loads every
native extension.

The module runs the server as a hardened systemd service with a dynamic
user. Non-secret settings go to config.toml in the Nix store, secrets are
passed as systemd credentials through the new _file options, and
certificates come from files or from security.acme. The firewall options
keep plain listeners closed behind a reverse proxy.

The VM test covers TLS and reverse proxy setups, the firewall, TURN
allocation from a second machine, persistence of dashboard toggles across
restarts and the absence of secrets in the generated config.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant