summaryrefslogtreecommitdiffstats
path: root/configs/claude.nix
diff options
context:
space:
mode:
Diffstat (limited to 'configs/claude.nix')
-rw-r--r--configs/claude.nix70
1 files changed, 61 insertions, 9 deletions
diff --git a/configs/claude.nix b/configs/claude.nix
index 6084c39..6cb2405 100644
--- a/configs/claude.nix
+++ b/configs/claude.nix
@@ -3,7 +3,8 @@
# 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 systemctl start claude-remote-control@NAME, until the next boot).
+# or with systemctl start claude-remote-control@NAME, which lasts, across
+# reboots, until systemctl stop (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,
@@ -27,6 +28,30 @@ let
fi
exec ${tmux "\"$name\""} attach -t claude
'';
+
+ # 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 ];
@@ -54,6 +79,7 @@ in
"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
@@ -108,18 +134,44 @@ in
User = "claude";
WorkingDirectory = "/home/claude";
RuntimeDirectory = "claude/%i";
- # A directory in ~/remotes: neither ~/remotes itself, nor anything
- # above, nor a dot file.
- ExecStartPre = "${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";
+ 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-attach ];
}