AT Protocol Application Framework — Rust Development Environment
README.md

AT Protocol Rust Quickstart #

A batteries-included, federation-ready template for building applications on the AT Protocol with Rust — clone, rename, run, done.

This repo gives you a complete local ATProto development stack in one command, with a full Rust toolchain, PostgreSQL, Redis, and a local PDS (tranquil-pds), all wired together and orchestrated for you.


🚀 30-Second Quickstart #

git clone <this-repo-url> my-app && cd my-app
nix run .#init -- my_app   # rename the template (writes ./project.nix)
nix develop                # enter the reproducible dev shell
dev-start                  # boots postgres + redis + tranquil-pds
dev-health                 # verify everything is up

That's it. You now have a running local ATProto stack. Go build.

No scripts, no sed, no shell magic. Every command, env var, database name, and banner is a Nix derivation built from a single config value. See § How the Nix wiring works.


📋 Prerequisites #

You need exactly one thing: Nix with flakes enabled.

# Linux / macOS — one-liner
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install

# Then in your shell config (one-time):
# (the Determinate installer does this for you automatically)

Why only Nix? Every other dependency — Rust, PostgreSQL, Redis, tranquil-pds, process-compose, websocat, jq, everything — is declared in flake.nix and pinned in flake.lock. You don't install them. Nix does.

No Docker. No brew install marathon. No "works on my machine."


🧭 Step-by-Step: From Zero to Running #

1. Clone the repo #

git clone <this-repo-url> my-awesome-app
cd my-awesome-app

Pick whatever folder name you like — it has no effect on the project itself.

2. Run the initializer #

# Interactive — prompts you for a name:
nix run .#init

# Or non-interactive:
nix run .#init -- my_awesome_app

# Overwrite an existing project.nix:
nix run .#init -- my_awesome_app --force

The init app writes a tiny project.nix file at the repo root:

{
  name = "my_awesome_app";
}

That's it. There is no sed, no string rewriting, and no file mutation anywhere in the template. Every downstream value — the app database name, the PDS database name, the RUST_LOG target prefix, the dev-shell banner, the DATABASE_URL env, the process-compose.yaml — is derived from that single value at Nix evaluation time (see nix/commands.nix).

The initializer also runs git add --intent-to-add --force project.nix for you, because Nix flakes only evaluate files that git knows about. The file stays gitignored for commit purposes — each collaborator can keep their own local name — but the flake can still read it.

Naming rules (enforced by the script):

  • lowercase letters, digits, and underscores only
  • must start with a letter
  • max 40 characters
  • no reserved names (postgres, redis, pds, test, etc.)

Good examples: bsky_notes, federated_chat, my_app, atproto_wiki.

Permanent rename? If you want the rename baked in for everyone who clones the repo, either (a) edit nix/config.nix directly and commit, or (b) remove project.nix from .gitignore and commit it.

3. Enter the dev shell #

nix develop

The first run downloads Nix packages (≈ 5–10 min on a cold cache). Subsequent runs are instant.

You'll be dropped into a shell with:

  • The exact Rust toolchain pinned for this project
  • postgresql_16, redis, tranquil-pds, process-compose on your $PATH
  • ATProto dev tools: websocat, cbor-diag, check-jsonschema
  • A pile of convenience commands (see the banner that prints on entry)

4. Start the services #

dev-start

This boots, in the correct order:

Service Port What it does
PostgreSQL 16 unix socket Stores your app + PDS data
Redis 6379 L2 cache
tranquil-pds 3000 Local ATProto Personal Data Server

All data lives in ./.dev/ (gitignored). Nothing escapes your project directory.

5. Verify it's healthy #

dev-health            # human-readable
dev-health-json       # machine-readable (for CI)
dev-status            # list process states
dev-logs              # tail all logs
dev-logs tranquil-pds # tail one service

If any service is red, dev-reset && dev-start almost always fixes it.

6. Kick the tyres #

# Ask the PDS who it is
pds-describe | jq .

# Create a test account (handle, email, password)
pds-create-account alice.test alice@test.local pass123

# Get session tokens
pds-login alice.test pass123

# Open a psql shell against your app database
db

🏗️ How the Nix wiring works #

