feat(diigo-cache): production deployment with tailscale, systemd, and nix
diigo binds the session cookie to the IP it was issued to, so a Cloudflare deployment can't use a cookie minted in the browser. Run diigo-cache and webapp in one workerd on bldbox instead. The flake bundles each worker with wrangler's dry run, so the modules match what `wrangler deploy` ships, and generates the workerd config from the bundles. workerd comes from nixpkgs' wrangler, the only packaged one. A smoke check serves the home page and mints a webapp instance, which covers the WEBAPP binding and Durable Object storage. The service is a system unit with DynamicUser, so the webapp state holding the cookie isn't readable by processes running as the user. It listens on loopback behind tailscale serve: HTTPS, which PasswordCredential needs, at the tailnet name only. Operator docs in doc/; unsettled points recorded as IBIS issues. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
9c3eaa9819
commit
857816166e
14 files changed
+580
-2
No files matched your search
@@ -10,3 +10,4 @@ dist/
|
||||
worker-configuration.d.ts
|
||||
emdash-env.d.ts# diigo exports: personal data
|
||||
export/
|
||||
result
|
||||
@@ -69,4 +69,4 @@ Check a filled cache against a diigo RSS export: `node scripts/compare-export.ts
|
||||
|
||||
## Deploy
|
||||
|
||||
Not deployed yet; the order matters (goal 5 in [the capsite checklist](../capsite-checklist.md)): webapp first (`cd ../webapp && npx wrangler deploy`), then diigo-cache, after rate-limiting `/api` (above).
|
||||
On bldbox, tailnet only, via [the flake](flake.nix): see [the operator docs](doc/README.md). Cloudflare is out while diigo binds the cookie to the IP it was issued to ([open issue 1](doc/open-issues.md#-1-how-does-diigo-see-the-cookie-come-from-the-ip-it-was-issued-to)).
|
||||
@@ -23,4 +23,4 @@ Then, in the browser:
|
||||
- **Ids are capabilities:** the cache id is a bearer key you keep in your password manager. It rides URL fragments and POST bodies, never a path or query string, so it stays out of logs and history.
|
||||
- **Cheap to read:** pages come from Durable Object SQLite storage, and a fill job in the background fetches each diigo page once.
|
||||
|
||||
Design, tests, and deploying: [CONTRIBUTING](CONTRIBUTING.md).
|
||||
Design and tests: [CONTRIBUTING](CONTRIBUTING.md). Running it on bldbox: [the operator docs](doc/README.md).
|
||||
@@ -0,0 +1,19 @@
|
||||
# Operating diigo-cache on bldbox
|
||||
|
||||
diigo-cache and [webapp](../../webapp/README.md) run together in one workerd on bldbox, reachable only over the tailnet at `https://bldbox.tail09e44.ts.net/`. Cloudflare doesn't work: diigo binds the session cookie to the IP address it was issued to, and Cloudflare's egress IPs aren't that address (see [open issue 1](open-issues.md#-1-how-does-diigo-see-the-cookie-come-from-the-ip-it-was-issued-to)).
|
||||
|
||||
```
|
||||
browser on the tailnet
|
||||
→ tailscale serve (HTTPS, bldbox.tail09e44.ts.net:443)
|
||||
→ diigo-cache.socket (127.0.0.1:8790)
|
||||
→ diigo-cache.service: workerd, DynamicUser
|
||||
diigo-cache worker → state in /var/lib/diigo-cache/cache
|
||||
webapp worker → state in /var/lib/diigo-cache/webapp (holds the cookie)
|
||||
→ diigo.com
|
||||
```
|
||||
|
||||
- [Install, upgrade, roll back](install.md)
|
||||
- [Day to day: create a cache, logs, backups](operate.md)
|
||||
- [Open issues](open-issues.md), in IBIS style
|
||||
|
||||
The build is [the flake](../flake.nix): wrangler bundles each worker as `wrangler deploy` would, [gen-config.sh](../nix/gen-config.sh) fills [the workerd config](../nix/config.capnp.in), and the [socket](../nix/diigo-cache.socket) and [service](../nix/diigo-cache.service) units ship in the package. workerd comes from nixpkgs' `wrangler`, which bundles a patched one; nixpkgs has no standalone `workerd`.
|
||||
@@ -0,0 +1,81 @@
|
||||
# Install, upgrade, roll back
|
||||
|
||||
Everything below runs on bldbox. Neither unit has been run under the system manager yet; the flake's smoke check runs the same build under workerd directly.
|
||||
|
||||
## Once: tailnet HTTPS
|
||||
|
||||
In the [Tailscale admin console DNS page](https://login.tailscale.com/admin/dns), enable MagicDNS (on) and **HTTPS Certificates** (off as of 2026-10-14: `tailscale status --json | jq .CertDomains` is `null`). HTTPS matters: the "save to password manager" button uses `PasswordCredential`, which browsers offer only in a secure context.
|
||||
|
||||
## Build and check
|
||||
|
||||
```sh
|
||||
cd ~/oros-bots/capsite/diigo-cache
|
||||
nix flake check # builds, then serves the home page and mints a webapp instance
|
||||
out=$(nix build --no-link --print-out-paths .#)
|
||||
nix/probe-unit.sh "$out" # serves it under the unit's syscall filter: "probe: ok"
|
||||
```
|
||||
|
||||
The probe catches what the smoke check can't: the nix sandbox has no systemd, so it doesn't apply the unit's `SystemCallFilter`. The flake only sees files git tracks: `git add` new files before building.
|
||||
|
||||
## Install
|
||||
|
||||
The service runs whatever `/nix/var/nix/profiles/diigo-cache` points at, so the profile is also the GC root and the rollback history.
|
||||
|
||||
nix-env is spelled out in full because sudo's `secure_path` leaves out nix's profile.
|
||||
|
||||
```sh
|
||||
sudo /nix/var/nix/profiles/default/bin/nix-env -p /nix/var/nix/profiles/diigo-cache --set "$out"
|
||||
sudo systemctl link /nix/var/nix/profiles/diigo-cache/lib/systemd/system/diigo-cache.{socket,service}
|
||||
sudo systemctl enable --now diigo-cache.socket
|
||||
sudo tailscale serve --bg --https=443 http://127.0.0.1:8790
|
||||
```
|
||||
|
||||
`tailscale serve --bg` persists across reboots in tailscaled's state; `tailscale serve status` shows it.
|
||||
|
||||
## Verify
|
||||
|
||||
```sh
|
||||
curl -sf https://bldbox.tail09e44.ts.net/ | grep -c _connect # 1+; first request starts workerd
|
||||
systemctl status diigo-cache.service
|
||||
```
|
||||
|
||||
On a 502, make sure `tailscale serve` finds something on 8790: `curl -sI http://127.0.0.1:8790/`. (The cache also answers 502 when it can't load a page from diigo; its body then says `could not load page`.) If workerd crashes on start, systemd stops retrying, then fails the socket too (`Result: service-start-limit-hit` in `systemctl status diigo-cache.socket`). After fixing the cause:
|
||||
|
||||
```sh
|
||||
sudo systemctl reset-failed diigo-cache.socket diigo-cache.service
|
||||
sudo systemctl start diigo-cache.socket
|
||||
```
|
||||
|
||||
From another tailnet device, open `https://bldbox.tail09e44.ts.net/`. From off the tailnet, the name doesn't resolve and `100.89.146.23` doesn't route.
|
||||
|
||||
## Upgrade
|
||||
|
||||
```sh
|
||||
out=$(nix build --no-link --print-out-paths .#)
|
||||
nix/probe-unit.sh "$out"
|
||||
sudo /nix/var/nix/profiles/default/bin/nix-env -p /nix/var/nix/profiles/diigo-cache --set "$out"
|
||||
sudo systemctl daemon-reload # in case the units changed
|
||||
sudo systemctl restart diigo-cache.service
|
||||
```
|
||||
|
||||
Durable Object state outlives the code: an upgrade whose schema change existing state can't read is a breaking change (see [capsite AGENTS.md](../../AGENTS.md#durable-state-be-loud-and-conservative-about-breaking-changes)). Back up first ([operate](operate.md#backups)).
|
||||
|
||||
## Roll back
|
||||
|
||||
```sh
|
||||
sudo /nix/var/nix/profiles/default/bin/nix-env -p /nix/var/nix/profiles/diigo-cache --list-generations
|
||||
sudo /nix/var/nix/profiles/default/bin/nix-env -p /nix/var/nix/profiles/diigo-cache --rollback
|
||||
sudo systemctl restart diigo-cache.service
|
||||
```
|
||||
|
||||
## Update nixpkgs
|
||||
|
||||
`nix flake update` moves wrangler, and with it workerd. The flake names workerd's version (`version` in [flake.nix](../flake.nix)); if nixpkgs' wrangler bundles a different one, the build fails with `no workerd <version>`. Set it to the bundled version, which must be no older than the workers' `compatibility_date`.
|
||||
|
||||
## Remove
|
||||
|
||||
```sh
|
||||
sudo tailscale serve --https=443 off
|
||||
sudo systemctl disable --now diigo-cache.socket diigo-cache.service
|
||||
sudo rm /nix/var/nix/profiles/diigo-cache* # state in /var/lib/private/diigo-cache stays
|
||||
```
|
||||
@@ -0,0 +1,121 @@
|
||||
# Open issues
|
||||
|
||||
In [IBIS](https://github.com/dckc/vattr97/wiki/IBIS) notation: `?` issue, `?!` resolved, `?~` open with a fallback; `:` position, `:+` accepted, `:-` rejected, `:~+` fallback; `+` pro, `-` con; `!` to-do; `.` comment. Append; don't renumber.
|
||||
|
||||
### ?! 1. How does diigo see the cookie come from the IP it was issued to?
|
||||
|
||||
```
|
||||
?! 1. How does diigo see the cookie come from the IP it was issued to?
|
||||
. observed: minted from the browser, the cookie failed from Cloudflare
|
||||
. unverified that IP is the only binding; the mint sends user-agent
|
||||
"Mozilla/5.0", not the browser's, and that works in dev
|
||||
: 1a. log in to diigo from a browser that shares bldbox's public IP
|
||||
(same LAN, same NAT)
|
||||
+ no setup if that's where the browser already is
|
||||
- breaks when the browser roams (phone, laptop away from home)
|
||||
! compare: curl https://ifconfig.me on bldbox and in the browser
|
||||
: 1b. advertise bldbox as a tailnet exit node; route the browser through
|
||||
it while logging in to diigo and copying the cookie
|
||||
+ bldbox's IP by construction, from anywhere on the tailnet
|
||||
- bldbox isn't an exit node (ExitNodeOption false, 2026-10-14);
|
||||
needs `tailscale set --advertise-exit-node` and admin approval
|
||||
:+ 1c. run the browser on bldbox
|
||||
+ worked 2026-10-14: a cookie copied from Brave's devtools on bldbox
|
||||
minted, wired public, and filled 168 pages
|
||||
:- 1d. deploy to Cloudflare
|
||||
- its egress IPs aren't the browser's (the failure that prompted this)
|
||||
```
|
||||
|
||||
### ?~ 2. How is diigo-cache reachable only from the tailnet?
|
||||
|
||||
```
|
||||
?~ 2. How is diigo-cache reachable only from the tailnet?
|
||||
:~+ 2a. workerd on 127.0.0.1:8790; tailscale serve proxies
|
||||
https://bldbox.tail09e44.ts.net/ to it
|
||||
+ HTTPS with a real cert: PasswordCredential needs a secure context
|
||||
+ tailscaled owns the tailnet address; no start-order dependency
|
||||
- needs HTTPS certificates on in the tailnet (off as of 2026-10-14)
|
||||
! enable in the admin console (install.md)
|
||||
- every local user and process on bldbox reaches 127.0.0.1:8790 (see 3)
|
||||
+ the cache id is still needed for anything private
|
||||
: 2b. listen on 100.89.146.23 directly (ListenStream, FreeBind=yes)
|
||||
- plain HTTP: the password-manager button fails
|
||||
: 2c. tailscale serve to a unix socket only it can open
|
||||
+ no loopback port
|
||||
- unverified that tailscale 1.90.9 serve takes a unix socket target
|
||||
? 2d. Which tailnet devices may reach it?
|
||||
. today: every device on the tailnet (m5x, penguin, pixel-7, tiny-jwc)
|
||||
: 2d-i. restrict to some devices with a tailnet ACL grant on bldbox:443
|
||||
```
|
||||
|
||||
### ?~ 3. What account does the service run as?
|
||||
|
||||
```
|
||||
?~ 3. What account does the service run as?
|
||||
:~+ 3a. a system unit with DynamicUser=yes
|
||||
+ the cookie's state is 0700 to a uid nothing else runs as; agents
|
||||
and bots running as connolly can't read it
|
||||
+ full sandboxing (systemd-analyze security: 2.2 OK)
|
||||
- install and upgrade need sudo
|
||||
: 3b. a user unit under connolly, with lingering
|
||||
+ no sudo for upgrade; a ~/.local profile
|
||||
- the cookie sits in connolly's files, readable by every process
|
||||
connolly runs, Discord and opencode bots included
|
||||
- Ubuntu's AppArmor userns restriction weakens user-manager sandboxing
|
||||
- tailscale serve needs root (or `tailscale set --operator`) anyway
|
||||
```
|
||||
|
||||
### ? 4. Does /api need a rate limit on the tailnet?
|
||||
|
||||
```
|
||||
? 4. Does /api need a rate limit on the tailnet?
|
||||
. CONTRIBUTING asks for one "before exposing a deployment"
|
||||
:~+ 4a. not while only tailnet devices reach it (see 2)
|
||||
: 4b. a rate limit in front of workerd before any funnel (see 5)
|
||||
```
|
||||
|
||||
### ? 5. How does the weekly roundup read the public page?
|
||||
|
||||
```
|
||||
? 5. How does the weekly roundup read the public page?
|
||||
. the madmode blog build needs /public/<username>; where does it run?
|
||||
: 5a. build on a tailnet device
|
||||
: 5b. join CI to the tailnet (Tailscale's GitHub Action)
|
||||
: 5c. tailscale funnel just /public/ to the internet
|
||||
+ public pages are public by design
|
||||
- exposes a guessable URL; needs 4b
|
||||
```
|
||||
|
||||
### ? 6. How is state backed up?
|
||||
|
||||
```
|
||||
? 6. How is state backed up?
|
||||
:~+ 6a. by hand, service stopped (operate.md)
|
||||
: 6b. a systemd timer
|
||||
- backups hold the cookie: where do they go, encrypted how?
|
||||
: 6c. none: the cache is a cache
|
||||
- the ids people saved point at objects that would be gone
|
||||
```
|
||||
|
||||
### ? 7. How does a cache get a fresh cookie?
|
||||
|
||||
```
|
||||
? 7. How does a cache get a fresh cookie?
|
||||
. a wired cache refuses a different webapp id
|
||||
:~+ 7a. mint a fresh webapp instance and wire a new cache
|
||||
- a new cache id to save
|
||||
+ wiring public re-registers the username, so the public URL stays
|
||||
: 7b. let webapp replace the cookie in an existing instance
|
||||
+ cache ids stay put
|
||||
```
|
||||
|
||||
### ? 8. Should `wrangler dev` state move to bldbox?
|
||||
|
||||
```
|
||||
? 8. Should `wrangler dev` state move to bldbox?
|
||||
. Durable Object unique keys follow miniflare's `<script>-<class>`,
|
||||
so the per-object files may carry over; untested
|
||||
:~+ 8a. start fresh on bldbox
|
||||
+ dev cookies were minted for wherever dev ran (see 1)
|
||||
: 8b. copy .wrangler/state/v3/do/<script>-<class>/ into the state dirs
|
||||
```
|
||||
@@ -0,0 +1,48 @@
|
||||
# Day to day
|
||||
|
||||
## Create a cache
|
||||
|
||||
As in [the README](../README.md#getting-started), at `https://bldbox.tail09e44.ts.net/` instead of localhost: mint, wire, save the ids, fill. Copy the cookie from a browser running on bldbox: diigo accepts it only from the IP it was issued to (see [issue 1](open-issues.md#-1-how-does-diigo-see-the-cookie-come-from-the-ip-it-was-issued-to)).
|
||||
|
||||
The public page is then `https://bldbox.tail09e44.ts.net/public/<diigo username>?days=7`, on the tailnet only.
|
||||
|
||||
## Logs
|
||||
|
||||
```sh
|
||||
journalctl -u diigo-cache.service -f -o cat |
|
||||
jq -Rr 'fromjson? | "\(.timestamp / 1000 | todate) \(.level) \(.message | gsub("\\s*\\n\\s*"; " "))"'
|
||||
```
|
||||
|
||||
`fromjson?` skips lines that aren't JSON: systemd's own ("Started …") and entries from before `structuredLogging`.
|
||||
|
||||
workerd runs with `structuredLogging`: each entry is one line of JSON, `{timestamp, level, message}`. `message` is the logged object as `console.log` prints it, not JSON: on Cloudflare, Workers Logs indexes the fields of a logged object, so the workers log objects rather than strings.
|
||||
|
||||
workerd logs the workers' `console.*` output: `{event: "cache", kind, page, outcome}` per page read, `{event: "fill", state, …}` per fill step, `{event: "mint"}`. Ids show only as fingerprints; the cookie never appears.
|
||||
|
||||
## State
|
||||
|
||||
With `DynamicUser=yes`, systemd keeps the state in `/var/lib/private/diigo-cache/` (`/var/lib/diigo-cache` is a symlink to it), mode 0700, owned by a uid that exists only while the service runs:
|
||||
|
||||
| Dir | Holds | Secret? |
|
||||
| :--- | :--- | :--- |
|
||||
| `cache/diigo-cache-DiigoCache/` | private caches: every bookmark | yes |
|
||||
| `cache/diigo-cache-PublicCache/` | public caches and the username index | no |
|
||||
| `webapp/webapp-WebAppDo/` | webapp instances: the diigo cookie | yes, a credential |
|
||||
|
||||
Each Durable Object is one `<id hash>.sqlite`. Read one with the service stopped, or copy it first: `sudo sqlite3 -readonly <file> .tables`.
|
||||
|
||||
## Backups
|
||||
|
||||
The private caches are refillable from diigo; the cookie can be re-minted. What's lost with the state is the binding from the ids people saved to these objects. Backups hold the cookie, so treat them as secret.
|
||||
|
||||
```sh
|
||||
sudo systemctl stop diigo-cache.socket diigo-cache.service # the socket would restart it
|
||||
sudo tar -C /var/lib/private -czf diigo-cache-$(date +%F).tgz diigo-cache
|
||||
sudo systemctl start diigo-cache.socket
|
||||
```
|
||||
|
||||
No backup is scheduled (see [open issue 6](open-issues.md#-6-how-is-state-backed-up)).
|
||||
|
||||
## The cookie expires
|
||||
|
||||
When fills log `{event: "fill", state: "failed"}` with a diigo auth error, mint a fresh webapp instance and wire a new cache. A wired cache refuses a different webapp id (`setup` in [cache.ts](../src/cache.ts)) (see [open issue 7](open-issues.md#-7-how-does-a-cache-get-a-fresh-cookie)).
|
||||
Generated
+27
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1791734076,
|
||||
"narHash": "sha256-i5qk1kLiit98rRbh1AXmWBNT9QXC/y4l5Wk/u5EdpDQ=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "7fd4004d0511991fff3a0524856a9be09e28c5f0",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
{
|
||||
description = "diigo-cache and webapp on workerd, outside Cloudflare";
|
||||
|
||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||
|
||||
outputs =
|
||||
{ self, nixpkgs }:
|
||||
let
|
||||
system = "x86_64-linux";
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
inherit (pkgs) lib;
|
||||
|
||||
# nixpkgs has no workerd of its own; wrangler carries a patched one
|
||||
workerd =
|
||||
pkgs.runCommand "workerd-${version}" { } ''
|
||||
bin=${pkgs.wrangler}/lib/node_modules/.pnpm/@cloudflare+workerd-linux-64@${version}/node_modules/@cloudflare/workerd-linux-64/bin/workerd
|
||||
test -x $bin || { echo "no workerd ${version} in ${pkgs.wrangler}" >&2; exit 1; }
|
||||
mkdir -p $out/bin
|
||||
ln -s $bin $out/bin/workerd
|
||||
'';
|
||||
# the workerd that nixpkgs' wrangler bundles; must be at least the
|
||||
# workers' compatibility_date
|
||||
version = "1.20261006.1";
|
||||
|
||||
# the flake's source is all of capsite; take what the two workers need
|
||||
src = lib.fileset.toSource {
|
||||
root = ./..;
|
||||
fileset = lib.fileset.unions [
|
||||
./package.json
|
||||
./pnpm-lock.yaml
|
||||
./wrangler.jsonc
|
||||
./tsconfig.json
|
||||
./src
|
||||
../webapp/wrangler.jsonc
|
||||
../webapp/tsconfig.json
|
||||
../webapp/src
|
||||
];
|
||||
};
|
||||
|
||||
diigo-cache = pkgs.stdenv.mkDerivation (finalAttrs: {
|
||||
pname = "diigo-cache";
|
||||
version = "0.0.0";
|
||||
inherit src;
|
||||
sourceRoot = "${finalAttrs.src.name}/diigo-cache";
|
||||
|
||||
nativeBuildInputs = [
|
||||
pkgs.nodejs
|
||||
pkgs.pnpm_10
|
||||
pkgs.pnpmConfigHook
|
||||
pkgs.wrangler
|
||||
];
|
||||
pnpmDeps = pkgs.fetchPnpmDeps {
|
||||
inherit (finalAttrs) pname version src sourceRoot;
|
||||
pnpm = pkgs.pnpm_10;
|
||||
fetcherVersion = 3;
|
||||
hash = "sha256-sn/juOL2mhyy3i57GCUKA2X8SPyR9FwY4KoxdkgZa0M=";
|
||||
};
|
||||
|
||||
buildPhase = ''
|
||||
runHook preBuild
|
||||
export HOME=$TMPDIR WRANGLER_SEND_METRICS=false
|
||||
# wrangler writes .wrangler/tmp beside each config
|
||||
chmod -R u+w ../webapp
|
||||
wrangler deploy --dry-run --outdir $PWD/dist/cache -c wrangler.jsonc
|
||||
wrangler deploy --dry-run --outdir $PWD/dist/webapp -c ../webapp/wrangler.jsonc
|
||||
bash ${./nix/gen-config.sh} dist ${./nix/config.capnp.in} \
|
||||
"$(sed -n 's/.*"compatibility_date": *"\([^"]*\)".*/\1/p' wrangler.jsonc)" \
|
||||
"$(sed -n 's/.*"compatibility_date": *"\([^"]*\)".*/\1/p' ../webapp/wrangler.jsonc)" \
|
||||
> dist/config.capnp
|
||||
runHook postBuild
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
runHook preInstall
|
||||
share=$out/share/diigo-cache
|
||||
mkdir -p $share $out/bin $out/lib/systemd/system
|
||||
cp -r dist/cache dist/webapp dist/config.capnp $share/
|
||||
rm -f $share/*/README.md $share/*/*.map
|
||||
cat > $out/bin/diigo-cache-serve <<EOF
|
||||
#!${pkgs.runtimeShell}
|
||||
exec ${workerd}/bin/workerd serve $share/config.capnp "\$@"
|
||||
EOF
|
||||
chmod +x $out/bin/diigo-cache-serve
|
||||
cp ${./nix/diigo-cache.socket} $out/lib/systemd/system/diigo-cache.socket
|
||||
cp ${./nix/diigo-cache.service} $out/lib/systemd/system/diigo-cache.service
|
||||
runHook postInstall
|
||||
'';
|
||||
});
|
||||
in
|
||||
{
|
||||
packages.${system} = {
|
||||
default = diigo-cache;
|
||||
inherit diigo-cache workerd;
|
||||
};
|
||||
|
||||
# the built config serves the home page, and mints a webapp instance
|
||||
# through the WEBAPP binding into webapp's Durable Object storage;
|
||||
# each log entry is one line of JSON
|
||||
checks.${system}.smoke = pkgs.runCommand "diigo-cache-smoke" { nativeBuildInputs = [ pkgs.curl pkgs.jq ]; } ''
|
||||
mkdir -p cache webapp
|
||||
${diigo-cache}/bin/diigo-cache-serve -s http=127.0.0.1:18790 \
|
||||
-d cache-state=$PWD/cache -d webapp-state=$PWD/webapp > log 2>&1 &
|
||||
pid=$!
|
||||
for i in $(seq 50); do curl -sf http://127.0.0.1:18790/ -o home.html && break; sleep 0.2; done
|
||||
grep -q '/_connect' home.html
|
||||
curl -sf -X POST http://127.0.0.1:18790/_mint \
|
||||
--data-urlencode url=https://diigo.invalid/ --data-urlencode cookie=x \
|
||||
-D headers -o /dev/null
|
||||
grep -qi '^x-webapp-id: [A-Za-z0-9-]' headers
|
||||
ls webapp/*/*.sqlite
|
||||
kill $pid; wait $pid || true
|
||||
cat log
|
||||
grep -q "event: 'mint'" log
|
||||
jq -e . log > /dev/null
|
||||
touch $out
|
||||
'';
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
# diigo-cache and the webapp it reads diigo through, in one workerd.
|
||||
#
|
||||
# The listening socket has no address here: systemd hands it in
|
||||
# (-S http=3), so holding the socket is the authority to reach the cache.
|
||||
# State directories likewise come from the command line:
|
||||
# -d cache-state=<dir> -d webapp-state=<dir>.
|
||||
#
|
||||
# Durable Object unique keys follow miniflare's `<script>-<class>`, so a
|
||||
# `wrangler dev` state directory names its objects the same way.
|
||||
using Workerd = import "/workerd/workerd.capnp";
|
||||
|
||||
const config :Workerd.Config = (
|
||||
services = [
|
||||
(name = "diigo-cache", worker = .cache),
|
||||
(name = "webapp", worker = .webapp),
|
||||
(name = "nowhere", network = (allow = [])),
|
||||
(name = "cache-state", disk = (path = "cache-state", writable = true)),
|
||||
(name = "webapp-state", disk = (path = "webapp-state", writable = true)),
|
||||
],
|
||||
sockets = [
|
||||
(name = "http", http = (), service = "diigo-cache"),
|
||||
],
|
||||
# one JSON object per line, console.* included: journald keeps an
|
||||
# object in one entry rather than one entry per line of it
|
||||
structuredLogging = true,
|
||||
);
|
||||
|
||||
const cache :Workerd.Worker = (
|
||||
compatibilityDate = "@CACHE_DATE@",
|
||||
modules = [
|
||||
@CACHE_MODULES@
|
||||
],
|
||||
durableObjectNamespaces = [
|
||||
(className = "DiigoCache", uniqueKey = "diigo-cache-DiigoCache", enableSql = true),
|
||||
(className = "PublicCache", uniqueKey = "diigo-cache-PublicCache", enableSql = true),
|
||||
],
|
||||
durableObjectStorage = (localDisk = "cache-state"),
|
||||
bindings = [
|
||||
(name = "CACHE_DO", durableObjectNamespace = "DiigoCache"),
|
||||
(name = "PUBLIC_DO", durableObjectNamespace = "PublicCache"),
|
||||
(name = "WEBAPP", service = (name = "webapp", entrypoint = "WebAppFactory")),
|
||||
],
|
||||
# diigo is reached only through WEBAPP
|
||||
globalOutbound = "nowhere",
|
||||
);
|
||||
|
||||
const webapp :Workerd.Worker = (
|
||||
compatibilityDate = "@WEBAPP_DATE@",
|
||||
modules = [
|
||||
@WEBAPP_MODULES@
|
||||
],
|
||||
durableObjectNamespaces = [
|
||||
(className = "WebAppDo", uniqueKey = "webapp-WebAppDo", enableSql = true),
|
||||
],
|
||||
durableObjectStorage = (localDisk = "webapp-state"),
|
||||
bindings = [
|
||||
(name = "WEBAPP_DO", durableObjectNamespace = "WebAppDo"),
|
||||
],
|
||||
# globalOutbound: workerd's default "internet", public addresses only
|
||||
);
|
||||
@@ -0,0 +1,26 @@
|
||||
[Unit]
|
||||
Description=diigo-cache and webapp (workerd)
|
||||
Requires=diigo-cache.socket
|
||||
|
||||
[Service]
|
||||
# the profile path survives upgrades; see doc/install.md
|
||||
ExecStart=/nix/var/nix/profiles/diigo-cache/bin/diigo-cache-serve -S http=3 -d cache-state=/var/lib/diigo-cache/cache -d webapp-state=/var/lib/diigo-cache/webapp
|
||||
DynamicUser=yes
|
||||
StateDirectory=diigo-cache diigo-cache/cache diigo-cache/webapp
|
||||
StateDirectoryMode=0700
|
||||
# webapp fetches diigo.com; nothing else needs the network
|
||||
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
|
||||
ProtectSystem=strict
|
||||
ProtectHome=yes
|
||||
PrivateTmp=yes
|
||||
PrivateDevices=yes
|
||||
ProtectKernelTunables=yes
|
||||
ProtectKernelModules=yes
|
||||
ProtectControlGroups=yes
|
||||
RestrictNamespaces=yes
|
||||
LockPersonality=yes
|
||||
CapabilityBoundingSet=
|
||||
SystemCallFilter=@system-service
|
||||
# V8 uses memory protection keys; without these, SIGSYS at startup
|
||||
SystemCallFilter=pkey_alloc pkey_mprotect pkey_free
|
||||
# not MemoryDenyWriteExecute: V8's JIT needs W+X
|
||||
@@ -0,0 +1,9 @@
|
||||
[Unit]
|
||||
Description=diigo-cache: loopback port behind tailscale serve
|
||||
|
||||
[Socket]
|
||||
# tailscale serve proxies https://bldbox.<tailnet>.ts.net/ here
|
||||
ListenStream=127.0.0.1:8790
|
||||
|
||||
[Install]
|
||||
WantedBy=sockets.target
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
# Fill config.capnp.in from `wrangler deploy --dry-run` outdirs.
|
||||
# usage: gen-config.sh <dist> <template> <cache-date> <webapp-date> > config.capnp
|
||||
# <dist>/cache and <dist>/webapp each hold index.js plus the text modules
|
||||
# wrangler.jsonc's rules name (*.sql, capnweb's browser build); embed
|
||||
# paths are relative to the config, which sits beside them.
|
||||
set -euo pipefail
|
||||
dist=$1 template=$2 cache_date=$3 webapp_date=$4
|
||||
|
||||
modules() {
|
||||
local dir=$1
|
||||
if [[ ! -f $dist/$dir/index.js ]]; then
|
||||
echo "gen-config: $dist/$dir/index.js: missing" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo " (name = \"index.js\", esModule = embed \"$dir/index.js\"),"
|
||||
for f in "$dist/$dir"/*; do
|
||||
local name=${f##*/}
|
||||
case $name in
|
||||
index.js | README.md | *.map) ;;
|
||||
*.sql | *-index.js) echo " (name = \"$name\", text = embed \"$dir/$name\")," ;;
|
||||
*)
|
||||
echo "gen-config: $dir/$name: no module type rule" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
cache_modules=$(modules cache)
|
||||
webapp_modules=$(modules webapp)
|
||||
while IFS= read -r line; do
|
||||
case $line in
|
||||
@CACHE_MODULES@) echo "$cache_modules" ;;
|
||||
@WEBAPP_MODULES@) echo "$webapp_modules" ;;
|
||||
*)
|
||||
line=${line//@CACHE_DATE@/$cache_date}
|
||||
echo "${line//@WEBAPP_DATE@/$webapp_date}"
|
||||
;;
|
||||
esac
|
||||
done <"$template"
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env bash
|
||||
# Serve a built package under the service unit's syscall filter, as a
|
||||
# transient user unit, on a scratch port and state; check the home page.
|
||||
# The flake's smoke check can't: the nix sandbox has no systemd.
|
||||
# usage: probe-unit.sh <package out path>
|
||||
set -euo pipefail
|
||||
out=$1
|
||||
state=$(mktemp -d)
|
||||
trap 'systemctl --user stop diigo-cache-probe 2>/dev/null; rm -rf "$state"' EXIT
|
||||
mkdir "$state/cache" "$state/webapp"
|
||||
|
||||
props=()
|
||||
while IFS= read -r line; do props+=(-p "$line"); done \
|
||||
< <(grep '^SystemCallFilter=' "$out/lib/systemd/system/diigo-cache.service")
|
||||
|
||||
systemd-run --user -q --collect --unit=diigo-cache-probe "${props[@]}" \
|
||||
"$out/bin/diigo-cache-serve" -s http=127.0.0.1:18791 \
|
||||
-d cache-state="$state/cache" -d webapp-state="$state/webapp"
|
||||
for _ in $(seq 30); do
|
||||
if curl -sf -m 2 http://127.0.0.1:18791/ | grep -q _connect; then
|
||||
echo "probe: ok"
|
||||
exit 0
|
||||
fi
|
||||
sleep 0.3
|
||||
done
|
||||
echo "probe: no home page; see journalctl --user -u diigo-cache-probe and journalctl -k | grep syscall=" >&2
|
||||
exit 1
|
||||
Reference in new issue
Block a user