Skip to content

Self-host the backend

You can run the LilBub cloud yourself instead of using the hosted one. This page covers what you need, how to configure it, and how to connect the companion app and a device to it.

Read this first: a public https domain is required

Section titled “Read this first: a public https domain is required”

Self-hosting needs a publicly reachable https domain with a publicly trusted certificate. LAN-only self-hosting is not supported.

The reason is in the firmware. LilBub trusts only the public root certificates that are embedded in its firmware image when it talks to a backend over https. It does not accept a private or self-signed certificate authority for the API, and there is no setting to add one at runtime. A server on a home network with a self-signed certificate will be rejected by the device.

In practice:

  • Use a domain you control and a certificate from a public certificate authority, for example Let’s Encrypt.
  • The certificate must chain to a root in the firmware’s backend trust bundle. The bundle lists 19 roots, including ISRG Root X1 and X2 (Let’s Encrypt), GTS Root R1, R3 and R4 (Google Trust Services), USERTrust RSA and ECC (Sectigo), the SSL.com roots and the Certum roots. The exact list is in data/cert/ca_stores.json in the firmware repository.
  • The server does not have to be exposed in any particular way. A tunnel or a reverse proxy that presents a public certificate works, as long as the device can reach the name over the internet.

The MQTT broker is different. The device does not use public roots for MQTT. The backend issues each device a client certificate from your own private certificate authority and hands the device your CA chain during pairing, so the broker side of your deployment is a private PKI you run. See Broker identity.

Component Role Notes
LilBub backend HTTPS API and the phone relay Go services cmd/api and cmd/relay, built by the repository Dockerfile
Redis Presence, enrollment, rate limits, replay protection Keep it private. Needs a password if it is reachable beyond the compose network
MQTT broker Direct device route Mosquitto with mutual TLS. The repository ships a compose service and config under docker/mosquitto
Supabase Accounts and durable application data Hosted Supabase project or your own Supabase stack
Public https domain API, and a second name for the relay A hostname for the API, one for the relay, and one for MQTT if you expose it

Two ready-made layouts exist in the backend repository:

  • docker-compose.yml for a plain Docker host.
  • docker-compose.coolify.yml with COOLIFY.md for a Coolify deployment.

DEPLOY.md also describes a systemd and nginx layout. The component map and health model on Cloud services apply to any of them.

Copy .env.example to .env and fill in your values. Never commit the file. The backend reads its configuration from environment variables only (internal/config/config.go).

Variable What it is
SUPABASE_URL Your Supabase project URL
SUPABASE_KEY Supabase service-role key. Secret. Backend only
SUPABASE_ANON_KEY Supabase publishable (anon) key. Public by design
PUBLIC_API_URL Public https URL of the API, for example https://api.example.com
PUBLIC_RELAY_URL Public WebSocket URL of the relay, for example wss://relay.example.com/v1/mobile
MQTT_BROKER Broker URL the API uses internally, for example ssl://mosquitto:8883
MQTT_BROKER_PUBLIC Broker URL returned to devices after pairing, for example ssl://mqtt.example.com:8883
REDIS_ADDR Redis address. Add REDIS_PASSWORD and REDIS_DB as needed
INTERNAL_RELAY_TOKEN Random secret shared by the API and relay only
FRONTEND_URLS Comma-separated browser origins allowed by CORS
COOKIE_SECURE true for any https deployment

PUBLIC_RELAY_URL is required by the client-config endpoint described below. It must be an absolute URL, and the backend refuses to start if it is unset. The relay serves mobile clients at /v1/mobile, so route your relay hostname to the relay service and include that path in the value.

Variable What it is
SETTINGS_SIGNING_KEY_PATH P-256 private key for short-lived applet settings grants
MOBILE_SIGNING_KEY_PATH P-256 key for phone grants and relay downlinks. Defaults to the settings key

The public half of these keys is compiled into the firmware. Generate your pair once, before you build firmware:

Terminal window
go run ./cmd/settings-keygen \
-private /secure/lilbub-pki/settings-grant.key.pem \
-header ../lil_buddy/include/network/settings_grant_public_key.h

The generated header holds public material only. Keep the private key out of every repository and image. A firmware image built with someone else’s public key will reject your backend’s grants.

The backend needs a private certificate authority for MQTT: a CA chain, an intermediate certificate and key that the backend uses to issue device certificates, and separate leaf certificates for the broker, the backend and the relay. The variable names are MQTT_CA_CHAIN_PATH, MQTT_ISSUER_CERT_PATH, MQTT_ISSUER_KEY_PATH, MQTT_CLIENT_CERT_PATH, MQTT_CLIENT_KEY_PATH, MQTT_BROKER_CERT_PATH, MQTT_RELAY_CERT_PATH, MQTT_CRL_PATH and MQTT_DEVICE_CERT_VALIDITY. The compose files show where each file is mounted.

TODO for the maintainers: the backend repository has no documented script for creating this CA and its leaf certificates from scratch. The rotation tool cmd/pki-rotate renews existing leaves but does not bootstrap them. Until a bootstrap procedure is published, this step is not reproducible from the documentation alone.

API_PORT, TRUSTED_PROXIES, RELAY_ALLOWED_ORIGINS, APPLET_STORAGE_DIR, the pairing and abuse limits, and the device authentication timings have defaults. .env.example and DEPLOY.md list them. Set TRUSTED_PROXIES to your reverse proxy’s address range so rate limits see real client addresses.

Do not put any secret into a Docker build argument or a frontend build variable.