flake.nix                 # tiny top-level: inputs + devShell glue
├── nix/
│   ├── config.nix        # defaults (name = "app", ports, relay, …)
│   └── commands.nix      # every dev command as a writeShellApplication
└── project.nix           # OPTIONAL local override (gitignored)
                          # created by `nix run .#init`
  • nix/config.nix is the single source of truth. Change a value there (or in project.nix) and everything downstream rebuilds: env vars, process-compose YAML, banners, psql targets, log prefixes.
  • nix/commands.nix produces every former scripts/*.sh as a first-class writeShellApplication derivation. Each one declares its runtime dependencies (curl, jq, psql, etc.) so it cannot silently break if your host system is missing a tool. Every script is run through shellcheck at build time — typos and quoting bugs fail nix flake check, not production.
  • Every command is independently addressable:
    nix build .#dev-health       # build a single command
    nix run   .#pds-describe     # run it without entering the shell
    nix flake check              # shellcheck every command
    
  • Zero file rewriting ever happens. The template has no placeholder-replacement step — interpolation happens at Nix eval time, so git diff after nix run .#init shows exactly one three-line file added.

🛠️ Command Cheatsheet #

Everything below is available inside nix develop, and every one is also runnable directly via nix run .#<name>.

Lifecycle #

Command What it does
dev-start Start postgres, redis, tranquil-pds
dev-stop Stop all services
dev-status List running processes
dev-logs [name] Tail logs (all, or one service)
dev-reset Wipe .dev/ and start clean
dev-health Health check (human output)
dev-health-json Health check (JSON, for CI)

Databases #

Command What it does
db psql into your app database
db-pds psql into the PDS database

ATProto tooling #

Command What it does
pds-describe Show PDS server info
pds-create-account Create a test account on the local PDS
pds-login Obtain session tokens
firehose-tap [url] Stream the firehose over WebSocket
did-resolve <did> Resolve a DID to its document
lexicon-validate Validate a record against a lexicon schema
cbor-inspect <file> Decode CBOR / DAG-CBOR

🌱 Environment Variables #

These are exported automatically when you enter nix develop (see scripts/shellhook.sh):

DATABASE_URL           # postgres connection to YOUR app DB
PDS_DATABASE_URL       # postgres connection to the PDS DB
REDIS_URL              # redis://localhost:6379
PDS_URL                # http://localhost:3000
PDS_HOSTNAME           # localhost
ATPROTO_RELAY_ENDPOINTS # wss://bsky.network (change for a private relay)
PLC_DIRECTORY_URL      # https://plc.directory
RUST_LOG               # sensible per-crate defaults
RUST_BACKTRACE         # 1
PC_SOCKET_PATH         # per-project process-compose socket

The PC_SOCKET_PATH being per-project means you can run multiple quickstart projects in parallel without them fighting over the same process-compose socket.


📁 Project Layout #

.
├── flake.nix              # Top-level flake: inputs, devShell, packages, apps, checks
├── flake.lock             # Pinned inputs — reproducibility
├── nix/
│   ├── config.nix         # Default project config (name, ports, relay URLs, …)
│   └── commands.nix       # Every dev command as a Nix derivation
├── project.nix            # OPTIONAL per-checkout override (gitignored)
│                          # created by `nix run .#init`
├── .dev/                  # Runtime state (gitignored)
│   ├── postgres/          # DB cluster
│   ├── redis/             # Redis state
│   ├── pds/blobs/         # PDS blob storage
│   └── process-compose.yaml  # Generated from nix/commands.nix
├── CLAUDE.md              # Technical architecture reference
├── humandocs.md           # Conceptual / onboarding guide
└── README.md              # ← you are here

There is no scripts/ directory and no init.sh file — they've been absorbed into nix/commands.nix. If you used an earlier revision that had them, rm -rf scripts init.sh is safe.


🔧 Customization After Init #

Once you've run nix run .#init, you're free to change anything. Common next steps:

  1. Add your own Rust crate: cargo new --lib . in the repo root, then extend flake.nix if you need extra system deps.
  2. Define your lexicons: drop JSON Schema files under ./lexicons/com/your/app/ and validate with lexicon-validate.
  3. Point at a private relay: edit relayEndpoints in nix/config.nix (or in project.nix). The shell-hook picks it up on the next nix develop.
  4. Change a port: edit pdsPort / redisPort / pgPort in nix/config.nix. The process-compose.yaml, readiness probes, and env vars all rebuild from that.
  5. Add a new dev command: drop a new writeShellApplication entry into nix/commands.nix — it automatically becomes available in the shell, as nix run .#<name>, and gets shellchecked on nix flake check.
  6. Persist secrets: the PDS dev secrets in nix/commands.nix are literally named dev-*. Do not ship them. For production, wire them through sops-nix or your secret manager of choice.

For implementation-level guidance (auth, firehose, caching, rate limiting), see CLAUDE.md. For the conceptual ATProto primer, see humandocs.md.


🆘 Troubleshooting #

Symptom Fix
dev-health says postgres is down dev-reset && dev-start
tranquil-pds won't start dev-logs tranquil-pds — usually a DB migration
pds-create-account returns 500 PDS not ready yet; wait for dev-health to pass
Two shells fight over services They don't — each project has its own PC socket
nix develop fails on first run Enable flakes: see prerequisites
Want to rename again nix run .#init -- new_name --force
Rename didn't take effect git add -fN project.nix then re-enter nix develop
Want a clean slate dev-stop && rm -rf .dev && dev-start
Want to reset to default name app rm project.nix and re-enter nix develop
nix flake check fails on shellcheck Fix the reported line in nix/commands.nix

🌍 Portability Notes #

This template is tested on:

  • Linux (x86_64, aarch64) — uses mold linker for fast Rust builds
  • macOS (Intel + Apple Silicon) — libiconv wired in automatically
  • WSL2 — works exactly like Linux

Windows (native) is not supported. Use WSL2.

Because the entire template is Nix-native, portability quirks (GNU vs BSD sed, missing jq, wrong psql version, etc.) simply cannot happen — every command is a derivation that builds its own closed-world dependency set.


📜 License #

See LICENSE.


🧠 Where to go next #

  • New to AT Protocol? → humandocs.md
  • Building for real? → CLAUDE.md (architecture, threat model, perf contracts)
  • Stuck? → dev-reset fixes 90% of local issues. The other 10% live in dev-logs.

Happy federating. 🌐