1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
''
|