summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--configs/claude.nix62
1 files changed, 42 insertions, 20 deletions
diff --git a/configs/claude.nix b/configs/claude.nix
index d421bd2..4ff0a5b 100644
--- a/configs/claude.nix
+++ b/configs/claude.nix
@@ -1,17 +1,28 @@
-# Claude Code, on the Claude subscription, as a Remote Control server:
-# its sessions run here and are driven from claude.ai/code or the Claude
-# app. It runs as claude in a tmux, which root opens with claude-attach.
-# Once, as root: runuser -l claude -c 'claude auth login', then
-# claude-attach and answer Remote Control's questions there (enable it,
-# trust ~/src). The state is in /home/claude, i.e. on the run script's
-# --home drive, which is required.
+# 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 directory in ~claude, claude-remote-control@DIR, ~/src by default;
+# more by adding them in a layer:
+# systemd.targets.multi-user.wants = [ "claude-remote-control@DIR.service" ];
+# (or systemctl start claude-remote-control@DIR, until the next boot).
+# Each runs as claude in a tmux of its own, which root opens with
+# claude-attach [DIR]. Once, as root: runuser -l claude -c 'claude auth
+# login', then, for each DIR, claude-attach DIR and answer Remote
+# Control's questions there (enable it, trust ~/DIR). The state is in
+# /home/claude, i.e. on the run script's --home drive, which is required.
{ config, lib, pkgs, ... }:
let
- tmux = "${pkgs.tmux}/bin/tmux -S /run/claude/tmux";
+ # The tmux of claude-remote-control@DIR.
+ tmux = dir: "${pkgs.tmux}/bin/tmux -S /run/claude/${dir}/tmux";
- # Root's way into claude's tmux.
+ # Root's way into claude's tmuxes.
claude-attach = pkgs.writeShellScriptBin "claude-attach" ''
- exec ${tmux} attach -t claude
+ # usage: claude-attach [DIR] (default: src)
+ dir=''${1:-src}
+ if ! test -S /run/claude/"$dir"/tmux; then
+ echo "claude-attach: no claude-remote-control@$dir running" >&2
+ exit 1
+ fi
+ exec ${tmux "\"$dir\""} attach -t claude
'';
in
{
@@ -34,21 +45,24 @@ in
home = "/home/claude";
# /home is mounted after users get created, see tmpfiles below.
createHome = false;
- packages = config.systemd.services.claude-remote-control.path;
+ packages = config.systemd.services."claude-remote-control@".path;
};
- # Sessions start in ~/src: Claude Code never saves trust for the home
- # directory itself, and without a terminal to ask, it wouldn't start.
systemd.tmpfiles.rules = [
"d /home/claude 0700 claude users -"
- "d /home/claude/src 0755 claude users -"
];
+ systemd.targets.multi-user.wants = [ "claude-remote-control@src.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.
- systemd.services.claude-remote-control = {
- wantedBy = [ "multi-user.target" ];
+ # again, which brings back the sessions it served. Sessions start in
+ # ~/DIR, which gets created if need be: 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";
@@ -80,9 +94,17 @@ in
serviceConfig = {
Type = "forking";
User = "claude";
- WorkingDirectory = "/home/claude/src";
- RuntimeDirectory = "claude";
- ExecStart = "${tmux} new-session -d -s claude ${lib.getExe pkgs.claude-code} remote-control";
+ WorkingDirectory = "/home/claude";
+ RuntimeDirectory = "claude/%i";
+ # A directory in ~claude: neither ~ 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 directory in ~claude" >&2; exit 1
+ esac
+ mkdir -p /home/claude/"$1"
+ ''} %i";
+ ExecStart = "${tmux "%i"} new-session -d -s claude -c /home/claude/%i ${lib.getExe pkgs.claude-code} remote-control";
Restart = "always";
RestartSec = 10;
};