summaryrefslogtreecommitdiffstats
path: root/configs
diff options
context:
space:
mode:
Diffstat (limited to 'configs')
-rw-r--r--configs/claude.nix298
1 files changed, 189 insertions, 109 deletions
diff --git a/configs/claude.nix b/configs/claude.nix
index b1dcf86..bf64e41 100644
--- a/configs/claude.nix
+++ b/configs/claude.nix
@@ -7,6 +7,8 @@
# pkgs/claude-remote.nix), which lasts, across reboots, until
# claude-remote stop NAME (see stateDir). One taken out of the layers
# goes on like that, too, after the reboot (see staticFile).
+# Or a remote can be an interactive session instead, claude-interactive@NAME
+# (see istateDir): claude-remote start NAME --interactive.
# 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,
@@ -53,82 +55,105 @@ let
# root's, and readable for anyone.
stateDir = "/home/.var/lib/claude/remote-control";
# The remotes that were static at the last boot, so that the restore
- # service makes the ones that aren't anymore dynamic, and they go on.
+ # service makes the ones that aren't anymore dynamic, and they go on;
+ # and the interactive ones (staticI), likewise.
staticFile = "/home/.var/lib/claude/static-remotes";
- 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);
+ staticFileI = "/home/.var/lib/claude/static-interactive";
+ # The static ones, by the units multi-user.target wants.
+ staticOf = prefix: map (u: lib.removeSuffix ".service" (lib.removePrefix prefix u))
+ (lib.filter (lib.hasPrefix prefix) config.systemd.targets.multi-user.wants);
+ static = staticOf "claude-remote-control@";
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"
+ isIn = list: name: ''case " ${toString list} " in *" ${name} "*) true ;; *) false ;; esac'';
+ isStatic = isIn static;
+ # NAME, meant to run (a file in DIR), from its start, but for a static
+ # one, until it gets stopped (not by a failure, nor a shutdown).
+ rememberIn = dir: list: pkgs.writeShellScript "claude-remember" ''
+ if ! ${isIn list "$1"}; then
+ touch ${dir}/"$1"
fi
'';
- forget = pkgs.writeShellScript "claude-remote-control-forget" ''
+ forgetIn = dir: pkgs.writeShellScript "claude-forget" ''
if test "''${SERVICE_RESULT-}" = success &&
test "$(${systemctl} is-system-running || :)" != stopping; then
- rm -f ${stateDir}/"$1"
+ rm -f ${dir}/"$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 -"
- ];
+ remember = rememberIn stateDir static;
+ forget = forgetIn stateDir;
- # The repositories, for every remote (and every claude here, as managed
- # settings are the machine's, and win over anyone's own); and the
- # skills synced from the claude.ai account that are of no use here
- # (documents, browsers, desktop control), off: hidden from the model
- # and from /, each by its name and as the account's (anthropic-skills:).
- environment.etc."claude-code/managed-settings.json".text = builtins.toJSON {
- permissions.additionalDirectories = [ "/home/claude/src" ];
- skillOverrides = lib.listToAttrs (lib.concatMap (s: [
- (lib.nameValuePair s "off")
- (lib.nameValuePair "anthropic-skills:${s}" "off")
- ]) [
- "built-in-browser" "chrome-browser" "computer-use" "deep-research"
- "docs" "docx" "pdf" "pptx" "skill-creator" "xlsx"
- ]);
- };
+ # Interactive sessions, claude-interactive@NAME: a remote, too, in
+ # ~/remotes/NAME, but one claude, interactive (in its tmux), with Remote
+ # Control, rather than a Remote Control server; a NAME is either. Its
+ # session has an id of its own, kept in sessionsDir/NAME (which is
+ # what makes NAME interactive), made at its first start, and resumed
+ # once there's a transcript; those started with systemctl come back
+ # after a reboot, like the remotes, by istateDir. More memory and CPU
+ # than a Remote Control server, and its session is archived on
+ # claude.ai while it's stopped, back once it runs again.
+ istateDir = "/home/.var/lib/claude/interactive";
+ sessionsDir = "/home/.var/lib/claude/interactive-sessions";
+ staticI = staticOf "claude-interactive@";
+ # (as root) NAME's session id, made at its first start
+ sessionId = pkgs.writeShellScript "claude-interactive-session-id" ''
+ f=${sessionsDir}/"$1"
+ test -s "$f" || cat /proc/sys/kernel/random/uuid > "$f"
+ '';
+ # claude, interactive, with Remote Control, in NAME's session: by its
+ # id, the first time, else resumed, once there's a transcript
+ interactive = pkgs.writeShellScript "claude-interactive" ''
+ id=$(cat ${sessionsDir}/"$1")
+ key=$(printf %s /home/claude/remotes/"$1" | tr '/.' '--')
+ if test -e /home/claude/.claude/projects/"$key"/"$id".jsonl; then
+ exec claude --remote-control "$1" --resume "$id"
+ else
+ exec claude --remote-control "$1" --session-id "$id"
+ fi
+ '';
+ # (as root) not NAME of the other kind, too, running (their tmux would
+ # collide, and so would their sessions)
+ notAlso = other: pkgs.writeShellScript "claude-not-also" ''
+ if ${systemctl} is-active -q "${other}@$1.service"; then
+ echo "$1 runs as ${other}@$1 already" >&2
+ exit 1
+ fi
+ '';
- systemd.targets.multi-user.wants = [ "claude-remote-control@main.service" ];
+ # A directory in ~/remotes: neither ~/remotes itself, nor anything
+ # above, nor a dot file.
+ remoteDir = pkgs.writeShellScript "claude-remote-dir" ''
+ case $1 in
+ "" | .* | */*) echo "$1: not a name for a directory in ~/remotes" >&2; exit 1
+ esac
+ mkdir -p /home/claude/remotes/"$1"
+ '';
+ # Remote Control's questions, answered, rather than asked in the
+ # terminal: enable it (remoteDialogSeen, once per machine; that's what
+ # the units are for), and trust ~/remotes/NAME, the remote's own
+ # directory, just made. In ~/.claude.json (once there is one, i.e.
+ # after the login), atomically; if a remote writes back what it read
+ # before, and an answer gets lost, Remote Control asks again, as before.
+ answers = pkgs.writeShellScript "claude-remote-control-trust" ''
+ f=/home/claude/.claude.json d=/home/claude/remotes/$1
+ test -f "$f" || exit 0
+ if ! ${pkgs.jq}/bin/jq -e --arg d "$d" '.remoteDialogSeen == true and .projects[$d].hasTrustDialogAccepted == true' "$f" >/dev/null; then
+ ${pkgs.jq}/bin/jq --arg d "$d" '.remoteDialogSeen = true | .projects[$d].hasTrustDialogAccepted = true' "$f" > "$f.trust" &&
+ mv "$f.trust" "$f" || rm -f "$f.trust"
+ fi
+ '';
# 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
+ # shows the session's URL; an interactive session is there, too.
+ # Remote Control 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@" = {
+ claudeUnit = { other, preStart ? [ ], remember, forget, command }: {
wants = [ "network-online.target" ];
after = [ "network-online.target" "systemd-tmpfiles-setup.service" ];
unitConfig.RequiresMountsFor = "/home/claude";
@@ -168,40 +193,83 @@ in
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"
- # Remote Control's questions, answered, rather than asked in the
- # terminal: enable it (remoteDialogSeen, once per machine; that's
- # what the units are for), and trust ~/remotes/NAME, the remote's
- # own directory, just made. In ~/.claude.json (once there is
- # one, i.e. after the login), atomically; if a remote writes back
- # what it read before, and an answer gets lost, Remote Control
- # asks again, as before.
- "${pkgs.writeShellScript "claude-remote-control-trust" ''
- f=/home/claude/.claude.json d=/home/claude/remotes/$1
- test -f "$f" || exit 0
- if ! ${pkgs.jq}/bin/jq -e --arg d "$d" '.remoteDialogSeen == true and .projects[$d].hasTrustDialogAccepted == true' "$f" >/dev/null; then
- ${pkgs.jq}/bin/jq --arg d "$d" '.remoteDialogSeen = true | .projects[$d].hasTrustDialogAccepted = true' "$f" > "$f.trust" &&
- mv "$f.trust" "$f" || rm -f "$f.trust"
- fi
- ''} %i"
- # As root (+), stateDir being root's.
+ "+${notAlso other} %i"
+ "${remoteDir} %i"
+ "${answers} %i"
+ ] ++ preStart ++ [
+ # As root (+), the state being root's.
"+${remember} %i"
];
- # --name: its session named after the remote, rather than after the
- # host, which they all share.
- ExecStart = "${tmux "%i"} new-session -d -s claude -c /home/claude/remotes/%i ${lib.getExe pkgs.claude-code} remote-control --name %i";
+ ExecStart = "${tmux "%i"} new-session -d -s claude -c /home/claude/remotes/%i ${command}";
ExecStopPost = "+${forget} %i";
Restart = "always";
RestartSec = 10;
};
};
+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 -"
+ "d ${istateDir} 0755 root root -"
+ "d ${sessionsDir} 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); and the
+ # skills synced from the claude.ai account that are of no use here
+ # (documents, browsers, desktop control), off: hidden from the model
+ # and from /, each by its name and as the account's (anthropic-skills:).
+ environment.etc."claude-code/managed-settings.json".text = builtins.toJSON {
+ permissions.additionalDirectories = [ "/home/claude/src" ];
+ skillOverrides = lib.listToAttrs (lib.concatMap (s: [
+ (lib.nameValuePair s "off")
+ (lib.nameValuePair "anthropic-skills:${s}" "off")
+ ]) [
+ "built-in-browser" "chrome-browser" "computer-use" "deep-research"
+ "docs" "docx" "pdf" "pptx" "skill-creator" "xlsx"
+ ]);
+ };
+
+ systemd.targets.multi-user.wants = [ "claude-remote-control@main.service" ];
+
+ systemd.services."claude-remote-control@" = claudeUnit {
+ other = "claude-interactive";
+ inherit remember forget;
+ # --name: its session named after the remote, rather than after the
+ # host, which they all share.
+ command = "${lib.getExe pkgs.claude-code} remote-control --name %i";
+ };
+ systemd.services."claude-interactive@" = claudeUnit {
+ other = "claude-remote-control";
+ preStart = [ "+${sessionId} %i" ];
+ remember = rememberIn istateDir staticI;
+ forget = forgetIn istateDir;
+ command = "${interactive} %i";
+ };
# Starts the remotes in stateDir again, after a reboot, but for those
# that are static now, which it forgets instead; and before that, makes
@@ -210,7 +278,7 @@ in
# they have a directory (to retire one: claude-remote stop NAME); and
# starts the static ones, too, which multi-user.target's start skipped
# if there was no login yet. Once per boot, by the path unit below,
- # once there's a login.
+ # once there's a login. And the interactive sessions, likewise.
systemd.services.claude-remote-control-restore = {
after = [ "systemd-tmpfiles-setup.service" ];
unitConfig.RequiresMountsFor = stateDir;
@@ -219,26 +287,38 @@ in
# active, once done, so that the path unit doesn't start it again
serviceConfig.RemainAfterExit = true;
script = ''
- if test -f ${staticFile}; then
- for name in $(cat ${staticFile}); do
- if ! ${isStatic "$name"} && test -d /home/claude/remotes/"$name"; then
- touch ${stateDir}/"$name"
- fi
- done
- fi
- printf '%s\n' ${lib.escapeShellArgs static} > ${staticFile}
- 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"
+ # KIND STATIC-FILE STATE-DIR STATIC...: the remotes of KIND
+ # (claude-KIND@NAME) that were static at the last boot (in
+ # STATIC-FILE), but aren't anymore, made dynamic; the dynamic ones
+ # (in STATE-DIR) started, but for those that are static now, which
+ # get forgotten; and the static ones started.
+ restore() {
+ local kind=$1 file=$2 dir=$3 f name statics
+ shift 3
+ statics=" $* "
+ if test -f "$file"; then
+ for name in $(cat "$file"); do
+ case $statics in *" $name "*) continue ;; esac
+ if test -d /home/claude/remotes/"$name"; then
+ touch "$dir/$name"
+ fi
+ done
fi
- done
- for name in ${lib.escapeShellArgs static}; do
- ${systemctl} start --no-block "claude-remote-control@$name.service"
- done
+ printf '%s\n' "$@" > "$file"
+ for f in "$dir"/*; do
+ test -f "$f" || continue
+ name=''${f##*/}
+ case $statics in
+ *" $name "*) rm -f "$f" ;;
+ *) ${systemctl} start --no-block "claude-$kind@$name.service"
+ esac
+ done
+ for name; do
+ ${systemctl} start --no-block "claude-$kind@$name.service"
+ done
+ }
+ restore remote-control ${staticFile} ${stateDir} ${lib.escapeShellArgs static}
+ restore interactive ${staticFileI} ${istateDir} ${lib.escapeShellArgs staticI}
'';
};
# The restore service, once there's a login: at boot, if there's one