Your phone is the terminal. Stream a Linux/Mac desktop to an iPhone over WebRTC through NAT, with audio and input. Open source, single binaries.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/network-computer.git
HTTPS
https://gitstable.com/git/askrobots/network-computer.git
SSH
ssh://git@gitstable.com:2222/askrobots/network-computer.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Dan Quellhorst Firefox policy: no WebRTC mDNS (a page using WebRTC made Firefox listen on UDP 5353) a1d92f3 1 month, 3 weeks ago History
apply.sh · 27.1 KB · 482 lines
1#!/bin/sh
2# Provision (or re-provision) a network-computer host on Ubuntu 24.04.
3# Idempotent: safe to run repeatedly. Run as root.
4#
5# NC_PUBLIC_IP=<ip> NC_HOST_NAME=cloudbox provision/apply.sh
6#
7# Config lives as real files under provision/files/ and is copied into /.
8# Edit those files, commit, and re-run to converge a box to the repo state.
9set -e
10cd "$(dirname "$0")"
11export DEBIAN_FRONTEND=noninteractive
12# A fresh cloud image runs its own first-boot package updates; wait for them,
13# and make every apt call wait for the dpkg lock instead of failing on it.
14if command -v cloud-init >/dev/null 2>&1; then cloud-init status --wait >/dev/null 2>&1 || true; fi
15APT="apt-get -o DPkg::Lock::Timeout=600"
16GO_VERSION=1.27.1
17NC_PUBLIC_IP=${NC_PUBLIC_IP:-$(curl -s -4 ifconfig.me)}
18# ---- the desk ---------------------------------------------------------------
19# A DigitalOcean volume named desk-* attached to this computer holds everything
20# that must outlive it: the desk's identity (login, PIN, domain), the host's
21# pairing secret, the rendezvous certificate cache, and the desk user's locker
22# (settings, documents, keys). Without one, everything stays local.
23DESK=""
24DESK_DEV=$(ls /dev/disk/by-id/scsi-0DO_Volume_desk-* 2>/dev/null | head -1)
25if [ -n "$DESK_DEV" ]; then
26 echo ">> desk ($DESK_DEV)"
27 # DigitalOcean may auto-mount a formatted volume under /mnt; keep it only at /desk
28 for m in $(findmnt -rn -S "$(readlink -f "$DESK_DEV")" -o TARGET 2>/dev/null); do
29 [ "$m" = /desk ] || umount "$m"
30 done
31 sed -i "\|^$DESK_DEV |d; \|/mnt/desk_|d" /etc/fstab
32 echo "$DESK_DEV /desk ext4 defaults,nofail,discard,noatime 0 2" >> /etc/fstab
33 mkdir -p /desk
34 mountpoint -q /desk || mount /desk
35 install -d -m 0755 /desk/nc
36 install -d -m 0700 /desk/nc/host /desk/nc/rendezvous
37 # identity: /etc/nc/env points at the desk; migrate a local one the first time
38 install -d /etc/nc
39 if [ -f /etc/nc/env ] && [ ! -L /etc/nc/env ] && [ ! -f /desk/nc/env ]; then mv /etc/nc/env /desk/nc/env; fi
40 rm -f /etc/nc/env; ln -s /desk/nc/env /etc/nc/env
41 # host pairing secret and rendezvous certificate cache
42 for d in host rendezvous; do
43 if [ -d /var/lib/nc-$d ] && [ ! -L /var/lib/nc-$d ]; then
44 cp -an /var/lib/nc-$d/. /desk/nc/$d/ 2>/dev/null || true; rm -rf /var/lib/nc-$d
45 fi
46 ln -sfn /desk/nc/$d /var/lib/nc-$d
47 done
48 DESK=1
49fi
50# edits to /etc/nc/env must follow the symlink, or sed -i would replace it with
51# a plain file and the desk's identity would silently stop being used
52envset() { # envset KEY VALUE
53 if grep -q "^$1=" /etc/nc/env 2>/dev/null; then sed -i --follow-symlinks "s|^$1=.*|$1=$2|" /etc/nc/env
54 else echo "$1=$2" >> /etc/nc/env; fi
55}
56# nothing yet on a brand-new desk (the file is created below): empty, not an error
57envget() { if [ -f /etc/nc/env ]; then sed -n "s/^$1=//p" /etc/nc/env; fi; }
58
59# Host name: explicit NC_HOST_NAME, else what this box already recorded, else a
60# default. Recording it means a later re-provision can never rename the host
61# (which would strand clients and invalidate their pairing tokens).
62[ -n "$NC_HOST_NAME" ] || NC_HOST_NAME=$(envget NC_HOST_NAME)
63NC_HOST_NAME=${NC_HOST_NAME:-nc}
64# Optional public hostname for a real (Let's Encrypt) certificate. Browsers only
65# grant the microphone to secure pages, so this is what makes browser mic work.
66[ -n "$NC_DOMAIN" ] || NC_DOMAIN=$(envget NC_DOMAIN)
67# The desktop account (not root). Recorded like the others.
68[ -n "$NC_DESK_USER" ] || NC_DESK_USER=$(envget NC_DESK_USER)
69NC_DESK_USER=${NC_DESK_USER:-user}
70# Keyboard layout for the desktop, e.g. us:dvorak. Clients also set it on connect.
71[ -n "$NC_KEYBOARD" ] || NC_KEYBOARD=$(envget NC_KEYBOARD)
72
73# What each service depends on. apply.sh restarts a running service only when
74# one of these changed, so re-provisioning takes effect without killing the
75# desktop session for nothing.
76deps() {
77 case $1 in
78 xorg) echo /etc/systemd/system/nc-xorg.service /etc/X11/xorg.conf.d/10-dummy.conf ;;
79 desktop) echo /etc/systemd/system/nc-desktop.service /etc/systemd/system/nc-desktop.service.d/*.conf ;;
80 audio) echo /etc/systemd/system/nc-audio.service /usr/local/bin/nc-audio-setup /etc/pulse/client.conf /etc/pulse/daemon.conf.d/nc.conf ;;
81 rendezvous) echo /etc/systemd/system/nc-rendezvous.service /etc/systemd/system/nc-rendezvous.service.d/*.conf /usr/local/bin/nc-rendezvous /etc/nc/env ;;
82 host) echo /etc/systemd/system/nc-host.service /etc/systemd/system/nc-host.service.d/*.conf /usr/local/bin/nc-host /etc/nc/env ;;
83 object-server) echo /etc/systemd/system/nc-object-server.service /etc/systemd/system/nc-object-server.service.d/*.conf /etc/nc/object-server.env /opt/dbbasic-object-server/.git/HEAD /opt/dbbasic-object-server/.git/refs/heads/main ;;
84 object-daemon) echo /etc/systemd/system/nc-object-daemon.service /etc/systemd/system/nc-object-daemon.service.d/*.conf /etc/nc/object-server.env /opt/dbbasic-object-server/.git/refs/heads/main ;;
85 streams) echo /etc/systemd/system/nc-streams.service /etc/nc/mediamtx.yml /usr/local/bin/mediamtx /etc/nc/streams.env ;;
86 voice) echo /etc/systemd/system/nc-voice.service /etc/systemd/system/nc-voice.service.d/*.conf /usr/local/bin/nc-voice /etc/nc/object-server.env /opt/piper/voices/en_US-lessac-medium.onnx.json ;;
87 esac
88}
89snapshot() { for s in xorg desktop audio rendezvous host object-server object-daemon voice streams; do echo "$s $(cat $(deps $s) 2>/dev/null | md5sum | cut -c1-12)"; done; }
90BEFORE=$(snapshot)
91
92echo ">> memory: swap, so a spike slows the desk down instead of freezing it"
93# Cloud images come without swap. With 4 GB and none, running out of memory
94# did not kill anything: the kernel evicted and re-read program files until
95# ssh, the desktop and the web page all stopped answering (2026-09-25; a
96# Flutter build next to five video streams). Swap as large as the memory, at
97# most 4 GB, on the computer's own disk (not the desk volume).
98if ! swapon --show=NAME --noheadings | grep -q .; then
99 MEM=$(awk '/MemTotal/{print int($2/1024)}' /proc/meminfo); SZ=$(( MEM < 4096 ? MEM : 4096 ))
100 [ -f /swapfile ] || { fallocate -l ${SZ}M /swapfile && chmod 600 /swapfile && mkswap -q /swapfile; }
101 swapon /swapfile
102fi
103grep -q '^/swapfile ' /etc/fstab || echo '/swapfile none swap sw 0 0' >> /etc/fstab
104# prefer keeping programs in memory over file cache, but use swap before freezing
105printf 'vm.swappiness=20\n' > /etc/sysctl.d/60-nc-memory.conf; sysctl -q -p /etc/sysctl.d/60-nc-memory.conf
106
107echo ">> packages"
108$APT update -q
109# before any install: package upgrades must never restart the desk's own
110# services (needrestart would, closing every window of the session)
111install -Dm644 files/etc/needrestart/conf.d/nc.conf /etc/needrestart/conf.d/nc.conf
112grep -vE '^\s*#|^\s*$' packages.txt | xargs $APT install -y -q
113
114echo ">> memory: earlyoom stops the biggest runaway before the desk locks up"
115# Never the desk's own services, the desktop or ssh; builds and browser tabs first.
116cat > /etc/default/earlyoom <<'EOF'
117# network-computer (provision/apply.sh): act at 5% memory and 10% swap left
118EARLYOOM_ARGS="-m 5 -s 10 -r 3600 --avoid ^(nc-host|nc-rendezvous|sshd|Xorg|xfce4-session|xfwm4|xfce4-panel|systemd|pulseaudio|ffmpeg)$ --prefer ^(dart|dartaotruntime|gen_snapshot|flutter|clang|ld|WebKitWebProces|Isolated.Web.Co|Web.Content)$"
119EOF
120systemctl enable earlyoom >/dev/null 2>&1; systemctl restart earlyoom
121
122echo ">> Firefox (real deb from Mozilla, not the snap stub)"
123install -d -m 0755 /etc/apt/keyrings
124curl -fsSL https://packages.mozilla.org/apt/repo-signing-key.gpg -o /etc/apt/keyrings/packages.mozilla.org.asc
125echo "deb [signed-by=/etc/apt/keyrings/packages.mozilla.org.asc] https://packages.mozilla.org/apt mozilla main" > /etc/apt/sources.list.d/mozilla.list
126printf 'Package: *\nPin: origin packages.mozilla.org\nPin-Priority: 1000\n' > /etc/apt/preferences.d/mozilla
127$APT update -q
128$APT install -y -q --allow-downgrades firefox
129
130echo ">> config files"
131cp -a files/etc/. /etc/
132cp -a files/usr/. /usr/
133# Ubuntu ships an override that re-enables per-user pulseaudio autospawn; we run
134# one system pulse (nc-audio) and clients must attach to it, not spawn their own.
135rm -f /etc/pulse/client.conf.d/01-enable-autospawn.conf
136dconf update || true
137sysctl -p /etc/sysctl.d/99-nc-noipv6.conf >/dev/null 2>&1 || true
138udevadm control --reload-rules 2>/dev/null || true
139udevadm trigger --subsystem-match=block 2>/dev/null || true # re-evaluate disks (hide the config drive)
140modprobe uinput || true
141echo uinput > /etc/modules-load.d/uinput.conf
142
143echo ">> Go toolchain"
144if ! /usr/local/go/bin/go version 2>/dev/null | grep -q "$GO_VERSION"; then
145 curl -sL "https://go.dev/dl/go${GO_VERSION}.linux-$(dpkg --print-architecture).tar.gz" | tar -C /usr/local -xz
146fi
147export PATH=$PATH:/usr/local/go/bin
148
149echo ">> build binaries"
150if [ ! -d /opt/network-computer ]; then
151 git clone -q https://github.com/askrobots/network-computer /opt/network-computer
152else
153 git -C /opt/network-computer pull -q
154fi
155( cd /opt/network-computer && go build -buildvcs=false -o /usr/local/bin/ ./cmd/... )
156
157echo ">> identity (generated once; on the desk when there is one)"
158install -d /etc/nc
159if [ ! -s /etc/nc/env ]; then
160 umask 077
161 cat > /etc/nc/env <<EOT
162NC_PASSWORD=$(head -c 12 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c 16)
163NC_PIN=$(shuf -i 100000-999999 -n 1)
164EOT
165 umask 022
166fi
167chmod 600 /etc/nc/env
168envset NC_PUBLIC_IP "$NC_PUBLIC_IP"
169envset NC_HOST_NAME "$NC_HOST_NAME"
170envset NC_DOMAIN "$NC_DOMAIN"
171envset NC_DESK_USER "$NC_DESK_USER"
172[ -z "$NC_KEYBOARD" ] || envset NC_KEYBOARD "$NC_KEYBOARD"
173[ -z "$NC_SIZE" ] || envset NC_SIZE "$NC_SIZE"
174[ -z "$NC_PRICE_HOURLY" ] || envset NC_PRICE_HOURLY "$NC_PRICE_HOURLY"
175# what the desktop dashboard may show: no secrets (the env file holds passwords)
176( . /etc/nc/env; umask 022; printf 'NC_HOST_NAME=%s\nNC_DOMAIN=%s\nNC_PUBLIC_IP=%s\nNC_SIZE=%s\nNC_PRICE_HOURLY=%s\n' \
177 "$NC_HOST_NAME" "$NC_DOMAIN" "$NC_PUBLIC_IP" "$NC_SIZE" "$NC_PRICE_HOURLY" > /etc/nc/info )
178
179echo ">> desktop user ($NC_DESK_USER)"
180if ! id "$NC_DESK_USER" >/dev/null 2>&1; then
181 # a fixed uid keeps files on the desk owned correctly across rebuilds
182 if getent passwd 1000 >/dev/null; then
183 echo "!! uid 1000 already belongs to $(getent passwd 1000 | cut -d: -f1)"; exit 1
184 fi
185 useradd -m -u 1000 -U -s /bin/bash "$NC_DESK_USER"
186fi
187UHOME=$(getent passwd "$NC_DESK_USER" | cut -d: -f6)
188# ~/Downloads and ~/Pictures on this computer's own disk (not the desk): they
189# did not exist, and the desktop sent downloads loose into the home folder.
190# The standard folder settings are pointed at them unless the person chose
191# others (a setting still at "$HOME/" means nobody did).
192for d in Downloads Pictures; do runuser -u "$NC_DESK_USER" -- mkdir -p "$UHOME/$d"; done
193UDIRS="$UHOME/.config/user-dirs.dirs"
194if [ -f "$UDIRS" ]; then
195 sed -i 's|^XDG_DOWNLOAD_DIR="\$HOME/"$|XDG_DOWNLOAD_DIR="$HOME/Downloads"|; s|^XDG_PICTURES_DIR="\$HOME/"$|XDG_PICTURES_DIR="$HOME/Pictures"|' "$UDIRS"
196fi
197if [ -n "$DESK" ]; then
198 # the locker: only these live on the desk; downloads and caches stay local
199 install -d -o "$NC_DESK_USER" -g "$NC_DESK_USER" -m 0755 /desk/home
200 install -d -o "$NC_DESK_USER" -g "$NC_DESK_USER" -m 0700 /desk/secrets
201 for p in Documents Desktop .config .local/share/keyrings; do
202 install -d -o "$NC_DESK_USER" -g "$NC_DESK_USER" "/desk/home/$p"
203 runuser -u "$NC_DESK_USER" -- mkdir -p "$(dirname "$UHOME/$p")"
204 if [ -e "$UHOME/$p" ] && [ ! -L "$UHOME/$p" ]; then
205 cp -an "$UHOME/$p/." "/desk/home/$p/" 2>/dev/null || true; rm -rf "$UHOME/$p"
206 fi
207 ln -sfn "/desk/home/$p" "$UHOME/$p"; chown -h "$NC_DESK_USER:$NC_DESK_USER" "$UHOME/$p"
208 done
209 chown -R "$NC_DESK_USER:$NC_DESK_USER" /desk/home /desk/secrets
210 # the Trash for files on the desk (gio won't create it at the top of a volume)
211 DUID=$(id -u "$NC_DESK_USER")
212 install -d -o "$NC_DESK_USER" -g "$NC_DESK_USER" -m 0700 "/desk/.Trash-$DUID"
213fi
214# AI keys: ~/.config/dbbasic/ai.env, where every dbbasic app looks for them
215# (docs/DBBASIC-APPS.md); on a desk it lives on the desk through ~/.config.
216# /desk/secrets/env, where they used to be, becomes a link to it.
217AIENV="$UHOME/.config/dbbasic/ai.env"
218OLDSEC=/etc/nc/secrets.env; [ -n "$DESK" ] && OLDSEC=/desk/secrets/env
219runuser -u "$NC_DESK_USER" -- mkdir -p "$UHOME/.config/dbbasic"
220chmod 700 "$UHOME/.config/dbbasic"
221if [ ! -f "$AIENV" ]; then
222 if [ -f "$OLDSEC" ] && [ ! -L "$OLDSEC" ]; then
223 cp -p "$OLDSEC" "$AIENV" && rm -f "$OLDSEC" && echo " AI keys moved to ~/.config/dbbasic/ai.env"
224 else
225 printf '%s\n' "# AI keys for every dbbasic app on this desk: ANTHROPIC_API_KEY=... OPENAI_API_KEY=..." \
226 "# optional: DBBASIC_ANTHROPIC_MODEL=... DBBASIC_OPENAI_MODEL=... (keep this file 0600)" > "$AIENV"
227 fi
228fi
229chown "$NC_DESK_USER:$NC_DESK_USER" "$AIENV"; chmod 600 "$AIENV"
230[ -L "$OLDSEC" ] || rm -f "$OLDSEC"
231ln -sfn "$(readlink -f "$AIENV")" "$OLDSEC"
232# where the dbbasic apps file what they save by themselves (auto-save names
233# new documents): one place for every app, this desk's own; written once,
234# the person's changes kept
235PLACES="$UHOME/.config/dbbasic/places.env"
236if [ ! -f "$PLACES" ]; then
237 printf '%s\n' "# Where the dbbasic apps put documents they save by themselves (each app in" \
238 "# a folder of its name: ~/Documents/Writer). ~/Documents is on the desk volume." \
239 "# ~/Objects instead keeps them in your object server (every device, and search)." \
240 "DOCUMENTS=~/Documents" \
241 "# one app somewhere else, used as is: WRITER_DOCUMENTS=~/Objects/Writing" > "$PLACES"
242 chown "$NC_DESK_USER:$NC_DESK_USER" "$PLACES"
243fi
244# the file manager's "Send to my device" (nc-send); added once, the user's other actions kept
245UCA="$UHOME/.config/Thunar/uca.xml"
246if ! grep -q 'nc-send' "$UCA" 2>/dev/null; then
247 runuser -u "$NC_DESK_USER" -- mkdir -p "$UHOME/.config/Thunar"
248 [ -f "$UCA" ] || { cp /etc/xdg/Thunar/uca.xml "$UCA" 2>/dev/null || printf '<?xml version="1.0" encoding="UTF-8"?>\n<actions>\n</actions>\n' > "$UCA"; }
249 awk '/<\/actions>/{print "<action><icon>document-send</icon><name>Send to my device</name><submenu></submenu><unique-id>nc-send</unique-id><command>nc-send %F</command><description>Download on the device you are connected from</description><range>*</range><patterns>*</patterns><other-files/><text-files/><image-files/><audio-files/><video-files/></action>"}{print}' "$UCA" > "$UCA.new" && mv "$UCA.new" "$UCA"
250 chown "$NC_DESK_USER:$NC_DESK_USER" "$UCA"
251fi
252# ... and "Print on my device" for documents and pictures
253if ! grep -q 'nc-print' "$UCA" 2>/dev/null; then
254 awk '/<\/actions>/{print "<action><icon>printer</icon><name>Print on my device</name><submenu></submenu><unique-id>nc-print</unique-id><command>lp -d my-device %F</command><description>Opens the print dialog of the device you are connected from</description><range>*</range><patterns>*.pdf;*.PDF;*.txt;*.png;*.jpg;*.jpeg;*.PNG;*.JPG;*.JPEG</patterns><text-files/><image-files/><other-files/></action>"}{print}' "$UCA" > "$UCA.new" && mv "$UCA.new" "$UCA"
255 chown "$NC_DESK_USER:$NC_DESK_USER" "$UCA"
256fi
257
258echo ">> camera: the connected device's camera as a webcam here"
259# v4l2loopback ships with Ubuntu's kernel modules, but the video4linux core it
260# needs (videodev) is in the "extra" modules, which cloud images leave out: those
261# for the running kernel, and the metapackage that follows kernel updates
262if ! modinfo videodev >/dev/null 2>&1 || ! modinfo v4l2loopback >/dev/null 2>&1; then
263 $APT install -y -q "linux-modules-extra-$(uname -r)" linux-image-extra-virtual >/dev/null 2>&1 ||
264 $APT install -y -q v4l2loopback-dkms >/dev/null 2>&1 || true
265fi
266if [ -e /dev/video10 ] && [ "$(cat /sys/class/video4linux/video10/name 2>/dev/null)" != "network-computer camera" ]; then
267 modprobe -r v4l2loopback 2>/dev/null || true # loaded before with other options
268fi
269modprobe v4l2loopback || echo " (no v4l2loopback: the camera stays off)"
270# the desk user's apps open it; group video for later logins, ownership for the running session
271usermod -aG video "$NC_DESK_USER"
272# keep_format: this v4l2loopback (exclusive_caps) is otherwise stuck after the
273# first writer closes, and every later camera fails to open it
274printf 'SUBSYSTEM=="video4linux", ATTR{name}=="network-computer camera", OWNER="%s", GROUP="video", MODE="0660", RUN+="/usr/bin/v4l2-ctl -d $devnode -c keep_format=1"\n' "$NC_DESK_USER" > /etc/udev/rules.d/70-nc-camera.rules
275udevadm control --reload; udevadm trigger --subsystem-match=video4linux 2>/dev/null || true
276
277echo ">> live streams: SRT into the desk (off until nc-stream on)"
278MTX_V=1.20.0
279if [ "$(/usr/local/bin/mediamtx --version 2>/dev/null)" != "v$MTX_V" ]; then
280 t=$(mktemp -d)
281 curl -fsSL -o $t/m.tgz https://github.com/bluenviron/mediamtx/releases/download/v$MTX_V/mediamtx_v${MTX_V}_linux_amd64.tar.gz
282 curl -fsSL -o $t/sums https://github.com/bluenviron/mediamtx/releases/download/v$MTX_V/checksums.sha256
283 want=$(grep "linux_amd64.tar.gz" $t/sums | awk '{print $1}')
284 [ -n "$want" ] && [ "$(sha256sum $t/m.tgz | awk '{print $1}')" = "$want" ] || { echo "!! mediamtx checksum mismatch"; exit 1; }
285 tar -xzf $t/m.tgz -C $t mediamtx && install -m 0755 $t/mediamtx /usr/local/bin/mediamtx
286 rm -rf $t
287fi
288id nc-streams >/dev/null 2>&1 || useradd -r -M -d /var/lib/nc-streams -s /usr/sbin/nologin nc-streams
289# the recordings are the desk user's streams: readable, written only by the service
290install -d -o nc-streams -g nc-streams -m 0755 /var/lib/nc-streams /var/lib/nc-streams/recordings
291# the passphrase lives with the desk (a new computer keeps it); relay keys are
292# the desk user's own, in ~/.config/nc/stream-relays (on the desk already)
293if [ -n "${DESK:-}" ]; then
294 [ -e /desk/nc/streams.env ] || { [ -f /etc/nc/streams.env ] && [ ! -L /etc/nc/streams.env ] && mv /etc/nc/streams.env /desk/nc/; } || true
295 [ -L /etc/nc/streams.env ] || { [ -e /desk/nc/streams.env ] || touch /desk/nc/streams.env; rm -f /etc/nc/streams.env; ln -s /desk/nc/streams.env /etc/nc/streams.env; }
296 chown root:"$NC_DESK_USER" /desk/nc/streams.env; chmod 0640 /desk/nc/streams.env
297fi
298# on only if someone turned it on (nc-stream on): opening a port is their choice
299if systemctl is-enabled --quiet nc-streams 2>/dev/null; then ufw allow 8890/udp >/dev/null; fi
300
301echo ">> printing: \"My device\" prints on the device you are connected from"
302# CUPS (local only), a queue whose backend (ncdevice) hands each job as a PDF
303# to nc-host, and it is the default printer: File > Print in any app reaches
304# the phone's, iPad's or Mac's own print dialog.
305chmod 0700 /usr/lib/cups/backend/ncdevice # root: needs the send socket
306systemctl enable --now cups.socket cups.service >/dev/null 2>&1 || true
307# (re)applied every run: CUPS keeps its own copy of the PPD
308lpadmin -p my-device -E -v ncdevice:/ -P /usr/share/ppd/nc/my-device.ppd \
309 -D "My device" -L "the device you are connected from" -o printer-error-policy=abort-job 2>/dev/null
310lpadmin -d my-device
311echo ">> personal settings (from the profile; applied once, never over the user's own changes)"
312PERSON="$(dirname "$0")/person.env"
313if [ -f "$PERSON" ]; then
314 (
315 . "$PERSON"
316 if [ -n "$NC_TZ" ] && echo "$NC_TZ" | grep -Eq '^[A-Za-z][A-Za-z0-9_+-]*(/[A-Za-z0-9_+-]+){0,2}$' \
317 && [ -e "/usr/share/zoneinfo/$NC_TZ" ]; then
318 timedatectl set-timezone "$NC_TZ" && echo " time zone $NC_TZ"
319 fi
320 if [ -n "$NC_EXTRA_PACKAGES" ]; then
321 pk=$(echo "$NC_EXTRA_PACKAGES" | tr ' ,' '\n\n' | grep -E '^[a-z0-9][a-z0-9.+-]+$' | tr '\n' ' ')
322 [ -z "$pk" ] || { $APT install -y -q $pk >/dev/null && echo " extra apps: $pk"; }
323 fi
324 SEC=$(readlink -f "$AIENV")
325 for k in ANTHROPIC_API_KEY OPENAI_API_KEY; do
326 eval v=\$NC_$k
327 if [ -n "$v" ] && ! grep -q "^$k=." "$SEC"; then
328 printf '%s=%s\n' "$k" "$v" >> "$SEC" && echo " $k added to ~/.config/dbbasic/ai.env"
329 fi
330 done
331 chown "$NC_DESK_USER:$NC_DESK_USER" "$SEC"; chmod 600 "$SEC"
332 # the voice style waits for the object server (nc-object-bootstrap, below)
333 [ -z "$NC_VOICE_STYLE" ] || printf '%s' "$NC_VOICE_STYLE" > /etc/nc/voice-style.initial
334 # a welcome note on their Desktop, once
335 MARK="$UHOME/.config/nc/welcomed"
336 if [ ! -e "$MARK" ]; then
337 WHO=${NC_WELCOME_NAME:-$NC_DESK_USER}
338 runuser -u "$NC_DESK_USER" -- mkdir -p "$UHOME/Desktop" "$UHOME/.config/nc"
339 cat > "$UHOME/Desktop/Welcome.txt" <<EOT
340Welcome to your desk, $WHO.
341
342This computer lives in the cloud; your desk (settings, Documents, Desktop, keys)
343is kept even when the computer is replaced.
344
345 Search and launch Alt+Space (or the magnifier button at the top)
346 Talk to it the round microphone button, bottom left:
347 "open Firefox", "put Firefox on the left",
348 "what's on my screen?", "remind me at 3 to...",
349 "take a note: ...", "be more brief"
350 Copy and paste Cmd/Ctrl+C and V work between your device and here
351 Files drop files on the window; right-click a file here and
352 choose "Send to my device" to get it back
353 Your apps and notes "Object Server" on the Desktop
354
355Connect from any browser at ${NC_DOMAIN:+https://$NC_DOMAIN}, or the Network Computer app.
356EOT
357 chown "$NC_DESK_USER:$NC_DESK_USER" "$UHOME/Desktop/Welcome.txt"
358 runuser -u "$NC_DESK_USER" -- touch "$MARK"
359 echo " welcome note for $WHO"
360 fi
361 )
362 rm -f "$PERSON"
363fi
364
365# the desktop session runs as the desk user
366install -d /etc/systemd/system/nc-desktop.service.d
367cat > /etc/systemd/system/nc-desktop.service.d/user.conf <<EOT
368[Service]
369User=$NC_DESK_USER
370Group=$NC_DESK_USER
371Environment=HOME=$UHOME
372WorkingDirectory=$UHOME
373EOT
374echo ">> object server (voice, AI; local only, as $NC_DESK_USER)"
375OS=/opt/dbbasic-object-server
376if [ ! -d $OS/.git ]; then
377 git clone -q https://github.com/askrobots/dbbasic-object-server $OS
378else
379 git -C $OS pull -q
380fi
381[ -x $OS/.venv/bin/uvicorn ] || python3 -m venv $OS/.venv
382$OS/.venv/bin/pip install -q -e "$OS[server]" # editable: the package list omits some modules
383OSDATA=/var/lib/nc-object-server; [ -n "$DESK" ] && OSDATA=/desk/object-server
384install -d -o "$NC_DESK_USER" -g "$NC_DESK_USER" -m 0700 "$OSDATA" "$OSDATA/data" "$OSDATA/objects"
385OSENV=/etc/nc/object-server.env
386if [ -n "$DESK" ]; then
387 mkdir -p /desk/nc
388 [ -s /desk/nc/object-server.env ] || { [ -f $OSENV ] && [ ! -L $OSENV ] && mv $OSENV /desk/nc/object-server.env; }
389 ln -sfn /desk/nc/object-server.env $OSENV
390fi
391if [ ! -s $OSENV ]; then
392 umask 077
393 cat > $OSENV <<EOT
394# the desk's object server; the admin token opens sessions for nc-voice
395DBBASIC_ADMIN_TOKEN=$(head -c 24 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c 32)
396EOT
397 umask 022
398fi
399oset() { grep -q "^$1=" $OSENV && sed -i --follow-symlinks "s|^$1=.*|$1=$2|" $OSENV || echo "$1=$2" >> $OSENV; }
400oset DBBASIC_DATA_DIR "$OSDATA/data"
401oset DBBASIC_OBJECTS_DIR "$OSDATA/objects"
402oset DBBASIC_PACKAGES_DIR "$OS/packages"
403oset NC_DESK_USER "$NC_DESK_USER"
404for f in ENABLE_AI_CHAT ENABLE_TTS ENABLE_STT ENABLE_READER ENABLE_SITE_ROUTES ENABLE_PACKAGE_INSTALLS ENABLE_USER_FILES \
405 ENABLE_PASSWORD_LOGIN ENABLE_PERMISSION_ENFORCEMENT ENABLE_WEBDAV; do oset DBBASIC_$f true; done
406oset DBBASIC_COOKIE_SECURE false
407chmod 600 "$(readlink -f $OSENV)" # root only: systemd reads it before switching to the user
408echo ">> Piper (local text to speech for nc-voice)"
409[ -x /opt/piper/bin/python ] || python3 -m venv /opt/piper
410/opt/piper/bin/python -c 'import piper' 2>/dev/null || /opt/piper/bin/pip install -q piper-tts
411install -d /opt/piper/voices
412[ -s /opt/piper/voices/en_US-lessac-medium.onnx ] || ( cd /opt/piper/voices && /opt/piper/bin/python -m piper.download_voices en_US-lessac-medium >/dev/null 2>&1 )
413# Vosk: "hey desk" heard on live streams, on this computer (a 40 MB English model)
414/opt/piper/bin/python -c 'import vosk' 2>/dev/null || /opt/piper/bin/pip install -q vosk
415VOSK_M=vosk-model-small-en-us-0.15
416if [ ! -d /opt/vosk/$VOSK_M ]; then
417 install -d /opt/vosk
418 curl -fsSL -o /opt/vosk/m.zip https://alphacephei.com/vosk/models/$VOSK_M.zip
419 echo "30f26242c4eb449f948e42cb302dd7a686cb29a3423a8367f99ff41780942498 /opt/vosk/m.zip" | sha256sum -c --quiet - \
420 || { echo "!! vosk model checksum mismatch"; rm -f /opt/vosk/m.zip; exit 1; }
421 python3 -c 'import zipfile; zipfile.ZipFile("/opt/vosk/m.zip").extractall("/opt/vosk")' && rm -f /opt/vosk/m.zip
422fi
423for s in nc-object-server nc-object-daemon nc-voice; do
424 install -d /etc/systemd/system/$s.service.d
425 printf '[Service]\nUser=%s\nGroup=%s\n' "$NC_DESK_USER" "$NC_DESK_USER" > /etc/systemd/system/$s.service.d/user.conf
426done
427
428# services that read the desk wait for it at boot
429for s in nc-desktop nc-host nc-rendezvous nc-object-server nc-object-daemon nc-voice nc-object-files; do
430 install -d /etc/systemd/system/$s.service.d
431 if [ -n "$DESK" ]; then
432 printf '[Unit]\nRequiresMountsFor=/desk\n' > /etc/systemd/system/$s.service.d/desk.conf
433 else
434 rm -f /etc/systemd/system/$s.service.d/desk.conf
435 fi
436done
437
438echo ">> firewall"
439ufw allow 22/tcp >/dev/null
440# with a domain the rendezvous is https on 443 and 8765 listens on loopback only
441if [ -n "$NC_DOMAIN" ]; then ufw delete allow 8765/tcp >/dev/null 2>&1 || true; else ufw allow 8765/tcp >/dev/null; fi
442ufw allow 80/tcp >/dev/null; ufw allow 443/tcp >/dev/null # Let's Encrypt + https
443ufw allow 3478/udp >/dev/null; ufw allow 49152:65535/udp >/dev/null
444ufw --force enable >/dev/null
445# nothing on a desk needs network discovery; CUPS stays for "My device" and
446# listens on localhost only (verify checks 631 is not public)
447for u in avahi-daemon.socket avahi-daemon.service cups-browsed.service; do
448 systemctl disable --now "$u" >/dev/null 2>&1 || true
449done
450
451echo ">> services"
452AFTER=$(snapshot)
453systemctl daemon-reload
454for s in xorg desktop audio rendezvous host object-server object-daemon voice streams; do
455 was=$(echo "$BEFORE" | awk -v s=$s '$1==s{print $2}')
456 now=$(echo "$AFTER" | awk -v s=$s '$1==s{print $2}')
457 if [ "$was" != "$now" ] && systemctl is-active --quiet nc-$s; then
458 echo " nc-$s changed, restarting"
459 systemctl restart nc-$s
460 # the host captures from pulse; give it a fresh start after audio restarts
461 [ $s = audio ] && systemctl is-active --quiet nc-host && systemctl restart nc-host
462 fi
463done
464systemctl enable --now nc-xorg nc-desktop nc-audio nc-rendezvous nc-host nc-object-server nc-object-daemon nc-voice
465sleep 4
466systemctl is-active nc-xorg nc-desktop nc-audio nc-rendezvous nc-host nc-object-server nc-object-daemon nc-voice | paste -sd' ' -
467nc-object-bootstrap || echo "!! object server bootstrap failed"
468# the dbbasic apps built onto this desk (infra/apps.sh): menu entries, icons, file types
469nc-apps install || echo "!! dbbasic apps not installed"
470# the object server's files as ~/Objects (needs the key the bootstrap minted)
471systemctl enable nc-object-files >/dev/null 2>&1
472systemctl restart nc-object-files && nc-object-files status | sed 's/^/ ~\/Objects: /' || echo "!! ~/Objects did not mount"
473systemctl enable --now nc-health.timer >/dev/null 2>&1 # restarts anything that stops
474# session settings (Alt+Space...) apply at every login; apply them to the running session now
475for i in 1 2 3 4 5; do nc-session-setup >/dev/null 2>&1 && break; sleep 2; done
476echo
477echo ">> verify"
478sleep 3
479sh "$(dirname "$0")/verify.sh" || echo "!! verify found problems above"
480echo
481if [ -n "$NC_DOMAIN" ]; then echo "rendezvous: https://${NC_DOMAIN}"; else echo "rendezvous: http://${NC_PUBLIC_IP}:8765"; fi
482echo "login and PIN: infra/droplet.sh creds (stored in /etc/nc/env${DESK:+ -> /desk/nc/env})"