summaryrefslogtreecommitdiffstats
path: root/configs/hermes.nix
blob: 66b1094341ef79f9302894b2e4141113abc034eb (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Hermes Agent (Nous Research), with its own NixOS module, fitted to this
# guest.  Put ANTHROPIC_API_KEY=... into hermes.env in /xchg on the host.
# hermes runs as hermes in a tmux, which root's console opens.
{ config, lib, pkgs, ... }:
let
  tmux = "${pkgs.tmux}/bin/tmux -S /run/hermes/tmux";

  # Bump url and sha256 together.
  src = builtins.fetchTarball {
    url = "https://github.com/NousResearch/hermes-agent/archive/9f47677483d1b4de4d1a88d37b3b7ca18b18b215.tar.gz";
    sha256 = "1xpvwgmn5slmfc2zsr5bfgywz1c6dqx3i5qy6ppbdvg1j2vngx71";
  };

  # hermes-agent's flake without flakes, like flake-compat, but with this
  # system's pkgs as nixpkgs, which all of the flake's inputs follow.  The
  # other inputs are fetched as locked in its flake.lock; home-manager is
  # left out, as only the flake's checks use it.
  callFlake = src: inputs:
    let
      self = { _type = "flake"; outPath = src; inherit inputs; }
        // (import (src + "/flake.nix")).outputs (inputs // { inherit self; });
    in
    self;
  input = name:
    let locked = (builtins.fromJSON (builtins.readFile (src + "/flake.lock"))).nodes.${name}.locked;
    in builtins.fetchTarball {
      url = "https://github.com/${locked.owner}/${locked.repo}/archive/${locked.rev}.tar.gz";
      sha256 = locked.narHash;
    };
  nixpkgs = {
    _type = "flake";
    outPath = pkgs.path;
    inherit (pkgs) lib;
    # For nixos-26.05, which hermes-agent doesn't target.
    legacyPackages.${pkgs.stdenv.hostPlatform.system} = pkgs.extend (_: prev: {
      # Its fetchPnpmDeps overrides nodejs-slim in a pnpm >= 11, but
      # hermes-agent's pnpm 12 is a plain attrset with that.
      fetchPnpmDeps = args: prev.fetchPnpmDeps (args // {
        pnpm = args.pnpm // { override = attrs: args.pnpm // attrs; };
      });
      # hermes-agent patches a bug of patchelf 0.19 that its 0.15 doesn't
      # have, and the patch doesn't apply to it.
      patchelf =
        if lib.versionOlder prev.patchelf.version "0.19"
        then prev.patchelf // { overrideAttrs = _: prev.patchelf; }
        else prev.patchelf;
    });
  };
  pyproject-nix = callFlake (input "pyproject-nix") { inherit nixpkgs; };
  uv2nix = callFlake (input "uv2nix") { inherit nixpkgs pyproject-nix; };
  hermes-agent = callFlake src {
    inherit nixpkgs pyproject-nix uv2nix;
    flake-parts = callFlake (input "flake-parts") { nixpkgs-lib = nixpkgs; };
    pyproject-build-systems = callFlake (input "pyproject-build-systems") { inherit nixpkgs pyproject-nix uv2nix; };
  };
in
{
  # Straight from the file, as the flake's nixosModules.default needs pkgs,
  # which imports can't use.
  imports = [ (import (src + "/nix/nixosModules.nix") { inputs.self = hermes-agent; }).flake.nixosModules.default ];

  services.hermes-agent = {
    enable = true;
    # Without perl, as profiles/perlless forbids it in the closure: git
    # without send-email & co., and no wl-clipboard, whose xdg-utils needs
    # perl, and which is no use here anyway.
    package = hermes-agent.packages.${pkgs.stdenv.hostPlatform.system}.default.override {
      git = pkgs.gitMinimal;
      wl-clipboard = pkgs.emptyDirectory;
    };
    # /var is a tmpfs.  This is on the only drive that can outlive a boot,
    # like nix's database, see /nix/var.
    stateDir = "/nix/.rw-store/hermes";
    settings.model = {
      provider = "anthropic";
      default = "claude-opus-5-5";
    };
    # Read at boot; a missing file only gets a warning.
    environmentFiles = [ "/xchg/hermes.env" ];
  };

  # hermes' home is stateDir, so this uses the service's state at ~/.hermes.
  # tmux lets root attach to it.  Quitting hermes ends the session, and
  # systemd starts a new one.
  systemd.services.hermes-tmux = {
    wantedBy = [ "multi-user.target" ];
    requires = [ "hermes-agent-setup.service" ];
    after = [ "hermes-agent-setup.service" ];
    before = [ "serial-getty@ttyS0.service" ];
    serviceConfig = {
      Type = "forking";
      User = "hermes";
      RuntimeDirectory = "hermes";
      ExecStart = "${tmux} new-session -d -s hermes ${lib.getExe config.services.hermes-agent.package}";
      Restart = "always";
    };
  };
  # Detaching gets root's shell back.
  programs.bash.loginShellInit = ''
    if [ "$USER" = root ] && [ -z "$TMUX" ]; then ${tmux} attach -t hermes; fi
  '';

  # The module adds pkgs.git, which needs perl; the package has gitMinimal.
  systemd.services.hermes-agent.path = lib.mkForce [
    config.services.hermes-agent.package
    pkgs.bash
    pkgs.coreutils
  ];

  # nixos-init doesn't run activation scripts, and that's where the module
  # creates the state and installs config.yaml and .env.
  systemd.services.hermes-agent-setup = {
    requiredBy = [ "hermes-agent.service" ];
    before = [ "hermes-agent.service" ];
    serviceConfig.Type = "oneshot";
    serviceConfig.RemainAfterExit = true;
    script = config.system.activationScripts.hermes-agent-setup.text;
  };

  # users.manageLingering is off here.  Without a user manager, hermes'
  # cron can't run jobs; nothing else needs one.
  users.users.hermes.linger = null;
}