devname
Stable local names for dev servers: nobody remembers ports
EN · FR
Every dev server on this machine answers at a stable name: hindsight.localhost, bench.sre.localhost, devname.localhostNobody has ever had to remember a port.
The idea
*.localhost resolves to loopback on any modern OS (RFC 6761), with zero DNS setup. devname rides that: one Caddy proxying names to ports, a tiny registry, and a CLI that makes the whole thing boring.
devname ensure [project] [--service S] [--branch auto]
ensure is the only verb an agent ever needs: it prints the URL if the name exists, otherwise it allocates a port (17000–17999), registers the name, and reloads Caddy. Idempotent, safe to run concurrently (the registry is flocked), safe to forget.
How it works
- A launchd agent runs Caddy on
:80/:443, bound to the wildcard (the only way an unprivileged process can bind a low port on macOS). - Every site starts with an abort guard: any connection whose source is not loopback dies at Caddy. The names are local-program-only, enforced in the config, not in discipline.
- The registry is a flat
names.json; the Caddyfile is generated from it, never hand-edited. mkcertissues one SAN certificate covering every registered name, so https works out of the box.
For agents
The design target was agents starting servers: never hard-code a port, never parse lsof output.
PORT=$(devname ensure myapp --format port)
./myapp --port "$PORT" # now at myapp.localhostBranch variants get their own names (feat-x--tests.myapp.localhost), so a worktree never collides with the main checkout.
Every knob
The source lives at git.nech.pl/pln/devname. The full surface of the main verb:
usage: devname ensure [-h] [--service SERVICE] [--branch BRANCH] [--port PORT]
[--force] [--keep-host] [--note NOTE]
[--format {url,port,json}]
[project]
positional arguments:
project
options:
-h, --help show this help message and exit
--service SERVICE
--branch BRANCH a branch name, or 'auto' = the cwd's git branch
(main/master get no suffix)
--port PORT adopt an existing server's port instead of allocating
--force with --port: remap an existing name
--keep-host do not rewrite Host to 127.0.0.1:<port> (default:
rewrite)
--note NOTE
--format {url,port,json}The index page
Caddy also serves devname.localhost itself: every registered name with live up/down status, probed per request. The registry's front door, built by the same tool that keeps it.
