summaryrefslogtreecommitdiffstats
path: root/configs/claude.nix
diff options
context:
space:
mode:
Diffstat (limited to 'configs/claude.nix')
-rw-r--r--configs/claude.nix66
1 files changed, 39 insertions, 27 deletions
diff --git a/configs/claude.nix b/configs/claude.nix
index 4ff0a5b..6084c39 100644
--- a/configs/claude.nix
+++ b/configs/claude.nix
@@ -1,28 +1,31 @@
# 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.
+# 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).
+# 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-attach [NAME]. Once, as root: runuser -l
+# claude -c 'claude auth login', then, for each NAME, claude-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@DIR.
- tmux = dir: "${pkgs.tmux}/bin/tmux -S /run/claude/${dir}/tmux";
+ # The tmux of claude-remote-control@NAME.
+ tmux = name: "${pkgs.tmux}/bin/tmux -S /run/claude/${name}/tmux";
# Root's way into claude's tmuxes.
claude-attach = pkgs.writeShellScriptBin "claude-attach" ''
- # 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
+ # usage: claude-attach [NAME] (default: main)
+ name=''${1:-main}
+ if ! test -S /run/claude/"$name"/tmux; then
+ echo "claude-attach: no claude-remote-control@$name running" >&2
exit 1
fi
- exec ${tmux "\"$dir\""} attach -t claude
+ exec ${tmux "\"$name\""} attach -t claude
'';
in
{
@@ -49,19 +52,28 @@ in
};
systemd.tmpfiles.rules = [
"d /home/claude 0700 claude users -"
+ "d /home/claude/src 0755 claude users -"
+ "d /home/claude/remotes 0755 claude users -"
];
- systemd.targets.multi-user.wants = [ "claude-remote-control@src.service" ];
+ # 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
- # ~/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.
+ # ~/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" ];
@@ -96,15 +108,15 @@ in
User = "claude";
WorkingDirectory = "/home/claude";
RuntimeDirectory = "claude/%i";
- # A directory in ~claude: neither ~ itself, nor anything above, nor
- # a dot file.
+ # 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 directory in ~claude" >&2; exit 1
+ "" | .* | */*) echo "claude-remote-control@$1: not a name for a directory" >&2; exit 1
esac
- mkdir -p /home/claude/"$1"
+ mkdir -p /home/claude/remotes/"$1"
''} %i";
- ExecStart = "${tmux "%i"} new-session -d -s claude -c /home/claude/%i ${lib.getExe pkgs.claude-code} remote-control";
+ ExecStart = "${tmux "%i"} new-session -d -s claude -c /home/claude/remotes/%i ${lib.getExe pkgs.claude-code} remote-control";
Restart = "always";
RestartSec = 10;
};