Skip to content

Self-hosting

Run your own knot to keep your repositories on a server you control. The web app still shows them, and people can still open issues and pull requests against them. The knot is a single Rust binary, knot-server, with no database: git is the only store, plus a sealed key file. Run a knot is the short version of this page.

If you already run the Go knot, follow Golang to Rust knot migration guide instead.

  • A Linux machine that stays online.
  • A DNS name. It becomes the knot’s identity, did:web:<hostname>, so choose one you’ll keep.
  • Your DID, which becomes the first admin.
  • TLS, from a reverse proxy or from the knot itself.

The knot keeps its signing keys in a sealed file, encrypted with a master key from an environment variable. Make the key once:

printf 'KNOT_MASTER_KEY=%s\n' "$(openssl rand -base64 32)" > knot.env

Ports, Taylor’s version (ha ha, 22 joke)

Section titled “Ports, Taylor’s version (ha ha, 22 joke)”

A knot has two listeners where HTTP defaults to [::]:5555, SSH defaults to [::]:2222.

Thus far in Tangled in general, SSH is heavily used because the old Tangled knot implementation didn’t do HTTP pushing. Tangled generally auto-generates a URL for SSH git’ing that looks like this:

git clone git@knot.oyster.cafe:did:plc:barnacle

However notice that from Tangled’s client, it builds a clone URL with no port number in, because it always assumes 22. If the knot’s SSH listener is listening on another port, all of its users have gotta either rewrite the URL as ssh://git@knot.oyster.cafe:2222/did:plc:barnacle, or add a block to their ~/.ssh/config.

So give the knot port 22 if one can! It doesn’t need unix git user / shell account, so the only thing in the way is the remote computer’s own sshd, which is usually already taking port 22.

If the reader is nodding along instead of saying “no Lewis I won’t change the remote computer’s sshd because I like not bricking it” then:

Moving sshd is something that can lock one out of one’s own server, so do it in this order:

  1. Open a second SSH session to the server and keep it open for this whole procedure. If step 5 goes wrong, having this session open might be the saving grace.
  2. Find out which thing owns the port with systemctl is-enabled ssh.socket, since each answer sends you to a different file in step 3:
    • enabled: edit the socket unit. A Port line in sshd_config does nothing at all here, and editing it is the usual way to lose an afternoon. Ask me how I know.
    • disabled, or no systemd: edit Port in sshd_config; sshd holds the port itself.
  3. Edit /etc/ssh/sshd_config and set Port 2200 or whatever free port one likes. Leave Port 22 in place as well for now, so sshd listens on both. Under the socket unit, systemctl edit ssh.socket takes a [Socket] section with ListenStream=2200 instead, such that the new port joins whatever’s already listening.
  4. Open the new port in the firewall if there is one. On ufw that’s ufw allow 2200/tcp (I think!! Untested). On a cloud provider one will probably also have to deal with it / open it in their proprietary config.
  5. Restart sshd, or systemctl restart ssh.socket for the socket unit, and from the local computer, in a third terminal, confirm ssh -p 2200 root@the.server works before continuing.
  6. Once confirmed, only now remove Port 22 from sshd_config, restart sshd once more, & give the port to the knot. Under the socket unit that’s an empty ListenStream= above the ListenStream=2200, since a drop-in only adds to the port it inherits. When running the binary directly, that means ssh_listen_addr = "[::]:22". Comparatively, in a container it entails publishing the container’s 2222 as the host’s 22, which is what the compose file in Run a knot does.

If one would rather not move sshd at all, another cool option for having knot2 on port 22 is a second IP address on the remote computer. Bind sshd to one with ListenAddress, bind the knot to the other with ssh_listen_addr = "<second-ip>:22", and just put the knot’s DNS record on that second address. Leaving the knot on [::]:22 would wildcard-bind every address on the box and would collide with sshd no matter which single IP that sshd listens on.

HTTP can stay on 5555 behind a reverse proxy, or move to 443 if one wants the knot to terminate TLS itself.

knot-server config-template > config.toml
knot-server --config config.toml validate

The required keys are server.hostname, server.admins, server.ssh_host_key_file, repo.scan_path, secrets.sealed_key_file, secrets.master_key_env and atproto.plc_directory. Any key can also come from a KNOT_* environment variable.

{
imports = [ tangled.nixosModules.knot-rs ];
services.tangled.knot-rs = {
enable = true;
environmentFile = "/etc/knot/knot.env";
settings = {
server = {
hostname = "knot.example.com";
admins = [ "did:plc:yourdid" ];
};
atproto.plc_directory = "https://plc.directory";
lfs.store_path = "/var/lib/knot/lfs";
contact.email = "your@contact.email";
};
};
}

The knot listens on 127.0.0.1:5555 and keeps its state in /var/lib/knot.

