fix(discord-agent-workspace): confine the bot with bwrap in a user unit, not ProtectHome=

Where the user manager can't mount (Ubuntu's
kernel.apparmor_restrict_unprivileged_userns=1), ProtectHome= and
friends are dropped without an error and the service runs unconfined.
Ship a generic unit, as cloros1 runs: tmux for /login, bwrap for the
confinement.

Generated-by: Claude Opus 5.5
This commit is contained in:
dckc committed 2026-11-23 15:41:30 -06:00
1 parent cf83169064
commit e16e856eca
2 files changed
+48 -1

No files matched your search

@@ -0,0 +1,43 @@
# claude-bot: Claude Code Discord bot, confined to one workspace folder.
# Install: ~/.config/systemd/user/claude-bot.service; systemctl --user enable --now claude-bot
# loginctl enable-linger $USER (so it runs without you logged in)
# Attach: tmux -S /run/user/$UID/claude-bot/tmux.sock attach (detach: C-b d; /login happens here)
[Unit]
Description=Claude Code Discord bot
[Service]
Type=forking
WorkingDirectory=%h/workspace
Environment=PATH=%h/.local/bin:/usr/local/bin:/usr/bin:/bin
Environment=TERM=xterm-256color
RuntimeDirectory=claude-bot
# Optional: GH_TOKEN=<a fine-grained token of the bot's own, for just the repos it needs>
EnvironmentFile=-%h/.config/claude-bot/env
# The user manager has no network-online.target.
ExecStartPre=/bin/sh -c 'until getent hosts gateway.discord.gg >/dev/null; do sleep 2; done'
# bwrap, not ProtectHome= and friends: on Ubuntu (kernel.apparmor_restrict_unprivileged_userns=1)
# the user manager can't mount, and silently runs the service unconfined.
# Everything is read-only except the --bind paths; --tmpfs hides credential dirs.
# Hiding %t removes the systemd and D-Bus sockets (systemd-run would escape) and the ssh/gpg agents.
ExecStart=/usr/bin/tmux -S %t/claude-bot/tmux.sock new-session -d -s claude-bot \
/usr/bin/bwrap --die-with-parent \
--ro-bind / / --dev /dev --proc /proc --tmpfs /tmp --tmpfs %t \
--bind %h/workspace %h/workspace \
--bind %h/.claude %h/.claude \
--bind %h/.claude.json %h/.claude.json \
--bind %h/.cache %h/.cache \
--tmpfs %h/.ssh --tmpfs %h/.gnupg --tmpfs %h/.config/gh \
--ro-bind /dev/null %h/.git-credentials \
claude -n claude-bot --permission-mode auto --channels plugin:discord@claude-plugins-official
ExecStop=-/usr/bin/tmux -S %t/claude-bot/tmux.sock kill-server
# tmux exits 0 when claude exits, so on-failure would never fire.
Restart=always
RestartSec=30
[Install]
WantedBy=default.target