aboutsummaryrefslogtreecommitdiffstats
path: root/configs/claude.nix
blob: 9d1221085cb094f4ab640fda9468dad419ee2a14 (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
# 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";

  # Root's way into claude's tmux.
  claude-attach = pkgs.writeShellScriptBin "claude-attach" ''
    exec ${tmux} attach -t claude
  '';
in
{
  imports = [ ../modules/claude-code.nix ];

  fc.home.required = true;

  users.users.claude = {
    isNormalUser = true;
    # Fixed, for the files on the home drive: users get created anew at
    # every boot, and their uids would depend on the other users there.
    uid = 1000;
    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";
    # 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
      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 pkgs.claude-code} remote-control";
      Restart = "always";
      RestartSec = 10;
    };
  };
  environment.systemPackages = [ claude-attach ];
}