Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ renew_check_seconds = 43200
acme_server = "zerossl"

[admin]
# Every secret in this file can also be read from a separate file by adding
# "_file" to its name, for example password_hash_file = "/run/secrets/admin_password_hash".
# This works for admin.password_hash, admin.session_secret, admin.protocol_login_pin_hash,
# turn.password, network.localkey and network.mqtt_password. A relative path is
# resolved against the directory of this file. Set either the value or the file, not both.
password_hash = "pbkdf2_sha256$600000$replace_me$replace_me"
session_secret = "replace-with-at-least-24-random-characters"
session_ttl_seconds = 86400
Expand Down
7 changes: 6 additions & 1 deletion docs/installation.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Installation

Start here for a first-time setup. The project supports two installation methods:
Start here for a first-time setup. The project supports three installation methods:

- Docker Compose on your own Linux host or VM
- the Home Assistant add-on from this repository
- the NixOS module from this repository

After the stack is running, continue with [Onboarding](onboarding.md) to pair a vacuum.

Expand Down Expand Up @@ -143,6 +144,10 @@ If your model already has certificate notes on the tested-vacuums page, follow t

Use [Home Assistant](home_assistant.md) as the installation guide if you want to run the stack as a Home Assistant add-on instead of Docker Compose.

## Method 3: NixOS

Use [NixOS](nixos.md) as the installation guide if you want to run the stack as a NixOS service.

## After The Stack Starts

1. Open the admin dashboard at `https://api-roborock.example.com:555/admin` by default, or `https://api-roborock.example.com:YOUR_HTTPS_PORT/admin` if you chose a custom HTTPS port.
Expand Down
159 changes: 159 additions & 0 deletions docs/nixos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# NixOS

The repository is a Nix flake. It provides a package and a NixOS module that runs the server as a hardened systemd service. Read [Installation](installation.md) first. The requirements for DNS, the stack hostname and the choice of certificate apply here in the same way.

## What The Module Does

- It runs `roborock-local-server serve` as a systemd service with a dynamic, unprivileged user.
- It keeps all state in `/var/lib/roborock-local-server`.
- It writes the non-secret settings to `config.toml` in the Nix store.
- It passes secrets to the service as systemd credentials, so they never reach the Nix store.
- It starts the embedded Mosquitto broker and, if enabled, the coturn TURN relay, from the Nix store.
- It can serve a certificate from `security.acme` and restarts the service when the certificate is renewed.

The module supports certificates that are provided by the host. The server's own Cloudflare certificate management (`tls.mode = "cloudflare_acme"`) is not available with the module. Use `security.acme` with the Cloudflare DNS provider instead, as shown below.

## Create The Secrets

The server needs three secrets. Create them with the package, for example in a temporary shell:

```bash
nix shell github:Python-roborock/local_roborock_server
roborock-local-server hash-password # admin password hash
roborock-local-server hash-password # enter the 6-digit protocol login PIN
roborock-local-server generate-secret # admin session secret
```

Store each value in its own file and make the files readable by root only. A secret manager such as sops-nix or agenix works well for this. Any file path on the host is fine, because systemd reads the files as root when the service starts.

