Settings come from the Mac mini, with an R2 mirror, not iCloud
status: accepted #
Settings come from the Mac mini, with an R2 mirror, not iCloud
Today the station's settings (the lineup, logins, sports, groups, themes, the
relay's address and token) and each profile's history reach every device
through the iCloud key-value store (SettingsSync, ProfileHistorySync).
We're replacing that with the Mac mini's Manager as the one source of
settings, which also writes a copy to a Cloudflare R2 bucket on every change.
Apps ask the Mac mini first and read R2 when it can't be reached. Every device
keeps its full copy in UserDefaults and plays from it, so nothing has to be
running for you to watch: a server or bucket being down only stops changes
spreading.
Why leave iCloud: it ties the station to an Apple ID, a device signed out of
iCloud gets nothing, the store caps at 1 MB and 1,024 keys (On Demand's
history already works around it), changes take seconds to minutes, and the
Mac mini already serves the same settings to client Managers and the phone
(/v1/admin/settings, docs/remote-management.md), so there are two sync
paths where one would do.
Decisions #
-
The Mac mini is the only writer. Every change goes to it, gets a revision, and it writes the whole set to R2. With one writer R2 needs no conditional writes or merging: it's a mirror.
-
Apps prefer the Mac mini, then R2. The Mac mini answers in about three seconds on the home network; R2 covers the Mac mini being off and devices away from home.
-
Revisions survive a restart. The ledger's counter is saved with the settings, and the R2 copy carries it, so a device moving between the two keeps whichever is newer and never goes back. (Today the counter is in memory with a new epoch per start.)
-
Every device keeps a full cache and never empties it because it can't reach a source.
-
R2 holds one encrypted object with every key and its revision, and one per profile's history. It's encrypted on the Mac mini with a household key (CryptoKit), so Cloudflare only stores scrambled data. The Mac mini writes only when a hash of the plain settings changes, since encrypting the same settings twice gives different bytes. Devices check with the object's ETag; an unchanged object answers 304.
-
Keys. The Manager makes the encryption key. You make an R2 key in the Cloudflare dashboard and paste it into the Manager once: the write key stays on the Mac mini; devices get one that reads the settings bucket and writes only the outbox bucket (decision 10). One key per device, minted by the Manager and revoked when a device is forgotten, can come later without changing the devices.
-
An invite replaces signing in (amended 2026-10-18; it was "pairs with the Mac mini by code"). The Manager makes an invite, a link or QR code, that holds everything a device needs: its own key on the Mac mini, the R2 address and key, its outbox's name and secret, and the encryption key. Opening it sets the device up wherever it is, with no trip to the house: family phones are never on the home network. Devices already signed in to iCloud when this ships enroll themselves with the household token they have.
-
iCloud goes after a one-time move. The Mac mini uploads what iCloud has, the devices pair, then the key-value entitlement comes out.
-
Edits made while the Mac mini can't be reached wait for it. The device that made them queues them and sends them when the Mac mini is back, history included: a TV keeps your place in a show itself, it just doesn't follow you to another screen until then. What a TV writes is rare and deliberate (adding, removing, renumbering or restoring channels from Search; the radio station; the screensaver and its settings; a channel's buffer; the World and MBTA toggles; Keep feed; a profile's theme or a new profile) or automatic (flaky spells, numbers for new channels, one-time migrations, and watch history). Logins, keys, sports, events, On Demand's setup, radio stations, names and groups come only from the Manager or the phone.
-
R2 is the queue when the Mac mini can't be reached. A device that can't reach it writes its queued edits to its own outbox object in a second bucket (
outbox/<device>, named at pairing), and the Mac mini reads each paired device's outbox by ETag (reads, not lists, which count as writes), applies it and clears it. So an edit made on the laptop away from home still reaches the TVs, by way of the Mac mini. Devices get write access to the outbox bucket only; the settings object keeps one writer. -
Queued lists merge, not replace. The lineup, added channels and groups are whole lists, so a queued TV change arriving after a Manager change to the same list would wipe it. They merge as channel numbers and flaky spells already do (
ChannelNumbers.merge,FlakySpells.merge): an add or remove is applied to the current list, not the whole old list written back. -
Managers are profiles, and invites name the profiles (2026-10-18). Everyone watches as a named profile, and a profile is a manager or not. An invite says which profiles its device can use (a family member's phone: just theirs; your own devices: all of them), and the device offers only those. The Mac mini takes setting changes only from a device with a manager among its profiles, and from any device only the history of its own profiles. A device with no manager isn't given the relay's household token, which also drives the TVs' remote.
-
Outboxes are signed (2026-10-18). An R2 key covers a whole bucket, so any device could write any outbox. Each device signs what it puts there with its own secret, which only it and the Mac mini know, and the Mac mini drops anything unsigned or signed by another device.
-
One database on the Mac mini (2026-10-18). The settings with their revisions, the devices and the outboxes taken in live in
station.sqlite, with the errors and reports (errors.sqlite, moved in). The On Demand catalog stays intitles.sqlite: it's a cache that is rebuilt, and the station's settings shouldn't go with it. R2 holds an encrypted snapshot, not the database: R2 stores whole objects, and Cloudflare shouldn't be able to read it. -
Keys rotate when a device goes (2026-10-18). The R2 keys can't tell devices apart, so what keeps a forgotten device out is the encryption. The settings copy (full and watch-only) and each profile's history have keys of their own, and each device gets the ones it may use in its own object in R2 (
keys/<device>), sealed with its secret. Forgetting a device makes new keys and hands them to every other device that way, so those away from home keep up, and the forgotten one holds keys to nothing. The R2 keys themselves are rotated by hand (cf) if one leaks. -
Invites expire (2026-10-18): one not used in seven days is forgotten as above. A device away from home says it has joined through its outbox.
-
The Mac mini keeps its keys in the Keychain (2026-10-18): the R2 key, the encryption keys and each device's secret, not in files or UserDefaults. Devices keep theirs in their own Keychain.
-
HTTPS at home comes next (agreed 2026-10-18, not part of stage 2): everything between a device and the Mac mini is plain HTTP on the home network today, IPTV passwords and keys included. A self-signed certificate pinned by each invite or pairing will close that.
-
Each device writes only its own outbox (agreed 2026-10-19, not built; amends decisions 6 and 13). Signing (decision 13) stops a device forging another's outbox, but the shared outbox key still lets it overwrite or delete one, losing the edits waiting there. Devices stop getting that key. The Mac mini instead signs an R2 temporary credential for each device (locally, HS256 with the outbox key as parent): the outbox bucket, the one object
outbox/<device>,PutObjectonly. These live at most seven days (Cloudflare's cap), so the Mac mini puts a fresh one in the device'skeys/<device>bundle every few days, and the first in the invite. Forgetting a device rotates the outbox key and re-signs the others' credentials, as decision 15 does for the sealing keys.- The shared read key stays, and doesn't expire. A read credential would expire after seven days too, and a device that misses the refresh couldn't read the bundle that holds the next one. What it reads is sealed, so the shared key alone shows nothing.
- An expired write credential loses nothing. That happens when the Mac mini has been off for a week, or the device hasn't been opened. The edits stay queued on the device until its next bundle read brings a new credential.
- To try first: whether a locally signed credential accepts seven
days, and
PutObjecton an exact object path.
Accepted: a device that only watches still has the IPTV logins, since it plays the streams itself.
Considered options #
- The Mac mini only, no R2. Simplest, but no sync away from home, no off-site copy, and a new device can't be set up with the Mac mini off.
- R2 only, every device writing. No server needed for settings, but every device needs a write key, and concurrent writes need conditional writes and merging. The Mac mini already has the revisions and runs anyway for the relay and jobs.
- A shared drive (SMB, a NAS). tvOS can't mount network shares; it would need a bundled SMB client, polling a file, and writers could clobber each other.
- Keep iCloud. Zero-step setup and Apple does delivery, but the problems above stay.
Consequences #
- Setup takes a step: each device pairs once, where iCloud needed none.
- Settings stay encrypted in transit and at rest in R2, but between a device and the Mac mini they cross the home network over plain HTTP, like the relay. IPTV passwords and the Plex token included.
- Cost: R2's free tier (10M reads, 1M writes, 10 GB a month) covers it with room. Change checks must read the object, not list the bucket, since lists count as writes. (Corrected 2026-10-19; this first said 2.1M reads, for four devices reading every five seconds all day.) As built, a device reads R2 only while it can't reach the server: the settings every thirty seconds (about 86k reads a month) and each profile's history every two minutes (about 22k). The Mac mini reads every device's outbox every thirty seconds, about 86k a month each. Five devices with one away all the time come to about 0.55M. The outbox reads could skip devices heard from directly in the last minute.
- Delivery is our code: retries, catching up after a device has been off, and queued offline edits, where Apple used to do them.
Phin Tech docs
Rabbit Ears Manager in the store →