naitokosuke · dotfiles

Adrafinil (cask in hosts/common/homebrew.nix) keeps the Mac awake only while an agent is working. Everything that runs its CLI is declared here: the Claude Code hooks, the MCP server entry, and the cask they depend on (#553).

Tied to the cask

One line names the cask and the reason; ./required-casks.nix turns it into an assertion, so dropping the cask fails evaluation instead of leaving the hooks silently doing nothing and the server entry pointing at nothing.

line 41
41  naitokosuke.requiredCasks.adrafinil = "home/adrafinil.nix runs the Adrafinil CLI";

Claude Code hooks

The app's one-click installer writes these hooks into ~/.claude/settings.json, which is a read-only store symlink here, so they are a hand-maintained copy of ClaudeCodeIntegration.swift at v1.7.0: the app self-updates past the flake.lock pin, so re-check upstream when it does. The commands must match upstream byte for byte — Adrafinil recognises its hooks by command and compares them verbatim, so Settings → Agents reports Claude Code as connected without the _adrafinil marker. UserPromptSubmit / Stop bracket each turn, the sub-agent pair outlives the parent turn, and the rest cover turn ends that fire no Stop. The module system merges them with the hooks ./claude.nix and ./claude-rm-guard declare.

lines 55–73
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73  programs.claude-code.settings.hooks = {
    UserPromptSubmit = hook { command = turn "acquire"; };
    Stop = hook { command = turn "release"; };
    # Esc-interrupt fires no Stop; this is a best-effort fast-path release.
    Notification = hook {
      command = turn "release";
      matcher = "idle_prompt";
    };
    SubagentStart = hook { command = subagent "acquire"; };
    SubagentStop = hook { command = subagent "release"; };
    # `/clear` and clear-context plan approval retire the session in-process
    # without a Stop: release the old id, and hold the new one whose plan
    # run skips UserPromptSubmit.
    SessionEnd = hook { command = turn "release"; };
    SessionStart = hook {
      command = turn "acquire";
      matcher = "clear";
    };
  };

MCP server

The keep_awake tool, so an agent can hold the Mac awake for work that outlives its turn (e.g. background shells), which the hooks do not cover. mcp-servers-nix has no module for it, so it goes through the freeform settings.servers. Same entry upstream's installer writes to ~/.claude.json. Adrafinil only inspects that file, so its Settings → Agents tab still reports the MCP server as not installed.

lines 82–89
82
83
84
85
86
87
88
89  mcp-servers.settings.servers.adrafinil = {
    command = cli;
    args = [
      "mcp"
      "--tool"
      "claude-code"
    ];
  };
View the complete file91 lines · adrafinil.nix
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# Adrafinil (cask in hosts/common/homebrew.nix) keeps the Mac awake only while
# an agent is working. Everything that runs its CLI is declared here: the
# Claude Code hooks, the MCP server entry, and the cask they depend on (#553).
{ lib, ... }:

let
  # Inside the app bundle, so it cannot come from the store.
  # /usr/local/bin/adrafinil is not used: the app only creates it
  # asynchronously at launch.
  cli = "/Applications/Adrafinil.app/Contents/Helpers/adrafinil";

  hook =
    {
      command,
      matcher ? null,
    }:
    [
      (
        {
          hooks = [
            {
              type = "command";
              inherit command;
            }
          ];
        }
        // lib.optionalAttrs (matcher != null) { inherit matcher; }
      )
    ];
  # stdin `session_id` wins over the env var, which is only the fallback.
  turn = op: "${cli} ${op} $CLAUDE_CODE_SESSION_ID --tool claude-code";
  # Keyed on the sub-agent's `agent_id` from stdin, so a backgrounded
  # sub-agent stays held after the parent turn's Stop.
  subagent = op: "${cli} ${op} --tool claude-code --subagent";
in
{
  ## Tied to the cask
  # One line names the cask and the reason; ./required-casks.nix turns it into
  # an assertion, so dropping the cask fails evaluation instead of leaving the
  # hooks silently doing nothing and the server entry pointing at nothing.
  naitokosuke.requiredCasks.adrafinil = "home/adrafinil.nix runs the Adrafinil CLI";

  ## Claude Code hooks
  # The app's one-click installer writes these hooks into
  # ~/.claude/settings.json, which is a read-only store symlink here, so they
  # are a hand-maintained copy of ClaudeCodeIntegration.swift at v1.7.0: the
  # app self-updates past the flake.lock pin, so re-check upstream when it
  # does. The commands must match upstream byte for byte — Adrafinil
  # recognises its hooks by command and compares them verbatim, so
  # Settings → Agents reports Claude Code as connected without the
  # `_adrafinil` marker. UserPromptSubmit / Stop bracket each turn, the
  # sub-agent pair outlives the parent turn, and the rest cover turn ends that
  # fire no Stop. The module system merges them with the hooks ./claude.nix
  # and ./claude-rm-guard declare.
  programs.claude-code.settings.hooks = {
    UserPromptSubmit = hook { command = turn "acquire"; };
    Stop = hook { command = turn "release"; };
    # Esc-interrupt fires no Stop; this is a best-effort fast-path release.
    Notification = hook {
      command = turn "release";
      matcher = "idle_prompt";
    };
    SubagentStart = hook { command = subagent "acquire"; };
    SubagentStop = hook { command = subagent "release"; };
    # `/clear` and clear-context plan approval retire the session in-process
    # without a Stop: release the old id, and hold the new one whose plan
    # run skips UserPromptSubmit.
    SessionEnd = hook { command = turn "release"; };
    SessionStart = hook {
      command = turn "acquire";
      matcher = "clear";
    };
  };

  ## MCP server
  # The `keep_awake` tool, so an agent can hold the Mac awake for work that
  # outlives its turn (e.g. background shells), which the hooks do not cover.
  # mcp-servers-nix has no module for it, so it goes through the freeform
  # `settings.servers`. Same entry upstream's installer writes to
  # ~/.claude.json. Adrafinil only inspects that file, so its Settings → Agents
  # tab still reports the MCP server as not installed.
  mcp-servers.settings.servers.adrafinil = {
    command = cli;
    args = [
      "mcp"
      "--tool"
      "claude-code"
    ];
  };
}
91 lines·3,587 chars·NIX