Flake to setup a local env for atproto development
1{ pkgs }:
2
3pkgs.writeShellScriptBin "caddy-proxy" ''
4 set -e
5
6 # Default values
7 CERT_DIR="./certs"
8 CADDYFILE="./Caddyfile"
9
10 # Parse arguments
11 while [[ $# -gt 0 ]]; do
12 case $1 in
13 --cert-dir)
14 CERT_DIR="$2"
15 shift 2
16 ;;
17 --caddyfile)
18 CADDYFILE="$2"
19 shift 2
20 ;;
21 --help|-h)
22 echo "Usage: $0 [--cert-dir <directory>] [--caddyfile <file>]"
23 echo ""
24 echo "Options:"
25 echo " --cert-dir <dir> Directory containing certificates (default: ./certs)"
26 echo " --caddyfile <file> Path to Caddyfile (default: ./Caddyfile)"
27 echo " --help, -h Show this help message"
28 echo ""
29 echo "The certificate directory should contain:"
30 echo " - cert.pem (certificate file)"
31 echo " - key.pem (private key file)"
32 echo ""
33 echo "Examples:"
34 echo " $0 # Use ./certs and ./Caddyfile"
35 echo " $0 --cert-dir ~/my-certs # Custom cert directory"
36 echo " $0 --caddyfile ~/my-caddy/Caddyfile # Custom Caddyfile"
37 echo " $0 --cert-dir ~/certs --caddyfile ./conf/Caddyfile"
38 exit 0
39 ;;
40 *)
41 echo "Unknown option: $1"
42 exit 1
43 ;;
44 esac
45 done
46
47 # Convert to absolute paths
48 CERT_DIR=$(realpath "$CERT_DIR")
49 CADDYFILE=$(realpath "$CADDYFILE")
50
51 # Check if Caddyfile exists
52 if [ ! -f "$CADDYFILE" ]; then
53 echo "ERROR: Caddyfile not found: $CADDYFILE"
54 echo "Create a Caddyfile or use: nix run .#generate-caddyfile"
55 exit 1
56 fi
57
58 # Check if certificate directory exists
59 if [ ! -d "$CERT_DIR" ]; then
60 echo "ERROR: Certificate directory does not exist: $CERT_DIR"
61 echo "Please create the directory and add your certificates."
62 exit 1
63 fi
64
65 # Check for required certificates
66 if [ ! -f "$CERT_DIR/cert.pem" ]; then
67 echo "ERROR: Missing cert.pem in $CERT_DIR"
68 exit 1
69 fi
70
71 if [ ! -f "$CERT_DIR/key.pem" ]; then
72 echo "ERROR: Missing key.pem in $CERT_DIR"
73 exit 1
74 fi
75
76 echo "Starting Caddy..."
77 echo "Caddyfile: $CADDYFILE"
78 echo "Certificates: $CERT_DIR"
79 echo "Press Ctrl+C to stop"
80 echo ""
81
82 # Set environment variables that can be used in Caddyfile
83 export CERT_DIR
84 export CERT_FILE="$CERT_DIR/cert.pem"
85 export KEY_FILE="$CERT_DIR/key.pem"
86
87 # Run Caddy with the specified Caddyfile
88 ${pkgs.caddy}/bin/caddy run --config "$CADDYFILE"
89''