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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# Claude Code, on the Claude subscription, as Remote Control servers: their
# sessions run here and are driven from claude.ai/code or the Claude app.
# 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 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-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 tool for the remotes.
claude-remote = pkgs.callPackage ../pkgs/claude-remote.nix {
inherit stateDir;
systemd = config.systemd.package; # the guest's, which may be slimmer
};
# 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
# in stateDir while it's meant to run, i.e. from its start until it gets
# stopped (not by a failure, after which it restarts, nor by a
# shutdown). Below /home/.var/lib/claude, the claude facet's part of
# what would be in /var/lib (which is on the tmpfs), on the home drive:
# root's, and readable for anyone.
stateDir = "/home/.var/lib/claude/remote-control";
static = map (u: lib.removeSuffix ".service" (lib.removePrefix "claude-remote-control@" u))
(lib.filter (lib.hasPrefix "claude-remote-control@") config.systemd.targets.multi-user.wants);
systemctl = "${config.systemd.package}/bin/systemctl";
isStatic = name: ''case " ${toString static} " in *" ${name} "*) true ;; *) false ;; esac'';
remember = pkgs.writeShellScript "claude-remote-control-remember" ''
if ! ${isStatic "$1"}; then
touch ${stateDir}/"$1"
fi
'';
forget = pkgs.writeShellScript "claude-remote-control-forget" ''
if test "''${SERVICE_RESULT-}" = success &&
test "$(${systemctl} is-system-running || :)" != stopping; then
rm -f ${stateDir}/"$1"
fi
'';
in
{
imports = [ ../modules/claude-code.nix ];
fc.home.required = true;
# For Claude to build and try things with (nix-build, nix-shell), see
# the service's path; with it, the run script requires --store.
assertions = [{
assertion = config.nix.enable;
message = "configs/claude.nix needs nix: put run-firecracker-vm's layers/nix.nix into the layers, before it.";
}];
users.users.claude = {
isNormalUser = true;
# Fixed, for the files on the home drive: users get created anew at
# every boot, and their uids would depend on the other users there.
uid = 1000;
home = "/home/claude";
# /home is mounted after users get created, see tmpfiles below.
createHome = false;
packages = lib.filter lib.isDerivation config.systemd.services."claude-remote-control@".path;
};
systemd.tmpfiles.rules = [
"d /home/claude 0700 claude users -"
"d /home/claude/src 0755 claude users -"
"d /home/claude/remotes 0755 claude users -"
"d ${stateDir} 0755 root root -"
];
# The repositories, for every remote (and every claude here, as managed
# settings are the machine's, and win over anyone's own).
environment.etc."claude-code/managed-settings.json".text = builtins.toJSON {
permissions.additionalDirectories = [ "/home/claude/src" ];
};
systemd.targets.multi-user.wants = [ "claude-remote-control@main.service" ];
# In a tmux, for a terminal: Remote Control asks its questions there, and
# shows the session's URL. It exits after about 10 minutes without
# network, which ends the session and the server, and systemd starts it
# again, which brings back the sessions it served. Sessions start in
# ~/remotes/NAME, which gets created if need be: a directory of its own,
# as Claude Code never saves trust for the home directory itself, and
# without a terminal to ask, it wouldn't start. A tmux each, as a second
# tmux new-session in the same server would exit without a process for
# systemd to follow, and in a runtime directory each, as stopping one
# removes its own.
systemd.services."claude-remote-control@" = {
wants = [ "network-online.target" ];
after = [ "network-online.target" "systemd-tmpfiles-setup.service" ];
unitConfig.RequiresMountsFor = "/home/claude";
# Without the drive, there's no mount to require, and /home is on the
# tmpfs: fail rather than keep the state there.
unitConfig.AssertPathIsMountPoint = "/home";
path = [
pkgs.claude-code
config.nix.package
pkgs.bash
pkgs.coreutils
pkgs.diffutils
pkgs.findutils
pkgs.gawk
pkgs.gitMinimal
pkgs.gnugrep
pkgs.gnused
pkgs.ripgrep
# And, after these, the system's, like fc-egress or claude-remote.
"/run/current-system/sw"
];
environment = {
# Telemetry and error reports go to Datadog. Unlike
# CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC, these leave Remote Control
# alone.
DISABLE_TELEMETRY = "1";
DISABLE_ERROR_REPORTING = "1";
# The claude.ai account's connectors, via mcp-proxy.anthropic.com.
ENABLE_CLAUDEAI_MCP_SERVERS = "false";
# Sessions named after the remote (NAME-...), rather than after the
# host, which they all share.
CLAUDE_REMOTE_CONTROL_SESSION_NAME_PREFIX = "%i";
};
serviceConfig = {
Type = "forking";
User = "claude";
WorkingDirectory = "/home/claude";
RuntimeDirectory = "claude/%i";
ExecStartPre = [
# A directory in ~/remotes: neither ~/remotes itself, nor anything
# above, nor a dot file.
"${pkgs.writeShellScript "claude-remote-control-dir" ''
case $1 in
"" | .* | */*) echo "claude-remote-control@$1: not a name for a directory" >&2; exit 1
esac
mkdir -p /home/claude/remotes/"$1"
''} %i"
# As root (+), stateDir being root's.
"+${remember} %i"
];
ExecStart = "${tmux "%i"} new-session -d -s claude -c /home/claude/remotes/%i ${lib.getExe pkgs.claude-code} remote-control";
ExecStopPost = "+${forget} %i";
Restart = "always";
RestartSec = 10;
};
};
# Starts the remotes in stateDir again, after a reboot, but for those
# that are static now, which it forgets instead.
systemd.services.claude-remote-control-restore = {
wantedBy = [ "multi-user.target" ];
after = [ "systemd-tmpfiles-setup.service" ];
unitConfig.RequiresMountsFor = stateDir;
unitConfig.AssertPathIsMountPoint = "/home";
serviceConfig.Type = "oneshot";
script = ''
for f in ${stateDir}/*; do
test -f "$f" || continue
name=''${f##*/}
if ${isStatic "$name"}; then
rm -f "$f"
else
${systemctl} start --no-block "claude-remote-control@$name.service"
fi
done
'';
};
environment.systemPackages = [ claude-remote ];
}
|