summaryrefslogtreecommitdiffstats
path: root/configs/claude.nix
diff options
context:
space:
mode:
Diffstat (limited to 'configs/claude.nix')
-rw-r--r--configs/claude.nix83
1 files changed, 83 insertions, 0 deletions
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 ];
+}