aboutsummaryrefslogtreecommitdiffstats
path: root/configs/claude.nix
blob: f29ea2f158c7c1cabeeda5803093bea0799cc192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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.
{ 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";

  fc.home.required = true;

  users.users.claude = {
    isNormalUser = true;
    home = "/home/claude";
    # /home is mounted after users get created, see tmpfiles below.
    createHome = false;
    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 -"
  ];

  # 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
    ];
    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";
    };
    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 ];
}