From bd3866713d5b041f6cee9182ba3f76ff8e5417b7 Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 20 Nov 2026 22:03:23 +0000 Subject: configs/claude.nix: init A Claude Code Remote Control server, on the Claude subscription, running as claude in a tmux which root opens with claude-attach. --- configs/claude.nix | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 configs/claude.nix (limited to 'configs') diff --git a/configs/claude.nix b/configs/claude.nix new file mode 100644 index 0000000..904ed5b --- /dev/null +++ b/configs/claude.nix @@ -0,0 +1,83 @@ +# 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. +{ config, lib, pkgs, ... }: +let + tmux = "${pkgs.tmux}/bin/tmux -S /run/claude/tmux"; + + # nixos-26.05 has 2.1.223; Opus 5.5 needs 2.1.280. From npm's platform + # package, which has the plain binary and an integrity hash; + # x86_64-linux only, like the package. + claude-code = pkgs.claude-code.overrideAttrs (_: rec { + version = "2.1.329"; + src = pkgs.fetchurl { + url = "https://registry.npmjs.org/@anthropic-ai/claude-code-linux-x64/-/claude-code-linux-x64-${version}.tgz"; + hash = "sha512-+n4lu1ubYAp02Y0X/MAUnxw9yY6SHHZ77F+IoW+V0wVoFuiI9opdH6gofjf14gXJCEYMTNpks7Gn1i8XZTKAWA=="; + }; + # installPhase installs $src as is. + preInstall = '' + tar -xzf $src package/claude + src=$PWD/package/claude + ''; + }); + + # Root's way into claude's tmux. + claude-attach = pkgs.writeShellScriptBin "claude-attach" '' + exec ${tmux} attach -t claude + ''; +in +{ + nixpkgs.config.allowUnfreePredicate = pkg: lib.getName pkg == "claude-code"; + + users.users.claude = { + isNormalUser = true; + home = "/home/claude"; + # /home is mounted after users get created, see tmpfiles below. + createHome = false; + packages = [ claude-code ]; + }; + # 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 -" + ]; + + # 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" ]; + wants = [ "network-online.target" ]; + after = [ "network-online.target" "systemd-tmpfiles-setup.service" ]; + unitConfig.RequiresMountsFor = "/home/claude"; + path = [ + claude-code + config.nix.package + pkgs.bash + pkgs.coreutils + pkgs.diffutils + pkgs.findutils + pkgs.gawk + pkgs.gitMinimal + pkgs.gnugrep + pkgs.gnused + pkgs.ripgrep + ]; + serviceConfig = { + Type = "forking"; + User = "claude"; + WorkingDirectory = "/home/claude/src"; + RuntimeDirectory = "claude"; + ExecStart = "${tmux} new-session -d -s claude ${lib.getExe claude-code} remote-control"; + Restart = "always"; + RestartSec = 10; + }; + }; + environment.systemPackages = [ claude-attach ]; +} -- cgit v1.3.1