Files
captoys/diigo-cache/flake.nix
T
dckc bb1fbbdc00 build(diigo-cache): ship the docs, and name the commit a build came from
systemctl status's Docs: line now leads to the README, at a profile
path that survives upgrades, and, for a build from a clean tree, to the
commit's source on git.inference.coop. The version ends in the short
rev (-dirty for a dirty tree), and share/doc/diigo-cache/REVISION holds
the full one. pnpmDeps keeps version 0.0.0, so a new commit doesn't
refetch the deps.

Generated-by: Claude Opus 5.5
2026-11-19 14:29:16 -06:00

135 lines
5.2 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;
# the commit a build came from; a dirty tree's ends in -dirty
shortRev = self.shortRev or self.dirtyShortRev or "unknown";
# a clean tree's commit, whose source anyone can read; systemctl
# status lists it under Docs:
sourceUrl = lib.optionalString (self ? rev) "https://git.inference.coop/dckc/captoys/src/commit/${self.rev}/diigo-cache";
# 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-${shortRev}";
inherit src;
sourceRoot = "${finalAttrs.src.name}/diigo-cache";
nativeBuildInputs = [
pkgs.nodejs
pkgs.pnpm_10
pkgs.pnpmConfigHook
pkgs.wrangler
];
pnpmDeps = pkgs.fetchPnpmDeps {
inherit (finalAttrs) pname src sourceRoot;
# not the rev: the deps don't change with each commit
version = "0.0.0";
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
substitute ${./nix/diigo-cache.service} $out/lib/systemd/system/diigo-cache.service \
--replace-fail '@source@' '${sourceUrl}'
# the docs, at a path the profile keeps across upgrades
doc=$out/share/doc/diigo-cache
mkdir -p $doc
cp ${./README.md} $doc/README.md
cp ${./CONTRIBUTING.md} $doc/CONTRIBUTING.md
cp -r ${./doc} $doc/doc
echo ${self.rev or self.dirtyRev or "unknown"} > $doc/REVISION
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
'';
};
}