Files
captoys/diigo-cache/flake.nix
T
dckcandClaude Opus 5.5 857816166e 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>
2026-10-14 23:12:47 -05:00

119 lines
4.4 KiB
Nix

{
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
'';
};
}