Flake to setup a local env for atproto development

Add postgres to did plc

edouard.paris 5b21b5b0 4ad3366a

verified
Changed files
+69 -5
packages
+6 -4
flake.nix
···
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
did-plc-server = pkgs.callPackage ./packages/did-method-plc.nix { };
+
plc = pkgs.callPackage ./packages/plc.nix { inherit did-plc-server; };
caddy-proxy = pkgs.callPackage ./packages/caddy.nix { };
pds = pkgs.callPackage ./packages/pds.nix { };
mailhog = pkgs.callPackage ./packages/mailhog.nix { };
···
{
packages.${system} = {
-
did-plc-server = did-plc-server;
+
plc = plc;
caddy-proxy = caddy-proxy;
···
bluesky-pds
openssl
mailhog
+
postgresql
];
shellHook = ''
···
echo "Available commands:"
echo " nix run .#generate-certs - Generate test certificates"
echo " nix run .#caddy-proxy - Start Caddy with full config"
-
echo " nix run .#did-plc-server - Start DID PLC server"
+
echo " nix run .#plc - Start DID PLC server with PostgreSQL"
echo " nix run .#pds - Start Bluesky PDS server"
echo " nix run .#mailhog - Start MailHog email server"
echo ""
echo "Services:"
echo " Bluesky PDS: https://pds.example.org:8443 (proxied from port 3000)"
-
echo " DID PLC: https://plc.example.org:8444 (proxied from port 2582)"
+
echo " DID PLC: https://plc.example.org:8444 (proxied from port 2582, PostgreSQL on 5433)"
echo " MailHog: http://localhost:8025 (SMTP on port 1025)"
echo ""
echo "Environment variables:"
···
echo " 127.0.0.1 plc.example.org"
echo " 2. Run 'nix run .#generate-certs' to create certificates"
echo " 3. Run 'nix run .#mailhog' in one terminal"
-
echo " 4. Run 'nix run .#did-plc-server' in another terminal (if needed)"
+
echo " 4. Run 'nix run .#plc' in another terminal"
echo " 5. Run 'nix run .#pds' in another terminal"
echo " 6. Run 'nix run .#caddy-proxy' in another terminal"
echo " 7. Access services at:"
+2 -1
packages/did-method-plc.nix
···
# Copy the source and dependencies
cp -r . $out/share/did-plc-server/
-
# Create the executable wrapper
+
# Create the executable wrapper for just the PLC server
makeWrapper "${lib.getExe pnpm_9}" "$out/bin/did-plc-server" \
--chdir "$out/share/did-plc-server" \
--set-default LOG_ENABLED "true" \
--set-default LOG_LEVEL "debug" \
+
--set-default DATABASE_URL "postgresql://$(whoami)@localhost:5433/plc" \
--add-flags "--filter" \
--add-flags "@did-plc/server" \
--add-flags "start"
+61
packages/plc.nix
···
+
{ pkgs, did-plc-server }:
+
+
pkgs.writeShellScriptBin "plc" ''
+
set -e
+
+
echo "Starting PLC services (PostgreSQL + DID PLC Server)..."
+
+
# Set PostgreSQL environment
+
export PGHOST="localhost"
+
export PGDATA="./data/plc/db"
+
export PGPORT="5433"
+
export PGUSER="$(whoami)"
+
+
# Create data directory
+
mkdir -p "$PGDATA"
+
+
echo "Using PostgreSQL at $PGDATA"
+
+
# Initialize PostgreSQL data directory if it doesn't exist
+
if [ ! -d "$PGDATA/base" ]; then
+
echo "Initializing PostgreSQL database..."
+
${pkgs.postgresql}/bin/pg_ctl initdb -D "$PGDATA" -o "--auth-local=trust --auth-host=trust"
+
+
# Add some logging configuration
+
echo "log_min_messages = warning" >> "$PGDATA/postgresql.conf"
+
echo "log_min_error_statement = error" >> "$PGDATA/postgresql.conf"
+
echo "log_connections = on" >> "$PGDATA/postgresql.conf"
+
echo "log_disconnections = on" >> "$PGDATA/postgresql.conf"
+
fi
+
+
# Start PostgreSQL server
+
echo "Starting PostgreSQL on port $PGPORT..."
+
${pkgs.postgresql}/bin/pg_ctl -D "$PGDATA" -o "-p $PGPORT --unix_socket_directories='$PWD'" start
+
+
# Wait for PostgreSQL to be ready
+
echo "Waiting for PostgreSQL to be ready..."
+
until ${pkgs.postgresql}/bin/pg_isready -p "$PGPORT" -h localhost; do
+
sleep 1
+
done
+
+
# Create database if it doesn't exist
+
${pkgs.postgresql}/bin/createdb -p "$PGPORT" -h localhost plc 2>/dev/null || true
+
+
# Set DATABASE_URL
+
export DATABASE_URL="postgresql://$PGUSER@localhost:$PGPORT/plc"
+
+
echo "PostgreSQL ready at: $DATABASE_URL"
+
echo "Starting DID PLC Server..."
+
+
# Cleanup function using pg_ctl
+
cleanup() {
+
echo "Shutting down PostgreSQL..."
+
${pkgs.postgresql}/bin/pg_ctl -D "$PGDATA" stop -m fast 2>/dev/null || true
+
}
+
+
# Set trap to cleanup on exit
+
trap cleanup EXIT INT TERM
+
+
# Start the PLC server (don't use exec so cleanup trap works)
+
${did-plc-server}/bin/did-plc-server
+
''