{ 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 ] [--caddyfile ]" echo "" echo "Options:" echo " --cert-dir Directory containing certificates (default: ./certs)" echo " --caddyfile 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" ''