Flake to setup a local env for atproto development

New organization per file

edouard.paris 922321b7 c745cdc2

verified
Changed files
+142 -135
packages
+6 -135
flake.nix
···
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
did-plc-server = pkgs.callPackage ./packages/did-method-plc.nix { };
+
caddy-proxy = pkgs.callPackage ./packages/caddy.nix { };
+
pds = pkgs.callPackage ./packages/pds.nix { };
in
{
packages.${system} = {
did-plc-server = did-plc-server;
+
+
caddy-proxy = caddy-proxy;
+
+
pds = pds;
# Script to generate certificates on host
generate-certs = pkgs.writeShellScriptBin "generate-certs" ''
···
echo "Certificates generated in ./certs/"
echo "Files created:"
ls -la .
-
'';
-
-
caddy-proxy = pkgs.writeShellScriptBin "caddy-proxy" ''
-
set -e
-
-
# Default values
-
CERT_DIR="./certs"
-
CADDYFILE="./Caddyfile"
-
-
# Parse arguments
-
while [[ $# -gt 0 ]]; do
-
case $1 in
-
--cert-dir)
-
CERT_DIR="$2"
-
shift 2
-
;;
-
--caddyfile)
-
CADDYFILE="$2"
-
shift 2
-
;;
-
--help|-h)
-
echo "Usage: $0 [--cert-dir <directory>] [--caddyfile <file>]"
-
echo ""
-
echo "Options:"
-
echo " --cert-dir <dir> Directory containing certificates (default: ./certs)"
-
echo " --caddyfile <file> Path to Caddyfile (default: ./Caddyfile)"
-
echo " --help, -h Show this help message"
-
echo ""
-
echo "The certificate directory should contain:"
-
echo " - cert.pem (certificate file)"
-
echo " - key.pem (private key file)"
-
echo ""
-
echo "Examples:"
-
echo " $0 # Use ./certs and ./Caddyfile"
-
echo " $0 --cert-dir ~/my-certs # Custom cert directory"
-
echo " $0 --caddyfile ~/my-caddy/Caddyfile # Custom Caddyfile"
-
echo " $0 --cert-dir ~/certs --caddyfile ./conf/Caddyfile"
-
exit 0
-
;;
-
*)
-
echo "Unknown option: $1"
-
exit 1
-
;;
-
esac
-
done
-
-
# Convert to absolute paths
-
CERT_DIR=$(realpath "$CERT_DIR")
-
CADDYFILE=$(realpath "$CADDYFILE")
-
-
# Check if Caddyfile exists
-
if [ ! -f "$CADDYFILE" ]; then
-
echo "ERROR: Caddyfile not found: $CADDYFILE"
-
echo "Create a Caddyfile or use: nix run .#generate-caddyfile"
-
exit 1
-
fi
-
-
# Check if certificate directory exists
-
if [ ! -d "$CERT_DIR" ]; then
-
echo "ERROR: Certificate directory does not exist: $CERT_DIR"
-
echo "Please create the directory and add your certificates."
-
exit 1
-
fi
-
-
# Check for required certificates
-
if [ ! -f "$CERT_DIR/cert.pem" ]; then
-
echo "ERROR: Missing cert.pem in $CERT_DIR"
-
exit 1
-
fi
-
-
if [ ! -f "$CERT_DIR/key.pem" ]; then
-
echo "ERROR: Missing key.pem in $CERT_DIR"
-
exit 1
-
fi
-
-
echo "Starting Caddy..."
-
echo "Caddyfile: $CADDYFILE"
-
echo "Certificates: $CERT_DIR"
-
echo "Press Ctrl+C to stop"
-
echo ""
-
-
# Set environment variables that can be used in Caddyfile
-
export CERT_DIR
-
export CERT_FILE="$CERT_DIR/cert.pem"
-
export KEY_FILE="$CERT_DIR/key.pem"
-
-
# Run Caddy with the specified Caddyfile
-
${pkgs.caddy}/bin/caddy run --config "$CADDYFILE"
-
'';
-
-
# Script to start bluesky-pds
-
pds = pkgs.writeShellScriptBin "pds" ''
-
set -e
-
-
# Default port for bluesky-pds
-
PORT=''${BLUESKY_PDS_PORT:-3000}
-
-
# Create data directories
-
mkdir -p ./data/pds/blocks
-
mkdir -p ./data/pds/db
-
-
echo "Starting Bluesky PDS on port $PORT..."
-
echo "Data directory: ./data/pds"
-
-
# Configure for local disk storage
-
export PDS_DATA_DIRECTORY="./data/pds"
-
export PDS_BLOBSTORE_DISK_LOCATION="./data/pds/blocks"
-
export PDS_DB_SQLITE_LOCATION="./data/pds/db/pds.sqlite"
-
export PDS_PORT="$PORT"
-
-
# Generate or use existing PLC rotation key
-
if [ ! -f "./data/pds/plc-rotation-key.txt" ]; then
-
echo "Generating PLC rotation key..."
-
openssl rand -hex 32 > ./data/pds/plc-rotation-key.txt
-
fi
-
export PDS_PLC_ROTATION_KEY_K256_PRIVATE_KEY_HEX="$(cat ./data/pds/plc-rotation-key.txt)"
-
-
# Generate or use existing JWT secret
-
if [ ! -f "./data/pds/jwt-secret.txt" ]; then
-
echo "Generating JWT secret..."
-
openssl rand -hex 32 > ./data/pds/jwt-secret.txt
-
fi
-
export PDS_JWT_SECRET="$(cat ./data/pds/jwt-secret.txt)"
-
-
# Set admin password
-
export PDS_ADMIN_PASSWORD="admin"
-
-
# Set hostname and URL scheme (using example.org domain)
-
export PDS_HOSTNAME="pds.example.org:8443"
-
export PDS_SERVICE_URL="https://pds.example.org:8443"
-
-
# Enable development mode
-
export PDS_DEV_MODE="true"
-
-
${pkgs.bluesky-pds}/bin/pds
'';
};
+89
packages/caddy.nix
···
+
{ pkgs }:
+
+
pkgs.writeShellScriptBin "caddy-proxy" ''
+
set -e
+
+
# Default values
+
CERT_DIR="./certs"
+
CADDYFILE="./Caddyfile"
+
+
# Parse arguments
+
while [[ $# -gt 0 ]]; do
+
case $1 in
+
--cert-dir)
+
CERT_DIR="$2"
+
shift 2
+
;;
+
--caddyfile)
+
CADDYFILE="$2"
+
shift 2
+
;;
+
--help|-h)
+
echo "Usage: $0 [--cert-dir <directory>] [--caddyfile <file>]"
+
echo ""
+
echo "Options:"
+
echo " --cert-dir <dir> Directory containing certificates (default: ./certs)"
+
echo " --caddyfile <file> Path to Caddyfile (default: ./Caddyfile)"
+
echo " --help, -h Show this help message"
+
echo ""
+
echo "The certificate directory should contain:"
+
echo " - cert.pem (certificate file)"
+
echo " - key.pem (private key file)"
+
echo ""
+
echo "Examples:"
+
echo " $0 # Use ./certs and ./Caddyfile"
+
echo " $0 --cert-dir ~/my-certs # Custom cert directory"
+
echo " $0 --caddyfile ~/my-caddy/Caddyfile # Custom Caddyfile"
+
echo " $0 --cert-dir ~/certs --caddyfile ./conf/Caddyfile"
+
exit 0
+
;;
+
*)
+
echo "Unknown option: $1"
+
exit 1
+
;;
+
esac
+
done
+
+
# Convert to absolute paths
+
CERT_DIR=$(realpath "$CERT_DIR")
+
CADDYFILE=$(realpath "$CADDYFILE")
+
+
# Check if Caddyfile exists
+
if [ ! -f "$CADDYFILE" ]; then
+
echo "ERROR: Caddyfile not found: $CADDYFILE"
+
echo "Create a Caddyfile or use: nix run .#generate-caddyfile"
+
exit 1
+
fi
+
+
# Check if certificate directory exists
+
if [ ! -d "$CERT_DIR" ]; then
+
echo "ERROR: Certificate directory does not exist: $CERT_DIR"
+
echo "Please create the directory and add your certificates."
+
exit 1
+
fi
+
+
# Check for required certificates
+
if [ ! -f "$CERT_DIR/cert.pem" ]; then
+
echo "ERROR: Missing cert.pem in $CERT_DIR"
+
exit 1
+
fi
+
+
if [ ! -f "$CERT_DIR/key.pem" ]; then
+
echo "ERROR: Missing key.pem in $CERT_DIR"
+
exit 1
+
fi
+
+
echo "Starting Caddy..."
+
echo "Caddyfile: $CADDYFILE"
+
echo "Certificates: $CERT_DIR"
+
echo "Press Ctrl+C to stop"
+
echo ""
+
+
# Set environment variables that can be used in Caddyfile
+
export CERT_DIR
+
export CERT_FILE="$CERT_DIR/cert.pem"
+
export KEY_FILE="$CERT_DIR/key.pem"
+
+
# Run Caddy with the specified Caddyfile
+
${pkgs.caddy}/bin/caddy run --config "$CADDYFILE"
+
''
+47
packages/pds.nix
···
+
{ pkgs }:
+
+
pkgs.writeShellScriptBin "pds" ''
+
set -e
+
+
# Default port for bluesky-pds
+
PORT=''${BLUESKY_PDS_PORT:-3000}
+
+
# Create data directories
+
mkdir -p ./data/pds/blocks
+
mkdir -p ./data/pds/db
+
+
echo "Starting Bluesky PDS on port $PORT..."
+
echo "Data directory: ./data/pds"
+
+
# Configure for local disk storage
+
export PDS_DATA_DIRECTORY="./data/pds"
+
export PDS_BLOBSTORE_DISK_LOCATION="./data/pds/blocks"
+
export PDS_DB_SQLITE_LOCATION="./data/pds/db/pds.sqlite"
+
export PDS_PORT="$PORT"
+
+
# Generate or use existing PLC rotation key
+
if [ ! -f "./data/pds/plc-rotation-key.txt" ]; then
+
echo "Generating PLC rotation key..."
+
${pkgs.openssl}/bin/openssl rand -hex 32 > ./data/pds/plc-rotation-key.txt
+
fi
+
export PDS_PLC_ROTATION_KEY_K256_PRIVATE_KEY_HEX="$(cat ./data/pds/plc-rotation-key.txt)"
+
+
# Generate or use existing JWT secret
+
if [ ! -f "./data/pds/jwt-secret.txt" ]; then
+
echo "Generating JWT secret..."
+
${pkgs.openssl}/bin/openssl rand -hex 32 > ./data/pds/jwt-secret.txt
+
fi
+
export PDS_JWT_SECRET="$(cat ./data/pds/jwt-secret.txt)"
+
+
# Set admin password
+
export PDS_ADMIN_PASSWORD="admin"
+
+
# Set hostname and URL scheme (using example.org domain)
+
export PDS_HOSTNAME="pds.example.org:8443"
+
export PDS_SERVICE_URL="https://pds.example.org:8443"
+
+
# Enable development mode
+
export PDS_DEV_MODE="true"
+
+
${pkgs.bluesky-pds}/bin/pds
+
''