Flake to setup a local env for atproto development

Add plc server and caddy proxy

edouard.paris 65e74fc2

verified
+1
.gitignore
···
···
+
certs
+17
Caddyfile
···
···
+
{
+
auto_https off
+
}
+
+
localhost:8443 {
+
tls ./certs/cert.pem ./certs/key.pem
+
+
header Content-Type "text/plain"
+
respond "Hello World!" 200
+
}
+
+
localhost:8444 {
+
tls ./certs/cert.pem ./certs/key.pem
+
+
header Content-Type "text/plain"
+
respond "Hello API!" 200
+
}
+27
flake.lock
···
···
+
{
+
"nodes": {
+
"nixpkgs": {
+
"locked": {
+
"lastModified": 1758427187,
+
"narHash": "sha256-pHpxZ/IyCwoTQPtFIAG2QaxuSm8jWzrzBGjwQZIttJc=",
+
"owner": "NixOS",
+
"repo": "nixpkgs",
+
"rev": "554be6495561ff07b6c724047bdd7e0716aa7b46",
+
"type": "github"
+
},
+
"original": {
+
"owner": "NixOS",
+
"ref": "nixos-unstable",
+
"repo": "nixpkgs",
+
"type": "github"
+
}
+
},
+
"root": {
+
"inputs": {
+
"nixpkgs": "nixpkgs"
+
}
+
}
+
},
+
"root": "root",
+
"version": 7
+
}
+148
flake.nix
···
···
+
{
+
description = "Simple Caddy Hello World with custom certificates";
+
+
inputs = {
+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
+
};
+
+
outputs = { self, nixpkgs }:
+
let
+
system = "x86_64-linux";
+
pkgs = nixpkgs.legacyPackages.${system};
+
did-plc-server = pkgs.callPackage ./packages/did-method-plc.nix { };
+
in
+
{
+
packages.${system} = {
+
+
did-plc-server = did-plc-server;
+
+
# Script to generate certificates on host
+
generate-certs = pkgs.writeShellScriptBin "generate-certs" ''
+
set -e
+
+
# Create certs directory
+
mkdir -p ./certs
+
cd ./certs
+
+
echo "Generating certificates with mkcert..."
+
+
# Generate wildcard certificate
+
${pkgs.mkcert}/bin/mkcert \
+
-cert-file cert.pem \
+
-key-file key.pem \
+
localhost \
+
127.0.0.1 \
+
::1
+
+
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"
+
'';
+
};
+
+
# Development shell
+
devShells.${system}.default = pkgs.mkShell {
+
buildInputs = with pkgs; [
+
caddy
+
mkcert
+
curl
+
];
+
+
shellHook = ''
+
echo "Caddy development environment"
+
echo "Available commands:"
+
echo " nix run .#generate-certs - Generate test certificates"
+
echo " nix run .#caddy-proxy - Start Caddy with full config"
+
echo " nix run .#caddy-oneliner - Start Caddy with minimal config"
+
'';
+
};
+
};
+
}
+65
packages/did-method-plc.nix
···
···
+
{
+
stdenv,
+
makeBinaryWrapper,
+
pnpm_9,
+
fetchgit,
+
nodejs,
+
lib,
+
}:
+
+
stdenv.mkDerivation (finalAttrs: {
+
pname = "did-method-plc";
+
version = "0.1.0";
+
+
src = fetchgit {
+
url = "https://tangled.org/@edouard.paris/did-method-plc";
+
# rev = "migrate-to-pnpm";
+
hash = "sha256-KewRzr0DwCdB4lqpAC5A82Vd7Y9fmRyXwoc2i23Cr+g=";
+
};
+
+
sourceRoot = "${finalAttrs.src.name}";
+
+
nativeBuildInputs = [
+
makeBinaryWrapper
+
nodejs
+
pnpm_9.configHook
+
];
+
+
pnpmDeps = pnpm_9.fetchDeps {
+
inherit (finalAttrs) pname version src sourceRoot;
+
fetcherVersion = 1;
+
hash = "sha256-cGS8adYh70urpxQEq3ipl7cgSGNlu5MDdXz/qefakNE=";
+
};
+
+
buildPhase = ''
+
runHook preBuild
+
# Build if needed
+
pnpm --filter @did-plc/server build || true
+
runHook postBuild
+
'';
+
+
installPhase = ''
+
runHook preInstall
+
+
mkdir -p $out/{bin,share/did-plc-server}
+
+
# Copy the source and dependencies
+
cp -r . $out/share/did-plc-server/
+
+
# Create the executable wrapper
+
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" \
+
--add-flags "--filter" \
+
--add-flags "@did-plc/server" \
+
--add-flags "start"
+
+
runHook postInstall
+
'';
+
+
meta = {
+
description = "DID PLC Server from monorepo";
+
mainProgram = "did-plc-server";
+
};
+
})