# 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 ''