diff options
| author | tv <tv@krebsco.de> | 2026-11-23 03:50:52 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-23 16:04:45 +0100 |
| commit | fdf1aa54c0f3335b092d7698c32f01389a07b661 (patch) | |
| tree | 4b573a70514a5fdc8281f74f297cd9094856d127 /configs | |
| parent | ec9e92b87e0c5afdbaafe10cd3db640a9fcf9f50 (diff) | |
configs/claude: tasks, claude-task@NAME:ID, by transient timers
A task is a prompt for a remote: claude -p, as claude, in ~/remotes/NAME,
with the remotes' PATH and environment (claudePath and claudeEnv, which
they share now), once there's a login, at a time (once), or at a
calendar event (again and again), per systemd.time(7), by a transient
timer (systemd-run --on-calendar, for the template unit). Its
definition is kept in /home/.var/lib/claude/tasks/NAME:ID (remote, kind,
normalized time, prompt, permission mode, and once it ran, last and
result), so that the restore service makes the timers again after a
reboot, and that they're backed up: a one-time one that ran is done, one
missed then runs now; missed recurring ones don't (yet). For
claude-remote --schedule.
Diffstat (limited to 'configs')
| -rw-r--r-- | configs/claude.nix | 118 |
1 files changed, 94 insertions, 24 deletions
diff --git a/configs/claude.nix b/configs/claude.nix index 4893a83..562294f 100644 --- a/configs/claude.nix +++ b/configs/claude.nix @@ -40,6 +40,7 @@ let inherit stateDir; systemd = config.systemd.package; # the guest's, which may be slimmer gitMinimal = git; + inherit taskTimer; }; # The login (runuser -l claude -c 'claude auth login'), which the @@ -142,6 +143,74 @@ let fi ''; + # What claude runs with, in the remotes and the tasks: their PATH, and + # environment. + claudePath = [ + pkgs.claude-code + config.nix.package + pkgs.bash + pkgs.coreutils + pkgs.diffutils + pkgs.findutils + pkgs.gawk + git + pkgs.gnugrep + pkgs.gnused + pkgs.ripgrep + # And, after these, the system's, like fc-egress or claude-remote. + "/run/current-system/sw" + ]; + claudeEnv = { + # 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"; + }; + + # Tasks, scheduled with claude-remote --schedule: a prompt for a remote, + # claude -p in ~/remotes/NAME, at a time (once), or at a calendar event + # (again and again), per systemd.time(7); each claude-task@NAME:ID, by a + # transient timer. Their definitions are in tasksDir/NAME:ID (remote, + # kind: at or every, when: normalized, prompt, mode, and once it ran, + # last and result), so that the restore service makes their timers + # again, after a reboot (and that they're backed up). + tasksDir = "/home/.var/lib/claude/tasks"; + # (as root) task NAME:ID's timer, from its definition: at its time; but a + # one-time one that ran is done, and one missed (the VM was off then) + # runs now + taskTimer = pkgs.writeShellScript "claude-task-timer" '' + set -eu + d=${tasksDir}/$1 unit=claude-task@$1 + if ${systemctl} is-active -q "$unit.timer"; then exit 0; fi + when=$(cat "$d/when") + if test "$(cat "$d/kind")" = at; then + test ! -e "$d/last" || exit 0 + if test "$(date -d "$when" +%s)" -le "$(date +%s)"; then + ${systemctl} start --no-block "$unit.service" + exit + fi + fi + ${config.systemd.package}/bin/systemd-run --quiet --unit="$unit" \ + --on-calendar="$when" --timer-property=AccuracySec=1s \ + --description="claude task $1" + ''; + # (as claude) task NAME:ID: its prompt, in its remote's directory + claudeTask = pkgs.writeShellScript "claude-task" '' + d=${tasksDir}/$1 + cd /home/claude/remotes/"$(cat "$d/remote")" + exec claude -p "$(cat "$d/prompt")" --permission-mode "$(cat "$d/mode")" + ''; + # (as root) task NAME:ID, run: when, and how it went + taskDone = pkgs.writeShellScript "claude-task-done" '' + d=${tasksDir}/$1 + test -d "$d" || exit 0 + date -Is > "$d/last" + printf '%s\n' "''${SERVICE_RESULT-}" > "$d/result" + ''; + # In a tmux, for a terminal: Remote Control asks its questions there, and # shows the session's URL; an interactive session is there, too. # Remote Control exits after about 10 minutes without network, which @@ -163,30 +232,8 @@ let # 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 - git - 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"; - }; + path = claudePath; + environment = claudeEnv; serviceConfig = { Type = "forking"; User = "claude"; @@ -236,6 +283,7 @@ in "d ${stateDir} 0755 root root -" "d ${istateDir} 0755 root root -" "d ${sessionsDir} 0755 root root -" + "d ${tasksDir} 0755 root root -" ]; # The repositories, for every remote (and every claude here, as managed @@ -271,6 +319,22 @@ in command = "${interactive} %i"; }; + # A task's run (see tasksDir), as its remote's would be, but once, and + # not before the login. + systemd.services."claude-task@" = { + wants = [ "network-online.target" ]; + after = [ "network-online.target" ]; + unitConfig.RequiresMountsFor = "/home/claude"; + unitConfig.ConditionPathExists = login; + path = claudePath; + environment = claudeEnv; + serviceConfig = { + Type = "oneshot"; + User = "claude"; + ExecStart = "${claudeTask} %i"; + ExecStopPost = "+${taskDone} %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 # the ones that were static at the last boot, but aren't anymore (taken @@ -319,6 +383,12 @@ in } restore remote-control ${staticFile} ${stateDir} ${lib.escapeShellArgs static} restore interactive ${staticFileI} ${istateDir} ${lib.escapeShellArgs staticI} + # and the tasks' timers + for d in ${tasksDir}/*/; do + test -d "$d" || continue + d=''${d%/} + ${taskTimer} "''${d##*/}" || : + done ''; }; # The restore service, once there's a login: at boot, if there's one |
