diff options
| author | tv <tv@krebsco.de> | 2026-11-21 14:21:46 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-21 14:21:46 +0000 |
| commit | 0e9becdfccd96009464ea8acd36f88d27b8616f4 (patch) | |
| tree | d981006dfce377cf377c64fd959648efac10516f | |
| parent | 0030f5807a9c583ed575cee537f27e6edc25f5cd (diff) | |
configs/claude: claude-remote, root's tool for the remotes
claude-remote list (the default) shows the remotes, static, dynamic,
with a unit loaded, or with a directory, with their kind, state, and
whether they trust their directory yet; inspect NAME shows a remote's
unit, directory, trust, claude.ai session (bridge-pointer.json), and
the last lines of its terminal; start, stop and restart NAME go through
systemctl (a remote started here is dynamic: it lasts until stopped
here), and start points to attach while the remote doesn't trust its
directory; attach [NAME] opens its terminal (it's claude-attach, which
is gone); log NAME shows its journal. Which remotes are static, it
asks systemd (multi-user.target's wants), so changing them doesn't
change it. A package (pkgs/claude-remote.nix), with bash completion of
the commands and the remotes' names.
| -rw-r--r-- | configs/claude.nix | 31 | ||||
| -rw-r--r-- | pkgs/claude-remote.nix | 164 |
2 files changed, 177 insertions, 18 deletions
diff --git a/configs/claude.nix b/configs/claude.nix index e4fde84..05583a9 100644 --- a/configs/claude.nix +++ b/configs/claude.nix @@ -3,31 +3,26 @@ # One per remote, claude-remote-control@NAME, main by default; more by # adding them in a layer: # systemd.targets.multi-user.wants = [ "claude-remote-control@NAME.service" ]; -# or with systemctl start claude-remote-control@NAME, which lasts, across -# reboots, until systemctl stop (see stateDir). +# or with claude-remote start NAME (root's tool for the remotes, see +# pkgs/claude-remote.nix), which lasts, across reboots, until +# claude-remote stop NAME (see stateDir). # A remote is a context: it works in ~/remotes/NAME, which is its project # for Claude Code (sessions, memory, trust), on the repositories in ~/src, # which all remotes share. Each runs as claude in a tmux of its own, -# which root opens with claude-attach [NAME]. Once, as root: runuser -l -# claude -c 'claude auth login', then, for each NAME, claude-attach NAME -# and answer Remote Control's questions there (enable it, trust -# ~/remotes/NAME). The state is in /home/claude, i.e. on the run -# script's --home drive, which is required. +# which root opens with claude-remote attach [NAME]. Once, as root: +# runuser -l claude -c 'claude auth login', then, for each NAME, +# claude-remote attach NAME and answer Remote Control's questions there +# (enable it, trust ~/remotes/NAME). The state is in /home/claude, i.e. +# on the run script's --home drive, which is required. { config, lib, pkgs, ... }: let # The tmux of claude-remote-control@NAME. tmux = name: "${pkgs.tmux}/bin/tmux -S /run/claude/${name}/tmux"; - # Root's way into claude's tmuxes. - claude-attach = pkgs.writeShellScriptBin "claude-attach" '' - # usage: claude-attach [NAME] (default: main) - name=''${1:-main} - if ! test -S /run/claude/"$name"/tmux; then - echo "claude-attach: no claude-remote-control@$name running" >&2 - exit 1 - fi - exec ${tmux "\"$name\""} attach -t claude - ''; + # Root's tool for the remotes. + claude-remote = pkgs.callPackage ../pkgs/claude-remote.nix { + inherit stateDir; + }; # Remotes started with systemctl, rather than by multi-user.target's # wants (static), come back after a reboot: each has a file of its name @@ -176,5 +171,5 @@ in done ''; }; - environment.systemPackages = [ claude-attach ]; + environment.systemPackages = [ claude-remote ]; } diff --git a/pkgs/claude-remote.nix b/pkgs/claude-remote.nix new file mode 100644 index 0000000..10f94f4 --- /dev/null +++ b/pkgs/claude-remote.nix @@ -0,0 +1,164 @@ +# claude-remote, root's tool for the remotes (claude-remote-control@NAME, +# see configs/claude.nix): list, inspect, start, stop, restart, attach, +# log; with bash completion. +{ lib, runCommand, writeShellScript, writeText +, coreutils, findutils, gnugrep, gnused, jq, systemd, tmux + # Where remotes started with systemctl are kept, see configs/claude.nix. +, stateDir +}: +let + script = writeShellScript "claude-remote" '' + set -eu + export PATH=${lib.makeBinPath [ coreutils findutils gnugrep gnused jq systemd tmux ]} + # The static remotes: those multi-user.target wants, as the running + # system has it (so changing them doesn't change this script). + static=($(systemctl show --value -p Wants multi-user.target 2>/dev/null | + tr ' ' '\n' | sed -n 's/^claude-remote-control@\(.*\)\.service$/\1/p')) + state=${lib.escapeShellArg stateDir} + home=/home/claude + remotes=$home/remotes + + usage() { + cat <<'EOF' + usage: claude-remote [list] the remotes: name, kind, state, trusted + claude-remote inspect NAME a remote, in more detail + claude-remote start NAME start a remote (lasts until stop) + claude-remote stop NAME + claude-remote restart NAME + claude-remote attach [NAME] its terminal (tmux; default: main) + claude-remote log NAME [JOURNALCTL-OPTION...] + A remote is claude-remote-control@NAME, working in ~claude/remotes/NAME. + Static ones are started by the system's configuration; others, started + here, are dynamic: they last, across reboots, until stopped here. + EOF + } + die() { + echo "claude-remote: $*" >&2 + exit 1 + } + unit() { + printf 'claude-remote-control@%s.service' "$1" + } + valid() { + case $1 in "" | .* | */* | *[[:space:]]*) return 1 ;; esac + } + name_arg() { + valid "''${1-}" || die "not a remote's name: ''${1-}" + } + is_static() { + case " ''${static[*]} " in *" $1 "*) true ;; *) false ;; esac + } + kind() { + if is_static "$1"; then echo static + elif test -e "$state/$1"; then echo dynamic + else echo - + fi + } + active() { + systemctl is-active "$(unit "$1")" 2>/dev/null || : + } + trusted() { + jq -e --arg d "$remotes/$1" '.projects[$d].hasTrustDialogAccepted == true' \ + "$home/.claude.json" >/dev/null 2>&1 + } + # Claude Code's project directory for a path: every / and . a -. + project() { + printf '%s/.claude/projects/%s' "$home" "$(printf %s "$remotes/$1" | tr '/.' '--')" + } + # Every remote there is: static, dynamic, with a unit loaded, or with a + # directory. + names() { + { + printf '%s\n' "''${static[@]}" + find "$state" "$remotes" -mindepth 1 -maxdepth 1 -printf '%f\n' 2>/dev/null + systemctl list-units --all --plain --no-legend 'claude-remote-control@*' 2>/dev/null | + sed -n 's/^claude-remote-control@\([^ ]*\)\.service .*/\1/p' + } | while read -r n; do valid "$n" && echo "$n"; done | sort -u + } + hint_attach() { + if ! trusted "$1"; then + echo "$1 hasn't trusted ~claude/remotes/$1 yet: claude-remote attach $1, and answer Remote Control's questions there (C-b d to leave)." + fi + } + + cmd=''${1-list} + shift || : + case $cmd in + list) + printf '%-20s %-8s %-12s %s\n' NAME KIND STATE TRUSTED + names | while read -r n; do + printf '%-20s %-8s %-12s %s\n' "$n" "$(kind "$n")" "$(active "$n")" "$(trusted "$n" && echo yes || echo no)" + done + ;; + inspect) + name_arg "''${1-}" + n=$1 + echo "remote: $n ($(kind "$n"))" + echo "unit: $(unit "$n"): $(systemctl show --value -p ActiveState,SubState "$(unit "$n")" | paste -sd/), since $(systemctl show --value -p StateChangeTimestamp "$(unit "$n")")" + echo "directory: $remotes/$n$(test -d "$remotes/$n" || echo ' (not there yet)')" + echo "trusted: $(trusted "$n" && echo yes || echo no)" + p=$(project "$n")/bridge-pointer.json + if test -f "$p"; then + echo "claude.ai: $(jq -r '"session \(.sessionId), environment \(.environmentId)"' "$p")" + fi + if test -S /run/claude/"$n"/tmux; then + echo "terminal (claude-remote attach $n):" + tmux -S /run/claude/"$n"/tmux capture-pane -p -t claude -S -15 | sed '/^$/d; s/^/ /' + fi + hint_attach "$n" + ;; + start) + name_arg "''${1-}" + systemctl start "$(unit "$1")" + if ! is_static "$1"; then + echo "$1 is dynamic: it lasts, across reboots, until claude-remote stop $1." + fi + hint_attach "$1" + ;; + stop | restart) + name_arg "''${1-}" + systemctl "$cmd" "$(unit "$1")" + ;; + attach) + n=''${1:-main} + name_arg "$n" + test -S /run/claude/"$n"/tmux || die "$n isn't running (claude-remote start $n)" + exec tmux -S /run/claude/"$n"/tmux attach -t claude + ;; + log) + name_arg "''${1-}" + n=$1 + shift + exec journalctl -u "$(unit "$n")" "$@" + ;; + names) # for the completion + names + ;; + help | -h | --help) + usage + ;; + *) + usage >&2 + exit 1 + esac + ''; + + completion = writeText "claude-remote-completion" '' + _claude_remote() { + local cur=''${COMP_WORDS[COMP_CWORD]} + if [ "$COMP_CWORD" -eq 1 ]; then + COMPREPLY=($(compgen -W "list inspect start stop restart attach log help" -- "$cur")) + elif [ "$COMP_CWORD" -eq 2 ]; then + case ''${COMP_WORDS[1]} in + inspect | start | stop | restart | attach | log) + COMPREPLY=($(compgen -W "$(claude-remote names 2>/dev/null)" -- "$cur")) ;; + esac + fi + } + complete -F _claude_remote claude-remote + ''; +in +runCommand "claude-remote" { meta.mainProgram = "claude-remote"; } '' + install -D -m 755 ${script} $out/bin/claude-remote + install -D -m 644 ${completion} $out/share/bash-completion/completions/claude-remote +'' |
