···3873872. A (sub)domain name. People generally use `knot.example.com`.
3883883. A valid SSL certificate for your domain.
389389390390+Already running a knot and want to move it to knot 2?
391391+See [Migrating to knot 2](#migrating-to-knot-2).
392392+390393## NixOS
391394392395Refer to the [knot
···721724> **Note:** the server will refuse to start in Secure Mode if any
722725> repositories have not yet been isolation-migrated. Re-run
723726> `migrate-isolation` if you see this error.
727727+728728+## Migrating to Knot 2
729729+730730+Knot 2 is a second implementation of the knot concept.
731731+It serves the same repositories under the same hostname and the same `did:web`,
732732+and every clone URL and every `at://` reference will keep on working.
733733+You'll move to it by running one offline tool while the knot is stopped.
734734+Upgrading the old knot won't do a magic upgrade to knot 2 for you.
735735+This is intentional; we don't want to give anyone a nasty surprise
736736+if we'd switch out implementations under you or accidentally
737737+brick someone's janky-but-working setup by attempting to shift
738738+a jenga block underneath them.
739739+740740+Here's what a switchover involves:
741741+742742+- **Rehearsals:** try as many as you like, with the old knot still serving.
743743+- **Downtime:** from the `systemctl disable` in step 1 to `listening on` in step 5,
744744+ mostly the copying, depending on how large your repositories are and how fast the disk is.
745745+- **Parity:** the same knot on the same hostname,
746746+ with the same owners, members, & collaborators.
747747+- **Untouched stuff for reversion:** the old database,
748748+ and under the default-copy-mode, the old repositories too.
749749+750750+What do you get for upgrading?
751751+752752+- Knot 2 doesn't have a SQLite database. Git is the only database,
753753+ so your members, collaborators, & repository owners will be stored in git itself.
754754+- Knot 2 will serve SSH in-process, so you no longer need `sshd`,
755755+ the `AuthorizedKeysCommand`, or the unix `git` account.
756756+- Knot 2 will read SSH keys live from each user's PDS.
757757+- Repositories created from now on will stay SHA-1, matching what you already have.
758758+ Pass `--object-format sha256` to the migration if you'd rather modernize.
759759+760760+The repo directories will come across whole, hooks and commit-graphs included.
761761+Knot 2 doesn't do repo hooks,
762762+and it will rewrite commit-graphs on its own maintenance schedule.
763763+764764+### Before we begin
765765+766766+Get tools handy! Install both binaries:
767767+768768+```
769769+git clone https://tangled.org/did:plc:j5hmlfdrwkvtxm7cjmu7j2is
770770+cd core
771771+nix build .#knot-rs && sudo install -m755 result/bin/knot-server /usr/local/bin/
772772+nix build .#knot-migrate && sudo install -m755 result/bin/knot-migrate /usr/local/bin/
773773+```
774774+775775+On NixOS, the `knot-rs` module will write the config file, the service unit,
776776+& the state directory for you,
777777+so read [Migrating on NixOS](#migrating-on-nixos) alongside this guide.
778778+Run `./result/bin/knot-migrate` straight out of `nix build` instead of installing it.
779779+The knot never runs the migrator, so the module leaves it off the system entirely.
780780+781781+Make the dir that knot 2 will use, owned by the same user that the old knot runs as,
782782+then write a master key for unsealing the per-repository signing keys:
783783+784784+```
785785+sudo install -d -o git -g git /var/lib/knot
786786+sudo install -d -m 700 /etc/knot
787787+printf 'KNOT_MASTER_KEY=%s\n' "$(openssl rand -base64 32)" | sudo tee /etc/knot/knot.env > /dev/null
788788+sudo chmod 600 /etc/knot/knot.env
789789+```
790790+791791+That writes `/etc/knot/knot.env`, readable only by root:
792792+793793+```
794794+KNOT_MASTER_KEY=Xm9x2t2m2WNSVJ+9v0Cq0ftFhwEuLW/8x0aeCcaHiWM=
795795+```
796796+797797+**Back it up somewhere off the server.**
798798+Seriously.
799799+Seriously seriously.
800800+The sealed key store is useless without it,
801801+and the keys that it seals will sign every record that the migration writes.
802802+803803+### Dress rehearsal, with old knot still serving requests
804804+805805+`knot-migrate` will open its source database read-only,
806806+so you can rehearse as often as you like without stopping anything.
807807+Run it as root, since it'll read your `sshd` host key and the master key alongside the database:
808808+809809+```
810810+sudo sh -c 'set -a; . /etc/knot/knot.env; set +a; exec knot-migrate \
811811+ --source-db /home/git/knotserver.db \
812812+ --env-file /home/git/.knot.env \
813813+ --host-key /etc/ssh/ssh_host_ed25519_key \
814814+ --plc-url https://plc.directory \
815815+ --target /var/lib/knot \
816816+ --dry-run'
817817+```
818818+819819+The `--env-file` gives your hostname and repository scan path,
820820+so you don't have to repeat them.
821821+`--dry-run` won't touch the target,
822822+though SQLite will still add `knotserver.db-shm` and `knotserver.db-wal`
823823+beside a write-ahead-log database even to read it.
824824+Rehearse until the exit code is 0 and the last block of the report reads
825825+`scan path: writable`, `host key algorithm: ssh-ed25519`,
826826+and `master key: KNOT_MASTER_KEY decodes to a usable key`.
827827+Any line in the block with an error in it will stop the switchover.
828828+829829+Mount each host directory at the same location inside the container if you'd rather run it that way,
830830+such that every path in the command is still a host path.
831831+Mount the target too, even though the rehearsal won't write there,
832832+since it will measure the free space and the write permission on the target.
833833+The `atcr.io` image runs as its own unprivileged `knot` user,
834834+so pass `--user 0:0` to read the host key.
835835+An image that you built yourself from `knot2/Containerfile` is distroless,
836836+with its binaries in `/usr/local/bin` instead:
837837+838838+```
839839+sudo sh -c 'set -a; . /etc/knot/knot.env; set +a; exec docker run --rm \
840840+ --user 0:0 -v /home/git:/home/git -v /etc/ssh:/etc/ssh:ro \
841841+ -v /var/lib/knot:/var/lib/knot -e KNOT_MASTER_KEY \
842842+ --entrypoint /usr/bin/knot-migrate atcr.io/tangled.org/knot:2 \
843843+ <the same flags as above>'
844844+```
845845+846846+The `:ro` on `/home/git` will work only while the old knot is still running.
847847+Mount it writably from step 1 onward, rehearsals included,
848848+even though the migration only ever reads your database.
849849+SQLite will create an index file beside a write-ahead-log database to read it at all.
850850+Under a `:ro` mount, with the knot stopped and its log checkpointed, it can't,
851851+and `knot-migrate` errors out with `unable to open database file`.
852852+`--consume-source` will move the repositories out of it, so mount it writably for that too.
853853+854854+### Reading the report
855855+856856+Then read what the report says!
857857+858858+```
859859+knot owner: did:plc:akshay
860860+members to grant: 3
861861+repos to adopt: 5
862862+collaborator grants: 4
863863+864864+casbin cross-check drift:
865865+acl-only collaborator grants unioned in: 1
866866+did:plc:squid <- did:plc:boltless
867867+table-only collaborator grants missing from acl: 1
868868+did:plc:octopus <- did:plc:dawn
869869+repos with no acl owner marker where the owner regains push: 1
870870+did:plc:scallop
871871+872872+skipped repos: 2
873873+did:plc:conch unrepresentable name "Test knot"
874874+drops collaborator grant for did:plc:boltless
875875+did:plc:tuna no git repository at the source path
876876+877877+transfer mode: copy
878878+the filesystem checks used /var/lib/knot, since the scan path doesn't exist yet
879879+scan path: writable
880880+room to copy: 89.1GiB free is enough for the 580.0KiB that adoption will copy
881881+host key algorithm: ssh-ed25519
882882+master key: KNOT_MASTER_KEY decodes to a usable key
883883+884884+we left the target alone. Re-run without --dry-run to migrate.
885885+```
886886+887887+At the top is what's coming across,
888888+`repos to adopt`, `members to grant`, and `collaborator grants`,
889889+and they should match what you think you have.
890890+Under them is every repository that `knot-migrate` will skip, each with its reason.
891891+The last block is the preflight on the target,
892892+where `transfer mode: copy` and `room to copy: ... is enough`
893893+join the three lines you rehearsed against.
894894+895895+**Skips.** If `repos to adopt` is lower than you expect, the skip list is where the rest went:
896896+a name with a slash, a backslash, whitespace, a control character, `..`,
897897+a bare `.`, or more than 100 bytes, and a record key with anything outside letters,
898898+digits and `.-_:~`. Rename those on tangled.org and rehearse again, or accept losing them.
899899+900900+**Owner drift.** `knot-migrate` will refuse the whole switchover
901901+when the `acl` table and `repo_keys` are recorded with different owners for a repository.
902902+The drift section prints both DIDs, one repository per line, as `<repo> acl <did>, repo_keys <did>`.
903903+Settle each repository in the old knot's database,
904904+either by deleting the `acl` rows for the wrong owner or by correcting `repo_keys.owner_did`,
905905+then rehearse again.
906906+An extra `acl` owner marker beside the owner in `repo_keys` won't stop anything,
907907+since `knot-migrate` reports it and discards it.
908908+909909+**Room on disk.** Every disk measurement happens at `<target>/repos`,
910910+where your repositories end up.
911911+Point `--target` at a directory whose parent already exists,
912912+and mount or create the disk you mean to fill before you rehearse.
913913+A rehearsal against your root filesystem measures your root filesystem.
914914+Under `copy`, the default, your existing repositories stay where they are,
915915+and `room to copy` counts a second copy of every repository it adopts.
916916+`--consume-source` will move them instead, in seconds, with no extra room on disk,
917917+and only where source and target are on one filesystem
918918+and this process can write the old scan path.
919919+Once they've moved, only a filesystem snapshot taken beforehand will bring them back,
920920+even from a switchover that stopped halfway.
921921+`knot-migrate` will refuse before it writes anything,
922922+so your repos never end up split across two trees.
923923+924924+> 🦪 **Note:** if you ran Secure Mode, each repo tree has a per-owner uid,
925925+> so any one owner can read only their own. Root can read all of them.
926926+> `knot-migrate` will refuse outright when it can't read a source path,
927927+> and every repository is in the skip list with the error behind it.
928928+> It will rehearse everything else first either way,
929929+> and report that you need root alongside whatever else is outstanding.
930930+> It skips a path that isn't there at all, as usual.
931931+> `--skip-unreadable` will migrate the rest and leave those repositories on the old knot,
932932+> which is what you want when root can't read them either,
933933+> as with a dead disk or a stale network mount.
934934+> `--consume-source` will empty the old scan path of every repository that it could read,
935935+> so running it alongside `--skip-unreadable` leaves only the unreadable repositories on the old knot.
936936+937937+### Doing the thing
938938+939939+**Step 1.** Stop the knot and disable it,
940940+so SQLite checkpoints its WAL and no restart of the machine can take port 5555 back from knot 2.
941941+Leave it installed though, for "Going back":
942942+943943+```
944944+sudo systemctl disable --now knotserver
945945+```
946946+947947+**Step 2.** Copy database through a read-only connection, so the log stays untouched,
948948+and keep the copy until the switchover is settled.
949949+`knot-migrate` will never write to the source database.
950950+The copy is there for everything else that can go wrong on the day,
951951+and "Going back" has a restore-command:
952952+953953+```
954954+sudo sqlite3 "file:/home/git/knotserver.db?mode=ro" \
955955+ ".backup /var/tmp/knotserver-cutover.db"
956956+```
957957+958958+**Step 3.** Run the same command that you rehearsed, without `--dry-run`,
959959+then give the tree to the user that the service runs as, since root wrote all of it:
960960+961961+```
962962+sudo chown -R git:git /var/lib/knot
963963+```
964964+965965+`knot-migrate` will write your repositories, a `config.toml`, the imported host key,
966966+the sealed key store, and `repo-signing-keys.json`.
967967+That last file is every repository's private key in the clear,
968968+the same keys that your old database stored,
969969+so move it somewhere safe and off the server once the migration is done.
970970+Towards the end of the copy, install
971971+[`knot.service`](https://tangled.org/did:plc:j5hmlfdrwkvtxm7cjmu7j2is/blob/master/systemd/knot.service)
972972+to `/etc/systemd/system/`, so step 5 is only a start.
973973+974974+**Step 4.** Give port 22 to the knot,
975975+if you want SSH clone URLs to keep working without a port number in them.
976976+The order to do that in is under "Ports, Taylor's version" in the [knot 2 README](https://tangled.org/did:plc:j5hmlfdrwkvtxm7cjmu7j2is/blob/master/knot2/README.md),
977977+since getting it wrong will lock you out of your own server.
978978+Two moves there are migration-only:
979979+move `/etc/ssh/sshd_config.d/authorized_keys_command.conf` outside `sshd_config.d`,
980980+keeping it for going back,
981981+and uncomment `ssh_listen_addr` in `/var/lib/knot/config.toml` to set it to `"[::]:22"`.
982982+983983+**Step 5.** Start it and follow the log, watching the index rebuild from the meta-repo,
984984+until `index ready` and then `listening on`:
985985+986986+```
987987+sudo systemctl enable --now knot
988988+sudo journalctl -fu knot
989989+```
990990+991991+The knot rebuilds the index before it takes a connection.
992992+The README's systemd section covers editing the unit for a different user,
993993+a different binary path, or an LFS store outside `/var/lib/knot`.
994994+995995+Both knots serve HTTP on port 5555 by default,
996996+so you don't have to touch a reverse proxy already aimed at the old knot.
997997+Configure the proxy in the knot anyway.
998998+The knot will rate-limit by the address that a request arrives from,
999999+and every one of your users arrives from the proxy until you do.
10001000+In `/var/lib/knot/config.toml`,
10011001+set `xrpc.trusted_proxy_header` to the header that your proxy appends,
10021002+and `xrpc.trusted_proxies` to the address that it connects from.
10031003+The README's TLS section spells both out at length.
10041004+An empty `trusted_proxies` leaves the knot honoring the header from every address,
10051005+so fill it in whenever anything but your proxy can connect to port 5555,
10061006+and keep 5555 off the public internet either way.
10071007+10081008+### Migrating on NixOS
10091009+10101010+> 🦪
10111011+>
10121012+> NixOS is not my forte. Please read this and absolutely second-guess me.
10131013+> I have of course tested this, but still, a boy gets paranoid.
10141014+10151015+Steps 1 and 2 read the same, and the `knot-rs` module replaces the rest,
10161016+so configure it as the [knot 2 README](https://tangled.org/did:plc:j5hmlfdrwkvtxm7cjmu7j2is/blob/master/knot2/README.md) describes.
10171017+Set `settings.git.object_format` to whatever you passed `--object-format`,
10181018+since the module defaults to `sha256` where the migration defaults to `sha1`.
10191019+10201020+Run the migration before your first rebuild.
10211021+A knot that starts on an empty state directory won't serve any repositories,
10221022+and it will generate a host key of its own,
10231023+which `knot-migrate` will then refuse to overwrite.
10241024+A rebuild starts the knot the moment the module is on,
10251025+so if you want the module in place before you migrate, add this line first:
10261026+10271027+```
10281028+systemd.services.knot-rs.wantedBy = lib.mkForce [];
10291029+```
10301030+10311031+The knot then stays stopped until you start it by hand.
10321032+Migrate, `chown`, run `systemctl start knot-rs`, and delete the line at your next rebuild.
10331033+10341034+If the knot already started once, `--force-host-key` writes the imported key over the one it generated.
10351035+The fingerprint everybody cached under the old knot is the one that comes back,
10361036+so it only surprises whoever connected while knot 2 was serving a key of its own.
10371037+The `config.toml` that `knot-migrate` writes is only a record of what it chose,
10381038+because the file the knot reads is the module's.
10391039+10401040+Then run the migration and hand the tree to the module's user,
10411041+`knot`, where the Go module used `git`, and rebuild.
10421042+The `--source-repos` and `--hostname` flags replace the `--env-file` above,
10431043+since the Go module configures the old knot through systemd and will never write an env file.
10441044+Append `--dry-run` to rehearse it first, as often as you like:
10451045+10461046+```
10471047+nix build .#knot-migrate
10481048+sudo sh -c 'set -a; . /etc/knot/knot.env; set +a; exec ./result/bin/knot-migrate \
10491049+ --source-db /home/git/knotserver.db \
10501050+ --source-repos /home/git \
10511051+ --hostname knot.example.com \
10521052+ --host-key /etc/ssh/ssh_host_ed25519_key \
10531053+ --plc-url https://plc.directory \
10541054+ --target /var/lib/knot'
10551055+sudo chown -R knot:knot /var/lib/knot
10561056+sudo nixos-rebuild switch
10571057+```
10581058+10591059+The source paths above are the Go module's defaults,
10601060+where `stateDir` is `/home/git` and `repo.scanPath` is the state directory itself,
10611061+so read your own out of `services.tangled.knot` if you moved either.
10621062+"Check it worked" and everything after it apply unchanged,
10631063+with `systemctl status knot-rs` in place of `knot`.
10641064+10651065+### Checking that it worked
10661066+10671067+```
10681068+curl -s https://knot.example.com/xrpc/_health
10691069+curl -s https://knot.example.com/xrpc/sh.tangled.owner
10701070+ssh-keyscan -t ed25519 your.knot.com
10711071+```
10721072+10731073+The health endpoint should answer `{"version":"knot 2.0.0"}`,
10741074+`sh.tangled.owner` should answer with your own DID,
10751075+and the keyscan should match the fingerprint that your users already have.
10761076+Then clone a repository anonymously, and push to a repository that you own.
10771077+10781078+> 🦪
10791079+>
10801080+> knot 2 will take SSH keys from each user's PDS on an hour's lease,
10811081+> where the old knot kept a copy that could outlive the record.
10821082+> So when someone's push stops working: is their key record still in their PDS?
10831083+> Have they re-added the key in their Tangled settings, which writes it back?
10841084+> Have they waited out the lease since?
10851085+> Lower `keyfill.ttl_secs` if an hour is too long for you.
10861086+10871087+### Going back in case of emergency
10881088+10891089+If you ran the migration in its default copy mode,
10901090+your old data is exactly where it was.
10911091+Put `sshd` back on 22, move `authorized_keys_command.conf` back into `sshd_config.d`,
10921092+and start the old knot:
10931093+10941094+```
10951095+sudo systemctl disable --now knot
10961096+sudo systemctl enable --now knotserver
10971097+```
10981098+10991099+Restore `/var/tmp/knotserver-cutover.db` over `/home/git/knotserver.db` only if something wrote to the original,
11001100+which the migration itself never will.
11011101+If you used `--consume-source`, your repositories moved instead of being copied,
11021102+and only a filesystem snapshot taken before the migration
11031103+or your own little dir-reverse script would bring repos back.
72411047251105## Troubleshooting
7261106
···20202121Have fun!
22222323+Already running the Go knot and want this instead?
2424+Stop it once and point `knot-migrate` at its database.
2525+Afterwards the knot will serve the same hostname with the same repos,
2626+owners, members and collaborators.
2727+Upgrading the Go knot won't do a magic upgrade for you.
2828+The walkthrough is in [the docs](https://tangled.org/did:plc:j5hmlfdrwkvtxm7cjmu7j2is/blob/master/docs/DOCS.md#migrating-to-knot-2).
2929+2330# Running a knot2
24312532The following is how I actually run `knot.oyster.cafe`. Please treat it as one possible setup.
···90979198Moving sshd is something that can lock one out of one's own server, so do it in this order:
92999393-1. Open a second SSH session to the server and keep it open for this whole procedure. If step 4 goes wrong, having this session open might be the saving grace.
9494-2. 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.
9595-3. 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.
9696-4. Restart sshd, and **from the local computer, in a third terminal**, confirm `ssh -p 2200 root@the.server` works before continuing.
9797-5. Once confirmed, only now remove `Port 22` from `sshd_config`, restart sshd once more, & give the port to the knot. 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 example below does.
100100+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.
101101+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:
102102+ - `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.
103103+ - `disabled`, or no systemd: edit `Port` in `sshd_config`; sshd holds the port itself.
104104+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.
105105+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.
106106+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.
107107+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 example below does.
9810899109If 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.
100110101111HTTP can stay on 5555 behind a reverse proxy, or move to 443 if one wants the knot to terminate TLS itself.
102112113113+## Running under systemd
114114+115115+`systemd/knot.service` at the repository root is the unit I'd install to `/etc/systemd/system/` for running the binary straight on the machine, then `systemctl enable --now knot`.
116116+117117+It runs the knot as `git` from `/usr/local/bin/knot-server`, so edit `User=`, `Group=` and `ExecStart=` if one's setup differs. `ProtectSystem=strict` and `ReadWritePaths=/var/lib/knot` mean 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.
118118+103119## Running with containers
104120105105-The `Containerfile` at project root builds a distroless image with just the `knot-server` binary in it:
121121+`knot2/Containerfile` will build a distroless image with `knot-server` and `knot-migrate` in `/usr/local/bin`.
122122+Its `COPY` lines start at the workspace root,
123123+so build it from the repository root and point `-f` at it:
106124107125```sh
108108-podman build -t knot-oyster:latest .
126126+podman build -f knot2/Containerfile -t knot-oyster:latest .
109127```
110128111129I personally run it with a composefile. This is the file from `knot.oyster.cafe` with a few opsec adjustments:
···171189podman-compose up -d
172190```
173191174174-The `mkdir` is necessary, since the knot won't start unless the repo and LFS directories are present/writable. Podman would create the bind-mount sources for the operator, but then they belong to whichever unix user podman has rather than to the operator.
192192+The `mkdir` is necessary, since the knot won't start unless the repo and LFS directories are present/writable. Podman would create the bind-mount sources for the operator, but then they belong to whichever unix user podman has.
175193176194The knot creates the SSH host key on the first run at mode 600, aaand the sealed store on that same first run, because the knot's own signing key needs sealing before any repo exists. The knot purposefully won't load a host key that is group or other readable, so don't loosen those please.
177195178178-Note that this (my) config turns LFS on, since `lfs.store_path` is set. One can drop that whole `[lfs]` block if one doesn't want it. The floor of 30GiB is what I have judged for my disk (of 500GiB, doing other things at the same time), so pick something that suits one's own rather than copying mine.
196196+Note that this (my) config turns LFS on, since `lfs.store_path` is set. One can drop that whole `[lfs]` block if one doesn't want it. The floor of 30GiB is what I have judged for my disk (of 500GiB, doing other things at the same time), so pick something that suits one's own instead of copying mine.
179197180198Speaking of LFS, I made the directory different in the first place so that we could specify a whole separate storage medium if wanted. For example, let's say I want my actual git repos to be wicked fast, so everything *else* is on an SSD, and *only* LFS is on a massive-but-relatively-cheap HDD cluster. Wouldn't want terabytes and terabytes of massive files taking up precious SSD space in this economy!
199199+200200+## NixOS
201201+202202+`nixosModules.knot-rs` renders the config file, defines the same hardened unit as above, and creates the state directory. A `config.toml` the operator writes goes unread under the module. Add the flake as an input, import the module, and write a `knot.nix`:
203203+204204+```nix
205205+{
206206+ inputs.tangled.url = "git+https://tangled.org/did:plc:j5hmlfdrwkvtxm7cjmu7j2is";
207207+ outputs = {nixpkgs, tangled, ...}: {
208208+ nixosConfigurations.knot = nixpkgs.lib.nixosSystem {
209209+ system = "x86_64-linux";
210210+ modules = [tangled.nixosModules.knot-rs ./knot.nix];
211211+ };
212212+ };
213213+}
214214+```
215215+216216+```nix
217217+{
218218+ services.tangled.knot-rs = {
219219+ enable = true;
220220+ environmentFile = "/etc/knot/knot.env";
221221+ settings = {
222222+ server = {
223223+ hostname = "knot.oyster.cafe";
224224+ admins = ["did:plc:nel"];
225225+ ssh_listen_addr = "[::]:22";
226226+ };
227227+ atproto.plc_directory = "https://plc.directory";
228228+ xrpc = {
229229+ trusted_proxy_header = "x-forwarded-for";
230230+ trusted_proxies = ["127.0.0.1" "::1"];
231231+ };
232232+ };
233233+ };
234234+235235+ services.openssh.ports = [2200];
236236+}
237237+```
238238+239239+- `environmentFile` is where `KNOT_MASTER_KEY=` goes, and the module refuses to build without it. Write the path, never the value, so the key stays out of the nix store. Every `KNOT_*` variable the file sets overrides the matching key in `settings`.
240240+- Moving `services.openssh` to another port frees 22 for the knot, per the port dance above, and the module asserts the collision instead of starting two services on one port. It adds `CAP_NET_BIND_SERVICE` itself once a listen address is below 1024. Confirm a session on the new port before rebuilding, since a rebuild that moves sshd and takes 22 in one go will lock one out if the port is wrong.
241241+- `openFirewall` defaults to true and opens the port of each listen address that isn't loopback, so the knot's 22 opens while the default `listen_addr = "127.0.0.1:5555"` stays shut for a proxy on the same host. `services.openssh` opens its own port.
242242+- `stateDir` defaults to `/var/lib/knot`, and `systemd.tmpfiles` creates it and its `repos` at mode 0750 for the `knot` user. `settings.secrets.sealed_key_file` and `settings.server.ssh_host_key_file` default to `sealed-keys` and `ssh_host_key` inside it, and the module works out `ReadWritePaths=` from wherever the operator puts them.
243243+- The module installs the knot and nothing else. [Migrating from the Go knot](https://tangled.org/did:plc:j5hmlfdrwkvtxm7cjmu7j2is/blob/master/docs/DOCS.md#migrating-to-knot-2) runs `knot-migrate` out of `nix build`, before the module is on the machine at all.
181244182245## TLS
183246···277340278341Push works over both, of course . For HTTP pushing, it is up to the user to find a good Tangled-CLI or something that can put the right things in the git credential helper such that a service auth token is minted and used on push.
279342280280-A trailing `.git` on the repo name is optional, so `did:plc:nel/squid.git` goes to the same repo as `did:plc:nel/squid`. That only applies to the repo name variant though - `did:plc:barnacle.git` is read as a DID rather than as a repo-DID with a suffix, and it won't resolve. This would be made better from better DID parsing, since a `did:plc` can't have dots, only a `did:web` can.
343343+A trailing `.git` on the repo name is optional, so `did:plc:nel/squid.git` goes to the same repo as `did:plc:nel/squid`. That only applies to the repo name variant though - `did:plc:barnacle.git` is read as a DID with a `.git` on the end of it, and it won't resolve. This would be made better from better DID parsing, since a `did:plc` can't have dots, only a `did:web` can.
281344282345## Things worth knowing before one commits (get it?) to a config
283346···315378316379## Updating
317380318318-// TODO: publish to ATCR, maybe nix something something.
381381+Pre-built images are at `atcr.io/tangled.org/knot:2`,
382382+with `knot-server` and `knot-migrate` in them.
383383+The image comes from [@tangled.org/knot-docker](https://tangled.org/did:plc:f5s5la5wlofsxidb3zemdune) instead of the `Containerfile` here.
384384+It's a Debian build, with its binaries in `/usr/bin`,
385385+and the `:latest` tag over there is still the Go knot,
386386+so one has to ask for `:2` on purpose.
387387+My composefile above points at a locally built image under `pull_policy: never`.
388388+Switching to the published image means editing the `image:` line,
389389+or tagging the pulled image with the name the compose file already has:
390390+391391+```sh
392392+podman pull atcr.io/tangled.org/knot:2
393393+podman tag atcr.io/tangled.org/knot:2 localhost/knot-oyster:latest
394394+podman-compose up -d
395395+```
396396+397397+The published image bakes `KNOT_SCAN_PATH`,
398398+`KNOT_SEALED_KEY_FILE`,
399399+`KNOT_SSH_HOST_KEY_FILE` and `KNOT_MASTER_KEY_ENV` into itself,
400400+and an environment variable overrides the config file.
401401+The knot will then look for the sealed store at `/data/sealed-keys` and the host key at `/data/ssh_host_key`,
402402+whatever the mounted `config.toml` sets,
403403+find neither,
404404+and generate a new identity and a new host key on a path that isn't mounted by anything.
405405+Put my two paths back in the compose environment before switching to the published image:
406406+407407+```yaml
408408+ environment:
409409+ KNOT_SEALED_KEY_FILE: /data/secrets/sealed.bin
410410+ KNOT_SSH_HOST_KEY_FILE: /data/ssh/host_key
411411+```
412412+413413+The published image also runs as its own `knot` user at uid 1000,
414414+where the distroless image here runs as root,
415415+so chown the four bind mounts before the first start:
416416+417417+```sh
418418+sudo chown -R 1000:1000 repos ssh secrets lfs
419419+```
420420+421421+Skipping the chown will stop the knot at startup with `repo.scan_path /data/repos isn't writable`,
422422+since root wrote every one of the directories.
423423+`user: "0:0"` in the compose file is the other way out,
424424+at the cost of running the knot as root.
425425+426426+Building it oneself is the same three lines as always:
319427320428```sh
321429git pull
322322-podman build -t knot-oyster:latest .
430430+podman build -f knot2/Containerfile -t knot-oyster:latest .
323431podman-compose up -d
324432```
325433
···264264 .context("decode master key as base64")?,
265265 )
266266 .context("master key from environment")?;
267267+ if !config.secrets.sealed_key_file.exists() {
268268+ tracing::info!(
269269+ path = %config.secrets.sealed_key_file.display(),
270270+ "the knot will seal a new signing identity, since it can't find a key store at this path"
271271+ );
272272+ }
267273 let secrets = Arc::new(
268274 SealedStore::open(
269275 &config.secrets.sealed_key_file,
···374380 max_objects: ObjectCount::from(config.pack.selection_max_objects),
375381 time_budget: Duration::from_secs(config.pack.selection_time_budget_secs),
376382 });
383383+ if !config.server.ssh_host_key_file.exists() {
384384+ tracing::info!(
385385+ path = %config.server.ssh_host_key_file.display(),
386386+ "the knot will generate an ssh host key, since it can't find a key at this path. \
387387+ Anyone who already has this knot's fingerprint cached will see it change"
388388+ );
389389+ }
377390 let host_key = knot_ssh::load_or_create_host_key(&config.server.ssh_host_key_file)
378391 .context("load or create SSH host key")?;
379392