Files
connector-popos/configure.d/30-agents.sh
T
davidandClaude Opus 5.5 bc07e1d3e3 Set up Pop!_OS for a Federated Core, like CoreDesktop's configure
A port of CoreDesktop's configure to Pop!_OS 24.04 with COSMIC, in the shape
of connector-macos: its own install.sh, update and configure, installed in
~/.local/share/federated-connector/connector.

Every configure item is ported: the Core (email, calendars and contacts
through GNOME Online Accounts and Evolution, as COSMIC has no accounts of its
own; Superpanel; files over davfs2; Element; Tally; the email watcher),
Tailscale, backups, AI agents in the sandbox and their sign-ins, languages
with mise, AI servers, printers, network storage and the agent sandbox.
Folders go in the COSMIC Files sidebar. Each item installs what it needs
from apt, Flathub or the tool's own apt repository when you set it up.

smoketest/run checks it in Ubuntu 24.04 containers with Docker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 14:20:47 -07:00

261 lines
9.4 KiB
Bash

#!/bin/bash
# configure reads TITLE and DESCRIPTION
# shellcheck disable=SC2034
TITLE="AI agents"
# shellcheck disable=SC2034
DESCRIPTION="Install Claude Code, opencode and Hermes, sandboxed"
# Exists once you've run this, even if you installed no agents
AGENTS_CONF="$CONF_DIR/agents"
CLAUDE_SETTINGS="$HOME/.claude/settings.json"
BASE_CONFIG="$CONNECTOR/sandbox/config/base.json"
BASHRC="$HOME/.bashrc"
BLOCK_START="# >>> Federated Connector: agents >>>"
BLOCK_END="# <<< Federated Connector: agents <<<"
APPARMOR_PROFILE="/etc/apparmor.d/federated-connector-bwrap"
source "$CONNECTOR/configure.d/lib/json.sh"
source "$CONNECTOR/configure.d/lib/tools.sh"
# name|title|installer|installer's arguments. Each agent's own installer, from
# its makers. --non-interactive skips Hermes' setup questions and its gateway
# service; configure hermes chooses a model provider afterwards.
AGENTS=(
"claude|Claude Code (Anthropic)|https://claude.ai/install.sh|"
"opencode|opencode|https://opencode.ai/install|"
"hermes|Hermes Agent (Nous Research)|https://hermes-agent.nousresearch.com/install.sh|--non-interactive"
)
# srt (and Claude Code's own sandbox) use bubblewrap for the files an agent
# can see, socat to link it to the network proxy, and ripgrep to find the
# files it must not change. Node.js' own builds need libatomic.
SANDBOX_PACKAGES=(bubblewrap socat ripgrep libatomic1)
# --- Typing an agent's name runs it in the sandbox -------------------------------
# The block goes last in ~/.bashrc, so agents' installers adding their own
# folders to PATH don't come before it
path_block() {
cat << EOF
$BLOCK_START
# Typing an AI agent's name runs it in the sandbox: configure agents
export PATH="$SANDBOX_BIN:\$PATH"
$BLOCK_END
EOF
}
on_path() {
[ -f "$BASHRC" ] && [ "$(grep -v '^[[:space:]]*$' "$BASHRC" | tail -n 4)" = "$(path_block)" ]
}
put_on_path() {
local tmp
touch "$BASHRC" || return 1
tmp="$(mktemp)" || return 1
awk -v start="$BLOCK_START" -v end="$BLOCK_END" \
'$0 == start { skip = 1 } !skip { print } $0 == end { skip = 0 }' "$BASHRC" > "$tmp"
{ cat "$tmp"; echo; path_block; } > "$BASHRC"
rm -f "$tmp"
echo "Agents run in the sandbox in new terminals. To run one without it: unsandboxed <agent>"
}
# --- bubblewrap and Ubuntu's AppArmor ---------------------------------------------
# Ubuntu 24.04 lets only programs AppArmor allows make the user namespaces
# bubblewrap needs, and has no rule for bubblewrap itself
bwrap_works() {
bwrap --ro-bind / / --unshare-user --unshare-net true 2> /dev/null
}
allow_bwrap() {
echo "Ubuntu's AppArmor doesn't let bubblewrap, which the sandbox is built on, make"
echo "user namespaces. A profile for /usr/bin/bwrap allows it, as Ubuntu suggests."
confirm_yes "Add it ($APPARMOR_PROFILE)?" || return 1
sudo tee "$APPARMOR_PROFILE" > /dev/null << 'EOF' || return 1
# Written by Federated Connector (configure agents): let bubblewrap, which the
# AI agent sandbox uses, make user namespaces
abi <abi/4.0>,
include <tunables/global>
profile federated-connector-bwrap /usr/bin/bwrap flags=(unconfined) {
userns,
include if exists <local/federated-connector-bwrap>
}
EOF
sudo apparmor_parser -r "$APPARMOR_PROFILE" || return 1
bwrap_works || { echo "bubblewrap still can't make user namespaces."; return 1; }
}
# --- Claude Code -------------------------------------------------------------------
# Claude Code runs every command it starts in its own sandbox, also built on
# bubblewrap and socat; it can't run inside srt because bubblewrap can't be
# nested. Anything already in the sandbox section of your settings wins over
# these defaults.
claude_sandbox_on() {
jq -e '.sandbox.enabled == true' "$CLAUDE_SETTINGS" &> /dev/null
}
turn_on_claude_sandbox() {
local domains
domains="$(jq '.network.allowedDomains' "$BASE_CONFIG")" || return 1
edit_json "$CLAUDE_SETTINGS" '{}' --argjson domains "$domains" \
'.sandbox = ({enabled: true, network: {allowedDomains: $domains}} * (.sandbox // {}))' || return 1
echo "Claude Code runs the commands it starts in its own sandbox."
}
# A desktop notification when Claude Code needs you or is done, if you have no
# settings of your own yet
claude_notifications() {
[ -f "$CLAUDE_SETTINGS" ] && return 0
edit_json "$CLAUDE_SETTINGS" '{}' \
--arg needs "notify-send 'Claude Code' 'Needs your input'" \
--arg finished "notify-send 'Claude Code' 'Task completed'" \
'.hooks = {
Notification: [{matcher: "", hooks: [{type: "command", command: $needs}]}],
Stop: [{matcher: "", hooks: [{type: "command", command: $finished}]}]
}' || return 1
echo "Claude Code shows a notification when it needs you and when it's done."
}
# --- Hermes' gateway and Cline Desktop -----------------------------------------------
# Hermes' gateway acts on messages from outside this machine, so it runs in
# the sandbox too: a systemd drop-in starts it through sandbox/bin/hermes. It
# also needs to reach the Matrix server in Hermes' settings, which differs on
# each machine; for other messaging services, add their domains to
# ~/.config/federated-connector/sandbox/hermes.json.
GATEWAY_UNIT="$HOME/.config/systemd/user/hermes-gateway.service"
GATEWAY_DROPIN="$GATEWAY_UNIT.d/federated-connector-sandbox.conf"
gateway_dropin() {
local homeserver
homeserver="$(sed -nE "s#^MATRIX_HOMESERVER=[\"']?https?://([^/:\"']+).*#\1#p" "$HOME/.hermes/.env" 2> /dev/null | head -n 1)"
cat << EOF
# Written by Federated Connector (configure agents): run Hermes' gateway in the sandbox
[Service]
Environment="FEDERATED_SANDBOX_DOMAINS=$homeserver"
ExecStart=
ExecStart=$SANDBOX_BIN/hermes gateway run
EOF
}
gateway_ok() {
[ ! -f "$GATEWAY_UNIT" ] || [ "$(cat "$GATEWAY_DROPIN" 2> /dev/null)" = "$(gateway_dropin)" ]
}
sandbox_gateway() {
gateway_ok && return 0
mkdir -p "$(dirname "$GATEWAY_DROPIN")" && gateway_dropin > "$GATEWAY_DROPIN" \
&& systemctl --user daemon-reload || return 1
echo "Hermes' gateway runs in the sandbox (from its next start)."
}
# Cline Desktop is opened from the app menu, so an entry of the same name in
# ~/.local/share/applications, which wins over the package's, runs it in the
# sandbox. It's removed when the package is.
CLINE_DESKTOP="/usr/share/applications/cline-desktop.desktop"
CLINE_ENTRY="$HOME/.local/share/applications/cline-desktop.desktop"
cline_entry() {
sed "s#^Exec=.*#Exec=$SANDBOX_BIN/cline-desktop %U#" "$CLINE_DESKTOP"
}
cline_ok() {
if [ -f "$CLINE_DESKTOP" ]; then
[ "$(cat "$CLINE_ENTRY" 2> /dev/null)" = "$(cline_entry)" ]
else
[ ! -f "$CLINE_ENTRY" ]
fi
}
sandbox_cline() {
cline_ok && return 0
if [ -f "$CLINE_DESKTOP" ]; then
mkdir -p "$(dirname "$CLINE_ENTRY")" && cline_entry > "$CLINE_ENTRY" || return 1
echo "Cline Desktop runs in the sandbox."
else
rm -f "$CLINE_ENTRY"
fi
}
# --- The item -------------------------------------------------------------------------
sandbox_ready() {
srt_path > /dev/null && on_path && pkg_installed "${SANDBOX_PACKAGES[@]}" && bwrap_works \
&& { ! agent_installed claude || claude_sandbox_on; } && gateway_ok && cline_ok
}
is_configured() {
[ -f "$AGENTS_CONF" ] && sandbox_ready
}
# Download the installer, then run it, rather than piping it into bash
install_agent() {
local title="$1" url="$2" args="$3" tmp status
tmp="$(mktemp -d)" || return 1
echo "Downloading $title's installer from $url"
if curl -fsSL --max-time 120 -o "$tmp/install.sh" "$url"; then
# shellcheck disable=SC2086
(cd "$tmp" && bash ./install.sh $args)
status=$?
else
echo "Couldn't download it."
status=1
fi
rm -rf "$tmp"
return $status
}
run_configure() {
local entry name title url args status=0
mkdir -p "$CONF_DIR" && touch "$AGENTS_CONF" || return 1
echo "AI coding agents, run in a sandbox: an agent can change files only in the folder"
echo "you start it in, can't read your keys, passwords, browsers, mail or messages, and"
echo "reaches only AI providers and developer sites."
echo
for entry in "${AGENTS[@]}"; do
IFS='|' read -r name title url args <<< "$entry"
if agent_installed "$name"; then
echo " $title: installed"
elif confirm_yes "Install $title?"; then
if ! install_agent "$title" "$url" "$args" || ! agent_installed "$name"; then
echo -e "\e[31m$title was not installed.\e[0m"
status=1
fi
fi
done
echo
pkg_ensure "The sandbox" "${SANDBOX_PACKAGES[@]}" || return 1
if ! bwrap_works; then
if [ "$(sysctl -n kernel.apparmor_restrict_unprivileged_userns 2> /dev/null)" = 1 ]; then
allow_bwrap || { echo -e "\e[31mWithout it, the agents can't run in the sandbox.\e[0m"; return 1; }
else
echo -e "\e[31mbubblewrap can't make user namespaces on this computer, so the sandbox can't run.\e[0m"
return 1
fi
fi
srt_ensure || { echo -e "\e[31mWithout srt, opencode and Hermes can't run in the sandbox.\e[0m"; return 1; }
if ! on_path; then
echo "Typing opencode or hermes should run it in the sandbox. That takes a line at the"
echo "end of ~/.bashrc."
if confirm_yes "Add it?"; then
put_on_path || status=1
else
echo "Then run them sandboxed as $SANDBOX_BIN/<agent>."
fi
fi
if agent_installed claude; then
claude_notifications || status=1
claude_sandbox_on || turn_on_claude_sandbox || status=1
fi
sandbox_gateway || status=1
sandbox_cline || status=1
echo
echo "To sign in to them: configure claude, configure opencode, configure hermes"
return $status
}