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
{ depot, pkgs, lib, ... }:

let
  goDeps = import ../go-deps.nix { inherit depot pkgs; };

  unwrapped = depot.nix.buildGo.program {
    name = "hans";
    srcs = [
      ./main.go
      ./agent.go
      ./convert.go
      ./anthropic.go
      ./anthropic_auth.go
      ./anthropic_wire.go
      ./opencode_prompt.go
      ./interactions.go
      ./mcp.go
      ./store.go
      ./serve.go
      ./textview.go
      ./nodeview.go
    ];
    embeds = [ ./templates ./opencode ];
    deps = [
      # modernc.org/sqlite — the session store. Pure Go, so no CGO and no
      # native SQLite in the closure.
      goDeps.modernc-sqlite
      goDeps.modernc-libc
      goDeps.modernc-libc.sys.types
      goDeps.golang-x-sys-unix
    ];
  };
in
pkgs.symlinkJoin {
  name = "hans";
  paths = [ unwrapped ];
  buildInputs = [ pkgs.makeWrapper ];

  postBuild = ''
    # hans shells out to three programs, and all three are wrong to leave to
    # the ambient PATH:
    #
    #   pass  — holds the Gemini API key; without it hans cannot start unless
    #           GEMINI_API_KEY is already set.
    #   gopls — the MCP server behind -mcp.
    #   go    — not called by hans, but gopls resolves it from PATH and forces
    #           GOTOOLCHAIN=local for that subprocess. If it finds a go older
    #           than the workspace's go directive, nothing type-checks and
    #           every symbol query answers "No symbols found", which reads as
    #           proof the code does not exist. Pinning both here means they
    #           come from one nixpkgs and cannot drift apart.
    #
    # `--suffix` rather than `--prefix`: a project pinning its own toolchain
    # should still win, since gopls has to match the code being analysed, not
    # this closure.
    wrapProgram $out/bin/hans \
      --suffix PATH : ${lib.makeBinPath [ pkgs.gopls pkgs.go pkgs.pass ]}
  '';

  meta = {
    description = "A REPL-style Gemini coding agent, with a text/llm session service";
    longDescription = ''
      A terminal REPL agent speaking directly to the Gemini API, with no SDK
      between the agent loop and the wire format so that provider-specific
      features (prompt caching, the Interactions API's server-side state) stay
      reachable.

      Conversations are stored as trees rather than transcripts: each turn is a
      node whose parent is the turn it followed, which is the shape the
      Interactions API's previous_interaction_id already has. A conversation can
      therefore be continued from any point in it, and two clients prompting the
      same node get siblings rather than a conflict.

      With -serve it exposes those sessions over HTTP as text/llm, so another
      language model can read a conversation and continue or branch it. There is
      no authentication; bind it to loopback and read the caveats in hans(1).

      With -mcp it runs `gopls mcp` as an MCP server and offers its go_* tools
      — semantic search, symbol references, diagnostics — alongside its own
      file reading.

      The Gemini API key comes from $GEMINI_API_KEY, or from pass(1) at
      internet/ai.google.dev/gemini/api-keys/interactive.
    '';
  };
}