Connecting
Each folder you run nerdr in has its own nerdr, and its own API. It is there as long as that nerdr runs, with the window open or closed, until you quit.
With nerdr api
Section titled “With nerdr api”The simplest way is to start nerdr api from your program and talk to
it over its standard input and output:
nerdr api [FOLDER]FOLDER is the folder nerdr runs in, the current one when not given.
nerdr api finds the API socket and says hello with the key for you.
Every line after that is passed through unchanged.
- The first line out is the welcome:
{"api":1,"version":"v0.9.0","root":"/home/you/develop"}. - Each line your program writes is a request, and each line it reads is an answer to one, or an event once it has subscribed.
- When its input ends,
nerdr apianswers what it was sent and exits. If it subscribed, it keeps passing on events until it is stopped. - If no nerdr runs for the folder, or nerdr refuses the connection, it says why on standard error and exits with status 1.
nerdr api does not start nerdr. Start nerdr in the folder first; after
that, closing its window leaves the API running.
To find the folders nerdr runs in, ask nerdr list --json: each one’s
root is a FOLDER to give nerdr api.
[{"root":"/home/you/develop","running":true,"version":"v0.9.0","api":1, "tabs":3,"agents":4,"waiting":1,"questions":0}]From another machine
Section titled “From another machine”Run nerdr api over SSH. Nothing more is needed on either side:
ssh devbox nerdr api ~/developThis is also how to reach nerdr on a server from a program on your
laptop: start the program, have it run ssh, and read and write lines
as usual.
To reach several machines through one, such as the servers nerdr on
devbox connects to, see Several machines.
Straight to the socket
Section titled “Straight to the socket”A program can also connect to the socket itself, without nerdr api.
The socket and its key are in nerdr’s private socket folder:
| Folder | |
|---|---|
With XDG_RUNTIME_DIR set |
$XDG_RUNTIME_DIR/nerdr |
| Linux, without it | ~/.cache/nerdr/run |
| macOS, without it | ~/Library/Application Support/nerdr/run |
The socket is api-HASH.sock and the key is api-HASH.key. HASH is
the first 16 hexadecimal digits of the SHA-256 of the folder’s real path
(symbolic links resolved, no trailing slash). The key changes every time
nerdr’s core starts, so read it each time you connect.
The first line your program sends is the hello, with the key and the API version it was written for:
{"key":"5f0c…","api":1}nerdr answers with the welcome, or with {"error":"…"} and closes the
connection: for a wrong key, for an API version newer than its own, or
for a program it runs (see below).
In Python:
import hashlib, json, os, socket
def connect(folder): root = os.path.realpath(os.path.expanduser(folder)) run = os.path.join(os.environ.get("XDG_RUNTIME_DIR", ""), "nerdr") \ if os.environ.get("XDG_RUNTIME_DIR") else os.path.expanduser("~/.cache/nerdr/run") name = "api-" + hashlib.sha256(root.encode()).hexdigest()[:16] with open(os.path.join(run, name + ".key")) as f: key = f.read().strip() s = socket.socket(socket.AF_UNIX) s.connect(os.path.join(run, name + ".sock")) lines = s.makefile("rw") lines.write(json.dumps({"key": key, "api": 1}) + "\n") lines.flush() welcome = json.loads(lines.readline()) if "error" in welcome: raise RuntimeError(welcome["error"]) return linesOn macOS, replace ~/.cache/nerdr/run with
~/Library/Application Support/nerdr/run. Running nerdr api instead
saves you from this.
How it is guarded
Section titled “How it is guarded”The API can type into agents, answer nerdr’s questions and make tabs, so only you may use it. It is guarded the same way as the socket nerdr’s window connects to:
- Its folder is yours alone. The socket and the key live in a folder only your user can open.
- It needs the key. The key is in a file only you can read, and it changes every time nerdr’s core starts.
- nerdr’s own programs are refused. nerdr refuses any connection
from a program it runs, its agents included, whether they run in a
sandbox or not. An agent cannot use the API, even by running
nerdr api. - Sandboxes are checked. Before an agent in a sandbox starts, nerdr checks from inside the sandbox that the agent can neither connect to the API’s socket nor read its key, and it does not start the agent if it can (see what a sandbox must deny).