demo / network-computer
Public
Copy of github.com/askrobots/network-computer (MIT): a desk you reach from anything. The original lives on GitHub.
GitStable demo
README: say this is a copy and where the original is
cde82c2
13 hours, 36 minutes 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. |
| 9 | set -e |
| 10 | cd "$(dirname "$0")" |
| 11 | export 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. |
| 14 | if command -v cloud-init >/dev/null 2>&1; then cloud-init status --wait >/dev/null 2>&1 || true; fi |
| 15 | APT="apt-get -o DPkg::Lock::Timeout=600" |
| 16 | GO_VERSION=1.27.1 |
| 17 | NC_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. |
| 23 | DESK="" |
| 24 | DESK_DEV=$(ls /dev/disk/by-id/scsi-0DO_Volume_desk-* 2>/dev/null | head -1) |
| 25 | if [ -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 |
| 49 | fi |
| 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 |
| 52 | envset() { # 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 |
| 57 | envget() { 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) |
| 63 | NC_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) |
| 69 | NC_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. |
| 76 | deps() { |
| 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 | } |
| 89 | snapshot() { 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; } |
| 90 | BEFORE=$(snapshot) |
| 91 | |
| 92 | echo ">> 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). |
| 98 | if ! 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 |
| 102 | fi |
| 103 | grep -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 |
| 105 | printf 'vm.swappiness=20\n' > /etc/sysctl.d/60-nc-memory.conf; sysctl -q -p /etc/sysctl.d/60-nc-memory.conf |
| 106 | |
| 107 | echo ">> 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) |
| 111 | install -Dm644 files/etc/needrestart/conf.d/nc.conf /etc/needrestart/conf.d/nc.conf |
| 112 | grep -vE '^\s*#|^\s*$' packages.txt | xargs $APT install -y -q |
| 113 | |
| 114 | echo ">> 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. |
| 116 | cat > /etc/default/earlyoom <<'EOF' |
| 117 | # network-computer (provision/apply.sh): act at 5% memory and 10% swap left |
| 118 | EARLYOOM_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)$" |
| 119 | EOF |
| 120 | systemctl enable earlyoom >/dev/null 2>&1; systemctl restart earlyoom |
| 121 | |
| 122 | echo ">> Firefox (real deb from Mozilla, not the snap stub)" |
| 123 | install -d -m 0755 /etc/apt/keyrings |
| 124 | curl -fsSL https://packages.mozilla.org/apt/repo-signing-key.gpg -o /etc/apt/keyrings/packages.mozilla.org.asc |
| 125 | echo "deb [signed-by=/etc/apt/keyrings/packages.mozilla.org.asc] https://packages.mozilla.org/apt mozilla main" > /etc/apt/sources.list.d/mozilla.list |
| 126 | printf '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 | |
| 130 | echo ">> config files" |
| 131 | cp -a files/etc/. /etc/ |
| 132 | cp -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. |
| 135 | rm -f /etc/pulse/client.conf.d/01-enable-autospawn.conf |
| 136 | dconf update || true |
| 137 | sysctl -p /etc/sysctl.d/99-nc-noipv6.conf >/dev/null 2>&1 || true |
| 138 | udevadm control --reload-rules 2>/dev/null || true |
| 139 | udevadm trigger --subsystem-match=block 2>/dev/null || true # re-evaluate disks (hide the config drive) |
| 140 | modprobe uinput || true |
| 141 | echo uinput > /etc/modules-load.d/uinput.conf |
| 142 | |
| 143 | echo ">> Go toolchain" |
| 144 | if ! /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 |
| 146 | fi |
| 147 | export PATH=$PATH:/usr/local/go/bin |
| 148 | |
| 149 | echo ">> build binaries" |
| 150 | if [ ! -d /opt/network-computer ]; then |
| 151 | git clone -q https://github.com/askrobots/network-computer /opt/network-computer |
| 152 | else |
| 153 | git -C /opt/network-computer pull -q |
| 154 | fi |
| 155 | ( cd /opt/network-computer && go build -buildvcs=false -o /usr/local/bin/ ./cmd/... ) |
| 156 | |
| 157 | echo ">> identity (generated once; on the desk when there is one)" |
| 158 | install -d /etc/nc |
| 159 | if [ ! -s /etc/nc/env ]; then |
| 160 | umask 077 |
| 161 | cat > /etc/nc/env <<EOT |
| 162 | NC_PASSWORD=$(head -c 12 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c 16) |
| 163 | NC_PIN=$(shuf -i 100000-999999 -n 1) |
| 164 | EOT |
| 165 | umask 022 |
| 166 | fi |
| 167 | chmod 600 /etc/nc/env |
| 168 | envset NC_PUBLIC_IP "$NC_PUBLIC_IP" |
| 169 | envset NC_HOST_NAME "$NC_HOST_NAME" |
| 170 | envset NC_DOMAIN "$NC_DOMAIN" |
| 171 | envset 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 | |
| 179 | echo ">> desktop user ($NC_DESK_USER)" |
| 180 | if ! 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" |
| 186 | fi |
| 187 | UHOME=$(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). |
| 192 | for d in Downloads Pictures; do runuser -u "$NC_DESK_USER" -- mkdir -p "$UHOME/$d"; done |
| 193 | UDIRS="$UHOME/.config/user-dirs.dirs" |
| 194 | if [ -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" |
| 196 | fi |
| 197 | if [ -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" |
| 213 | fi |
| 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. |
| 217 | AIENV="$UHOME/.config/dbbasic/ai.env" |
| 218 | OLDSEC=/etc/nc/secrets.env; [ -n "$DESK" ] && OLDSEC=/desk/secrets/env |
| 219 | runuser -u "$NC_DESK_USER" -- mkdir -p "$UHOME/.config/dbbasic" |
| 220 | chmod 700 "$UHOME/.config/dbbasic" |
| 221 | if [ ! -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 |
| 228 | fi |
| 229 | chown "$NC_DESK_USER:$NC_DESK_USER" "$AIENV"; chmod 600 "$AIENV" |
| 230 | [ -L "$OLDSEC" ] || rm -f "$OLDSEC" |
| 231 | ln -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 |
| 235 | PLACES="$UHOME/.config/dbbasic/places.env" |
| 236 | if [ ! -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" |
| 243 | fi |
| 244 | # the file manager's "Send to my device" (nc-send); added once, the user's other actions kept |
| 245 | UCA="$UHOME/.config/Thunar/uca.xml" |
| 246 | if ! 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" |
| 251 | fi |
| 252 | # ... and "Print on my device" for documents and pictures |
| 253 | if ! 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" |
| 256 | fi |
| 257 | |
| 258 | echo ">> 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 |
| 262 | if ! 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 |
| 265 | fi |
| 266 | if [ -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 |
| 268 | fi |
| 269 | modprobe 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 |
| 271 | usermod -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 |
| 274 | printf '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 |
| 275 | udevadm control --reload; udevadm trigger --subsystem-match=video4linux 2>/dev/null || true |
| 276 | |
| 277 | echo ">> live streams: SRT into the desk (off until nc-stream on)" |
| 278 | MTX_V=1.20.0 |
| 279 | if [ "$(/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 |
| 287 | fi |
| 288 | id 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 |
| 290 | install -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) |
| 293 | if [ -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 |
| 297 | fi |
| 298 | # on only if someone turned it on (nc-stream on): opening a port is their choice |
| 299 | if systemctl is-enabled --quiet nc-streams 2>/dev/null; then ufw allow 8890/udp >/dev/null; fi |
| 300 | |
| 301 | echo ">> 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. |
| 305 | chmod 0700 /usr/lib/cups/backend/ncdevice # root: needs the send socket |
| 306 | systemctl enable --now cups.socket cups.service >/dev/null 2>&1 || true |
| 307 | # (re)applied every run: CUPS keeps its own copy of the PPD |
| 308 | lpadmin -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 |
| 310 | lpadmin -d my-device |
| 311 | echo ">> personal settings (from the profile; applied once, never over the user's own changes)" |
| 312 | PERSON="$(dirname "$0")/person.env" |
| 313 | if [ -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 |
| 340 | Welcome to your desk, $WHO. |
| 341 | |
| 342 | This computer lives in the cloud; your desk (settings, Documents, Desktop, keys) |
| 343 | is 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 | |
| 355 | Connect from any browser at ${NC_DOMAIN:+https://$NC_DOMAIN}, or the Network Computer app. |
| 356 | EOT |
| 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" |
| 363 | fi |
| 364 | |
| 365 | # the desktop session runs as the desk user |
| 366 | install -d /etc/systemd/system/nc-desktop.service.d |
| 367 | cat > /etc/systemd/system/nc-desktop.service.d/user.conf <<EOT |
| 368 | [Service] |
| 369 | User=$NC_DESK_USER |
| 370 | Group=$NC_DESK_USER |
| 371 | Environment=HOME=$UHOME |
| 372 | WorkingDirectory=$UHOME |
| 373 | EOT |
| 374 | echo ">> object server (voice, AI; local only, as $NC_DESK_USER)" |
| 375 | OS=/opt/dbbasic-object-server |
| 376 | if [ ! -d $OS/.git ]; then |
| 377 | git clone -q https://github.com/askrobots/dbbasic-object-server $OS |
| 378 | else |
| 379 | git -C $OS pull -q |
| 380 | fi |
| 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 |
| 383 | OSDATA=/var/lib/nc-object-server; [ -n "$DESK" ] && OSDATA=/desk/object-server |
| 384 | install -d -o "$NC_DESK_USER" -g "$NC_DESK_USER" -m 0700 "$OSDATA" "$OSDATA/data" "$OSDATA/objects" |
| 385 | OSENV=/etc/nc/object-server.env |
| 386 | if [ -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 |
| 390 | fi |
| 391 | if [ ! -s $OSENV ]; then |
| 392 | umask 077 |
| 393 | cat > $OSENV <<EOT |
| 394 | # the desk's object server; the admin token opens sessions for nc-voice |
| 395 | DBBASIC_ADMIN_TOKEN=$(head -c 24 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c 32) |
| 396 | EOT |
| 397 | umask 022 |
| 398 | fi |
| 399 | oset() { grep -q "^$1=" $OSENV && sed -i --follow-symlinks "s|^$1=.*|$1=$2|" $OSENV || echo "$1=$2" >> $OSENV; } |
| 400 | oset DBBASIC_DATA_DIR "$OSDATA/data" |
| 401 | oset DBBASIC_OBJECTS_DIR "$OSDATA/objects" |
| 402 | oset DBBASIC_PACKAGES_DIR "$OS/packages" |
| 403 | oset NC_DESK_USER "$NC_DESK_USER" |
| 404 | for 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 |
| 406 | oset DBBASIC_COOKIE_SECURE false |
| 407 | chmod 600 "$(readlink -f $OSENV)" # root only: systemd reads it before switching to the user |
| 408 | echo ">> 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 |
| 411 | install -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 |
| 415 | VOSK_M=vosk-model-small-en-us-0.15 |
| 416 | if [ ! -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 |
| 422 | fi |
| 423 | for 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 |
| 426 | done |
| 427 | |
| 428 | # services that read the desk wait for it at boot |
| 429 | for 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 |
| 436 | done |
| 437 | |
| 438 | echo ">> firewall" |
| 439 | ufw allow 22/tcp >/dev/null |
| 440 | # with a domain the rendezvous is https on 443 and 8765 listens on loopback only |
| 441 | if [ -n "$NC_DOMAIN" ]; then ufw delete allow 8765/tcp >/dev/null 2>&1 || true; else ufw allow 8765/tcp >/dev/null; fi |
| 442 | ufw allow 80/tcp >/dev/null; ufw allow 443/tcp >/dev/null # Let's Encrypt + https |
| 443 | ufw allow 3478/udp >/dev/null; ufw allow 49152:65535/udp >/dev/null |
| 444 | ufw --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) |
| 447 | for u in avahi-daemon.socket avahi-daemon.service cups-browsed.service; do |
| 448 | systemctl disable --now "$u" >/dev/null 2>&1 || true |
| 449 | done |
| 450 | |
| 451 | echo ">> services" |
| 452 | AFTER=$(snapshot) |
| 453 | systemctl daemon-reload |
| 454 | for 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 |
| 463 | done |
| 464 | systemctl enable --now nc-xorg nc-desktop nc-audio nc-rendezvous nc-host nc-object-server nc-object-daemon nc-voice |
| 465 | sleep 4 |
| 466 | systemctl is-active nc-xorg nc-desktop nc-audio nc-rendezvous nc-host nc-object-server nc-object-daemon nc-voice | paste -sd' ' - |
| 467 | nc-object-bootstrap || echo "!! object server bootstrap failed" |
| 468 | # the dbbasic apps built onto this desk (infra/apps.sh): menu entries, icons, file types |
| 469 | nc-apps install || echo "!! dbbasic apps not installed" |
| 470 | # the object server's files as ~/Objects (needs the key the bootstrap minted) |
| 471 | systemctl enable nc-object-files >/dev/null 2>&1 |
| 472 | systemctl restart nc-object-files && nc-object-files status | sed 's/^/ ~\/Objects: /' || echo "!! ~/Objects did not mount" |
| 473 | systemctl 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 |
| 475 | for i in 1 2 3 4 5; do nc-session-setup >/dev/null 2>&1 && break; sleep 2; done |
| 476 | echo |
| 477 | echo ">> verify" |
| 478 | sleep 3 |
| 479 | sh "$(dirname "$0")/verify.sh" || echo "!! verify found problems above" |
| 480 | echo |
| 481 | if [ -n "$NC_DOMAIN" ]; then echo "rendezvous: https://${NC_DOMAIN}"; else echo "rendezvous: http://${NC_PUBLIC_IP}:8765"; fi |
| 482 | echo "login and PIN: infra/droplet.sh creds (stored in /etc/nc/env${DESK:+ -> /desk/nc/env})" |