The schema is defined by the files in supabase/migrations/ in the backend repository. They are the source of truth.

  1. Create a Supabase project, or start your own stack.
  2. Link the Supabase CLI to it. The CLI is a pinned development dependency of the backend, so run it through npm: npm run supabase -- link.
  3. Apply the migrations in supabase/migrations/ in filename order, for example with npm run supabase -- db push.
  4. Copy the project URL, the service-role key and the publishable key into .env.
  5. In the Supabase authentication settings, set the site URL and redirect URLs to the address of the web app you use.

Do not replay the files in the top-level migrations/ directory. They are historical and are superseded by supabase/migrations/.

For local development, npm run supabase:start starts a disposable local stack in Docker. It is meant for testing, not for the devices you actually use.

db push changes a hosted database. Run it against a project you can afford to reset, and read the output before you confirm.

When a device pairs, the API returns the MQTT broker URL to it. The value is the MQTT_BROKER_PUBLIC setting. The device then connects to that address with the client certificate your backend issued, and trusts the CA chain it received during pairing.

This means the broker address is not compiled into the firmware. Set MQTT_BROKER_PUBLIC to the name and port that devices can reach from the internet, and make sure the broker’s certificate carries that name.

The API serves one public bootstrap document so that a client that knows only your API address can configure itself:

GET /api/v1/client-config
{
"api_url": "https://api.example.com",
"relay_url": "wss://relay.example.com/v1/mobile",
"supabase_url": "https://your-project.supabase.co",
"supabase_publishable_key": "your-publishable-key"
}

The endpoint needs no credentials and is rate limited per client address. The response may be cached for five minutes. It contains only values that are public by design. The service-role key and every other secret are never part of it.

The values come from PUBLIC_API_URL, PUBLIC_RELAY_URL, SUPABASE_URL and SUPABASE_ANON_KEY. Check it from outside your network:

Terminal window
curl -fsS https://api.example.com/api/v1/client-config

Also check /health through the same hostname, and follow the layered health model.

The Android companion asks which backend to use the first time it runs. Choose Your own server and enter your API address, for example https://api.example.com. The app fetches /api/v1/client-config from that address and uses the returned API, relay and Supabase values. You do not need to rebuild the app.

The choice is made at runtime. The same app build works with the hosted LilBub cloud when you choose that option instead.

A device talks to a backend only after a backend has been chosen. There are two ways to choose yours.

From the companion. When the app is set to your own server and your LilBub is paired to the phone, the app sends the backend address to the device with the mobile.backend.set command. The device refuses this command while it is paired to any cloud. To move a device from one cloud to another, unpair it first.

Over USB serial, for builders. Open a serial monitor at 115200 baud and run:

backend set https://api.example.com
backend status

backend hosted selects the hosted LilBub cloud instead.

The address must be an https origin with an optional path. It cannot include credentials, a query or a fragment. The serial console also accepts http:// addresses for development. Do not use one for a deployment you intend to keep. backend get prints the active address and backend clear forgets the choice.

Build firmware with a different hosted URL

Section titled “Build firmware with a different hosted URL”

You normally do not need to rebuild firmware to use your own server: choose it from the companion or over USB as described above. The compile-time setting LILBUB_HOSTED_URL is the address used only by the explicit “Hosted” choice. It defaults to https://bub.0xbubbuff.de. A custom server is never taken from this setting.

If you build your own firmware and want its Hosted choice to point at your server, set LILBUB_HOSTED_URL to an absolute http(s) URL. The build script reads it from the environment or from a .env file in the firmware repository:

Terminal window
export LILBUB_HOSTED_URL=https://api.example.com
pio run -e waveshare_hw

The build prints LILBUB_HOSTED_URL= followed by the address near the start. Even then, a fresh device talks to no backend until one is chosen. Build and flash steps are on Build the hardware.

Firmware updates are verified on the device against a public key compiled into the image (include/ota_key.h). A device accepts an update only when it is signed by the matching private key and passes its version and hash checks.

  • Generate your own pair with python scripts/sign_release.py keygen and export the public half with python scripts/sign_release.py export-pubkey. Both are in the firmware repository. See docs/RELEASES.md there.
  • Keep the private key in a protected secret store, such as a protected CI environment. Do not commit it, and do not copy it into the backend, the companion or an image.
  • The backend does not store firmware binaries. In the reference deployment it serves a short-lived download redirect to releases stored in a private GitHub repository, using a read-only GitHub App (GITHUB_APP_ID, GITHUB_APP_INSTALLATION_ID, GITHUB_APP_PRIVATE_KEY_PATH) and an operator token (RELEASE_API_TOKEN) for publishing.

Unverified: whether a self-hosted backend can run with these OTA variables unset, and which update path works without a GitHub App. Until that is documented, plan on USB flashing for your own devices.

This page is built from the backend, firmware and companion sources. It has not been reproduced as a clean deployment from these instructions.

  • Merged and live in the backend: the GET /api/v1/client-config endpoint and its required PUBLIC_RELAY_URL setting. In the firmware, the LILBUB_HOSTED_URL rename, the rule that a device talks to no backend until one is chosen, the mobile.backend.set command and the USB backend set and backend hosted commands are merged.
  • Still landing in a parallel companion change at the time of writing: the runtime “Your own server” choice in the companion app. Check the companion repository for its state.
  • The broker PKI bootstrap is not documented in the sources. See the TODO above.
  • Physical behavior of a device against a self-hosted backend has not been tested end to end.
End of article