The protocol login PIN is the 6-digit code that Home Assistant or the Roborock app enter together with the protocol login email. See [Credential Names](installation.md#credential-names).

## Configure The Module

Add the flake to your system flake and import the module:

```nix
{
inputs.roborock-local-server.url = "github:Python-roborock/local_roborock_server";

outputs = { nixpkgs, roborock-local-server, ... }: {
nixosConfigurations.myhost = nixpkgs.lib.nixosSystem {
modules = [
roborock-local-server.nixosModules.default
./configuration.nix
];
};
};
}
```

Then configure the service:

```nix
{
services.roborock-local-server = {
enable = true;
openFirewall = true;

adminPasswordHashFile = "/run/secrets/roborock/admin-password-hash";
sessionSecretFile = "/run/secrets/roborock/session-secret";
protocolLoginPinHashFile = "/run/secrets/roborock/protocol-login-pin-hash";

useACMEHost = "example.com";

settings = {
network = {
stack_fqdn = "api-roborock.example.com";
region = "eu";
timezone = "Europe/Berlin";
};
admin.protocol_login_email = "you@example.com";
};
};
}
```

`settings` accepts the settings from `config.example.toml` in the repository, with these exceptions:

- Secrets are not allowed, because the Nix store is readable by every user. The module refuses to build if a secret is set in `settings`. Use the options that end in `File` instead.
- `storage.data_dir` is fixed to the state directory of the service.
- `tls.mode` only supports `provided`. See the certificate sections below.
- `network.localkey` and `network.mqtt_password` have no `File` option. Leave them unset, and the server generates them on the first start and keeps them in its state directory.

The default ports are `555` for HTTPS and `8881` for MQTT TLS. Change them with `settings.network.https_port` and `settings.network.mqtt_tls_port`. `openFirewall` opens the ports that the server listens on.

## Certificates From security.acme

`useACMEHost` names a certificate from `security.acme.certs`. The certificate has to cover the stack hostname. If you use the camera, it also has to cover the camera hostname. See [Camera](camera.md).

Vacuums differ in which certificate authorities they trust. NixOS defaults to Let's Encrypt with an EC key, which is not the combination that the server requests by itself. Check [Tested Vacuums](tested_vacuums.md) and set the certificate authority and key type to a combination that is known to work for your model. This example requests an RSA certificate from ZeroSSL through Cloudflare DNS:

```nix
{
security.acme = {
acceptTerms = true;
certs."example.com" = {
domain = "example.com";
extraDomainNames = [ "*.example.com" ];
email = "you@example.com";
server = "https://acme.zerossl.com/v2/DV90";
keyType = "rsa2048";
dnsProvider = "cloudflare";
environmentFile = "/run/secrets/roborock/acme-environment";
};
};
}
```

The environment file holds the Cloudflare token and the External Account Binding credentials that ZeroSSL and Actalis require. You get the credentials from the dashboard of the certificate authority.

```bash
CLOUDFLARE_DNS_API_TOKEN=...
LEGO_EAB=true
LEGO_EAB_KID=...
LEGO_EAB_HMAC=...
```

The service waits for the certificate before it starts. On the very first start, NixOS may serve a temporary self-signed certificate until the real one has been issued. The service is restarted as soon as the real certificate is available, and after every renewal.

## Certificates From Files

If you manage certificates in another way, point the module to the files instead of using `useACMEHost`:

```nix
{
services.roborock-local-server = {
certificateFile = "/var/lib/certificates/fullchain.pem";
certificateKeyFile = "/var/lib/certificates/key.pem";
};
}
```

Restart the service after the files change, for example from the renewal hook of your certificate tool.

## Reverse Proxy

If a reverse proxy terminates TLS, set `settings.network.listener_mode = "external_tls"` and follow [Reverse Proxy](reverse_proxy.md). No certificate is needed in this mode. `openFirewall` does not open the HTTPS and MQTT ports in this mode, because the server then speaks plain HTTP and MQTT, which only the proxy should reach. The TURN ports are still opened if the server runs the TURN relay.

## Camera TURN Relay

To let the server run the TURN relay for the camera preview, set `settings.turn.mode = "provided"` together with the other `turn` settings from [Camera](camera.md), and put the TURN password in a file that `turnPasswordFile` points to. With `openFirewall`, the module also opens the TURN port and the UDP relay ports `49160` to `49179`.

To use a TURN server that runs elsewhere, for example `services.coturn`, set `settings.turn.mode = "external"`. Make sure that the username, password and realm match the configuration of that server.

## Dashboard Settings

Turning protocol auth or new connections on or off in the admin dashboard is stored in the state directory. The values in `settings.admin` are the defaults. A dashboard change stays in effect across restarts until the corresponding value in `settings.admin` changes.

## Backups

Back up `/var/lib/private/roborock-local-server`, which is where systemd keeps the state of a service with a dynamic user. It holds device keys, credentials, the imported cloud data and the logs. Treat the backup like a secret.

## Troubleshooting

```bash
systemctl status roborock-local-server
journalctl -u roborock-local-server -f
```

The service is marked as running even if the optional TURN relay could not start. Check the log for warnings about the relay in that case.
99 changes: 99 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
description = "Private local server for Roborock vacuums";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

pyproject-nix = {
url = "github:pyproject-nix/pyproject.nix";
inputs.nixpkgs.follows = "nixpkgs";
};

uv2nix = {
url = "github:pyproject-nix/uv2nix";
inputs.pyproject-nix.follows = "pyproject-nix";
inputs.nixpkgs.follows = "nixpkgs";
};

pyproject-build-systems = {
url = "github:pyproject-nix/build-system-pkgs";
inputs.pyproject-nix.follows = "pyproject-nix";
inputs.uv2nix.follows = "uv2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs =
{
self,
nixpkgs,
pyproject-nix,
uv2nix,
pyproject-build-systems,
}:
let
inherit (nixpkgs) lib;

systems = [
"x86_64-linux"
"aarch64-linux"
];

forAllSystems = function: lib.genAttrs systems (system: function nixpkgs.legacyPackages.${system});

# The Python dependencies are taken from uv.lock, so the package uses
# exactly the versions that the project is tested with. The source only
# contains the files that are needed to build the package, so that changes
# to the documentation, the tests or the Nix files do not rebuild it.
workspace = uv2nix.lib.workspace.loadWorkspace {
workspaceRoot = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./pyproject.toml
./uv.lock
./README.md
./LICENSE
./src
];
};
};
in
{
packages = forAllSystems (pkgs: {
roborock-local-server = pkgs.callPackage ./nix/package.nix {
inherit workspace pyproject-nix pyproject-build-systems;
};
default = self.packages.${pkgs.stdenv.hostPlatform.system}.roborock-local-server;
});

nixosModules = {
roborock-local-server = import ./nix/module.nix self;
default = self.nixosModules.roborock-local-server;
};

checks = forAllSystems (pkgs: {
package = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
nixos = pkgs.testers.runNixOSTest (import ./nix/test.nix self);
});

devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
packages = [
pkgs.python312
pkgs.uv
pkgs.mosquitto
pkgs.coturn
pkgs.openssl
pkgs.nixfmt
];
env = {
# Let uv manage the virtual environment with the Python from Nix.
UV_PYTHON = lib.getExe pkgs.python312;
UV_PYTHON_DOWNLOADS = "never";
};
};
});

formatter = forAllSystems (pkgs: pkgs.nixfmt-tree);
};
}
Loading