Use atcr.io/tangled.org/knot:2. It runs as uid 1000, so chown your data directories to 1000:1000, and set KNOT_SCAN_PATH, KNOT_SEALED_KEY_FILE and KNOT_SSH_HOST_KEY_FILE to your mounted paths. Run a knot has a full compose file.

We show an example systemd/knot.service installable to /etc/systemd/system/ for running the binary straight on the machine, then systemctl enable --now knot.

It runs the knot as git (well remember this is just for fun, not because we have to have a git user mind you) from /usr/local/bin/knot-server, so edit User=, Group= and ExecStart= if one’s setup differs. ProtectSystem=strict and ReadWritePaths=/var/lib/knot such that anything the knot writes outside the tree gets its own ReadWritePaths= entry, an LFS store or an ACME cache on another disk being the usual suspects. ProtectHome=true has to go if any of the paths is under /home. The two CAP_NET_BIND_SERVICE lines let it bind a port below 1024, and both can go when SSH stays on 2222 and HTTP on 5555.

Behind a reverse proxy. Proxy to port 5555, and don’t add CORS headers at the proxy. Set xrpc.trusted_proxy_header = "x-forwarded-for" when doing this, otherwise every client looks like it comes from the proxy and the ratelimiter will treat them as one very busy mister. Only set it behind a proxy the operator controls, since a direct client can like, invent that header.

Add xrpc.trusted_proxies = ["fd00:1::4", "10.89.0.4"] for example, one entry per address that the proxy connects from, so the knot honors that header from the proxy alone & ratelimits anyone else by the address they connected from. A CIDR block will work too, ["173.245.48.0/20"] covers a whole provider’s edge. If several proxies you control are in the path, list them all. Mister knot will read the chain right -> left, iterate over every entry the list covers, and take the first entry it doesn’t. It will read 32 entries at most, and the knot will ratelimit by the address the request connected from when the list covers all 32.

The knot can also terminate TLS itself (and that’s the only way to get its HTTP3 support) because a plain TCP frontend can’t proxy QUIC. Using a certificate the operator already manages:

[server]
listen_addr = "[::]:443"
[tls]
cert_path = "/data/tls/fullchain.pem"
key_path = "/data/tls/privkey.pem"

Or let it fetch its own via ACME:

[server]
listen_addr = "[::]:443"
[tls]
acme_enabled = true
acme_cache_dir = "/data/acme"
acme_contact = "nel@oyster.cafe"

ACME here uses the TLS-ALPN-01 challenge, so the knot has to be the receiver (in the phone sense) ponging on 443 for the configured hostname. Set acme_staging = true while testing such that a typo doesn’t nuke the Let’s Encrypt ratelimit. Leave trusted_proxy_header unset in this mode, and open 443/udp in the firewall if one wants HTTP3 to be reachable.

curl https://knot.example.com/xrpc/_health

Then add the knot under Settings → Knots in the web app.

By default only admins can create repositories. Set acl.admission = "open" to let anyone in, or add members with the admin commands. These need acl.legacy_admin_secret_env set:

knot-server --config config.toml admin member add did:plc:friend
knot-server --config config.toml admin create-invite-code

We use the now-required lfs.store_path to have the knot save LFS objects, atproto record attachments (from issues and pulls, say) and soon LOPs objects too. Suggestion: choose a directory that’s alongside the repos folder, like /var/lib/knot/lfs beside /var/lib/knot/repos for example. If you don’t plan to have actual git repos using this for massive files, then it’ll be fine on the same disk as the git repos; if you do plan to have massive files, consider putting the LFS folder on a different disk entirely like an HDD. Wherever you put the folder, keep it out of the repo.scan_path.

Back these things up! It’s of the utmost importance.

  1. The master key, wherever one keeps it.
  2. secrets/sealed.bin, the sealed key store. It’s useless without the master key & the master key is useless without it, so treat them as a pair.
  3. repos/, which is every repo plus the knot’s own ACL data. Git is the database after all, so that one directory is the knot’s entire state.
  4. lfs/, the LFS/LOPs/ atproto record attachments.
  5. I guess the SSH host key, though much less dire if it is lost and has to be changed.

Stop the knot, replace the binary or image, and start it again. Read Upgrading knots and spindles first.

Pre-built images are at atcr.io/tangled.org/knot:latest, with knot-server and knot-migrate in them. The image comes from @tangled.org/knot-docker instead of the knot2/Containerfile in this repo.

It runs as its own knot user at uid 1000, so chown the data directories to 1000:1000 before the first start.

The knot drains at SIGTERM time, so in-flight clones/pushes get up to 40s to finish before they’re cut off.

  • The knot won’t start: run knot-server validate with the same config and environment.
  • A push is rejected for an SSH key: publish the key on the user’s PDS, then wait up to an hour for the knot’s cache.
  • Every user is rate-limited together: set xrpc.trusted_proxy_header and xrpc.trusted_proxies.

Knot Configuration lists every setting.