Flake to setup a local env for atproto development
at main 6.4 kB view raw
1{ 2 description = "Simple Caddy Hello World with custom certificates"; 3 4 inputs = { 5 nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; 6 }; 7 8 outputs = { self, nixpkgs }: 9 let 10 system = "x86_64-linux"; 11 pkgs = nixpkgs.legacyPackages.${system}; 12 did-plc-server = pkgs.callPackage ./packages/did-method-plc.nix { }; 13 plc = pkgs.callPackage ./packages/plc.nix { inherit did-plc-server; }; 14 caddy-proxy = pkgs.callPackage ./packages/caddy.nix { }; 15 pds = pkgs.callPackage ./packages/pds.nix { }; 16 mailhog = pkgs.callPackage ./packages/mailhog.nix { }; 17 indigo-relay = pkgs.callPackage ./packages/indigo-relay.nix { }; 18 in 19 { 20 packages.${system} = { 21 22 plc = plc; 23 24 caddy-proxy = caddy-proxy; 25 26 pds = pds; 27 28 mailhog = mailhog; 29 30 indigo-relay = indigo-relay; 31 32 # Script to generate certificates on host 33 generate-certs = pkgs.writeShellScriptBin "generate-certs" '' 34 set -e 35 36 # Create certs directory 37 mkdir -p ./certs 38 cd ./certs 39 40 echo "Generating certificates with mkcert..." 41 42 # Generate wildcard certificate 43 ${pkgs.mkcert}/bin/mkcert \ 44 -cert-file cert.pem \ 45 -key-file key.pem \ 46 localhost \ 47 127.0.0.1 \ 48 ::1 \ 49 pds.example.org \ 50 plc.example.org \ 51 relay.example.org 52 53 echo "Certificates generated in ./certs/" 54 echo "Files created:" 55 ls -la . 56 ''; 57 58 # Script to start all services in tmux 59 all = pkgs.writeShellScriptBin "all" '' 60 set -e 61 62 # Check if tmux is available 63 if ! command -v tmux >/dev/null 2>&1; then 64 echo " tmux is not installed. Please install tmux first." 65 exit 1 66 fi 67 68 # Check if certificates exist 69 if [ ! -f "./certs/cert.pem" ]; then 70 echo " WARNING: SSL certificates not found. Run 'nix run .#generate-certs' first." 71 read -p "Continue anyway? (y/N): " -n 1 -r 72 echo 73 if [[ ! $REPLY =~ ^[Yy]$ ]]; then 74 exit 1 75 fi 76 fi 77 78 # Check if hosts file is configured 79 if ! grep -q "pds.example.org" /etc/hosts 2>/dev/null; then 80 echo " WARNING: Please add these lines to your /etc/hosts file:" 81 echo " 127.0.0.1 pds.example.org" 82 echo " 127.0.0.1 plc.example.org" 83 echo "" 84 fi 85 86 # Kill existing session if it exists 87 tmux kill-session -t atproto 2>/dev/null || true 88 89 echo "🚀 Starting AT Protocol services in tmux..." 90 91 # Create new tmux session with PLC server 92 tmux new-session -d -s atproto "${plc}/bin/plc" 93 94 # Split vertically for PDS server 95 tmux split-window -v -t atproto "${pds}/bin/pds" 96 97 # Split vertically for Caddy proxy 98 tmux split-window -v -t atproto "${caddy-proxy}/bin/caddy-proxy" 99 100 # Split vertically for Relay (with environment variables) 101 tmux split-window -v -t atproto " 102 export RELAY_ADMIN_PASSWORD=password 103 export RELAY_PLC_HOST=https://plc.example.org:8444 104 export RELAY_TRUSTED_DOMAINS=*.example.org 105 export RELAY_ALLOW_INSECURE_HOSTS=true 106 export RELAY_LOG_LEVEL=debug 107 export RELAY_DISABLE_SSRF=true 108 export RELAY_ALLOW_CUSTOM_PORTS=true 109 ${indigo-relay}/bin/relay serve 110 " 111 112 113 # Select the first pane 114 tmux select-pane -t atproto.0 115 116 echo " Services started in tmux session 'atproto'" 117 echo "" 118 echo "📋 Available commands:" 119 echo " tmux attach -t atproto - Attach to the session" 120 echo " tmux kill-session -t atproto - Stop all services" 121 echo "" 122 echo "📋 Panes layout:" 123 echo " Pane 0: PLC server" 124 echo " Pane 1: PDS server" 125 echo " Pane 2: Caddy proxy" 126 echo " Pane 3: AT Protocol Relay" 127 echo "" 128 echo "💡 Use Ctrl+b followed by arrow keys to switch between panes" 129 echo "💡 To monitor firehose: goat firehose --relay-host wss://relay.example.org:8445" 130 ''; 131 132 # Script to start relay with environment 133 relay = pkgs.writeShellScriptBin "relay" '' 134 set -e 135 136 echo "Starting AT Protocol Relay..." 137 echo "Admin password: password" 138 echo "PLC host: https://plc.example.org:8444" 139 echo "" 140 141 # Set relay environment variables 142 export RELAY_ADMIN_PASSWORD="password" 143 export RELAY_PLC_HOST="https://plc.example.org:8444" 144 export RELAY_TRUSTED_DOMAINS="*.example.org" 145 export RELAY_ALLOW_INSECURE_HOSTS="true" 146 export RELAY_LOG_LEVEL="debug" 147 export RELAY_DISABLE_SSRF="true" 148 export RELAY_ALLOW_CUSTOM_PORTS="true" 149 150 ${indigo-relay}/bin/relay serve 151 ''; 152 }; 153 154 # Development shell with tools (no automatic service management) 155 devShells.${system}.default = pkgs.mkShell { 156 buildInputs = with pkgs; [ 157 caddy 158 mkcert 159 curl 160 jq 161 bluesky-pds 162 openssl 163 mailhog 164 postgresql 165 atproto-goat 166 tmux 167 bash 168 ]; 169 170 shellHook = '' 171 echo "🚀 AT Protocol Development Environment" 172 echo "" 173 echo "🌐 Services will be available at:" 174 echo " Bluesky PDS: https://pds.example.org:8443" 175 echo " DID PLC: https://plc.example.org:8444" 176 echo " MailHog: http://localhost:8025" 177 echo "" 178 echo "🛠 Available tools: goat" 179 echo "" 180 echo "💡 Available packages:" 181 echo " nix run .#all - Start all services in tmux (recommended)" 182 echo " nix run .#plc - Start PLC server" 183 echo " nix run .#pds - Start PDS server" 184 echo " nix run .#caddy-proxy - Start Caddy proxy" 185 echo " nix run .#relay - Start AT Protocol Relay" 186 echo " nix run .#mailhog - Start MailHog" 187 echo " nix run .#generate-certs - Generate SSL certificates" 188 echo "" 189 echo "🚀 Quick start: nix run .#all" 190 echo " Note: You control when services start and stop" 191 echo "" 192 193 # Set custom prompt 194 export PS1='[AT Proto Dev] \u@\h:\w\$ ' 195 ''; 196 }; 197 }; 198}