Flake to setup a local env for atproto development
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 in 18 { 19 packages.${system} = { 20 21 plc = plc; 22 23 caddy-proxy = caddy-proxy; 24 25 pds = pds; 26 27 mailhog = mailhog; 28 29 # Script to generate certificates on host 30 generate-certs = pkgs.writeShellScriptBin "generate-certs" '' 31 set -e 32 33 # Create certs directory 34 mkdir -p ./certs 35 cd ./certs 36 37 echo "Generating certificates with mkcert..." 38 39 # Generate wildcard certificate 40 ${pkgs.mkcert}/bin/mkcert \ 41 -cert-file cert.pem \ 42 -key-file key.pem \ 43 localhost \ 44 127.0.0.1 \ 45 ::1 \ 46 pds.example.org \ 47 plc.example.org 48 49 echo "Certificates generated in ./certs/" 50 echo "Files created:" 51 ls -la . 52 ''; 53 54 # Script to start all services in tmux 55 all = pkgs.writeShellScriptBin "all" '' 56 set -e 57 58 # Check if tmux is available 59 if ! command -v tmux >/dev/null 2>&1; then 60 echo " tmux is not installed. Please install tmux first." 61 exit 1 62 fi 63 64 # Check if certificates exist 65 if [ ! -f "./certs/cert.pem" ]; then 66 echo " WARNING: SSL certificates not found. Run 'nix run .#generate-certs' first." 67 read -p "Continue anyway? (y/N): " -n 1 -r 68 echo 69 if [[ ! $REPLY =~ ^[Yy]$ ]]; then 70 exit 1 71 fi 72 fi 73 74 # Check if hosts file is configured 75 if ! grep -q "pds.example.org" /etc/hosts 2>/dev/null; then 76 echo " WARNING: Please add these lines to your /etc/hosts file:" 77 echo " 127.0.0.1 pds.example.org" 78 echo " 127.0.0.1 plc.example.org" 79 echo "" 80 fi 81 82 # Kill existing session if it exists 83 tmux kill-session -t atproto 2>/dev/null || true 84 85 echo "🚀 Starting AT Protocol services in tmux..." 86 87 # Create new tmux session with PLC server 88 tmux new-session -d -s atproto "${plc}/bin/plc" 89 90 # Split horizontally for PDS server 91 tmux split-window -h -t atproto "${pds}/bin/pds" 92 93 # Split the right pane vertically for Caddy proxy 94 tmux split-window -v -t atproto.1 "${caddy-proxy}/bin/caddy-proxy" 95 96 # Split the left pane vertically for MailHog 97 tmux split-window -v -t atproto.0 "${mailhog}/bin/mailhog" 98 99 # Select the first pane 100 tmux select-pane -t atproto.0 101 102 echo " Services started in tmux session 'atproto'" 103 echo "" 104 echo "📋 Available commands:" 105 echo " tmux attach -t atproto - Attach to the session" 106 echo " tmux kill-session -t atproto - Stop all services" 107 echo "" 108 echo "🔲 Panes layout (2x2 grid):" 109 echo " Top-left: PLC server" 110 echo " Bottom-left: MailHog server" 111 echo " Top-right: PDS server" 112 echo " Bottom-right: Caddy proxy" 113 echo "" 114 echo "💡 Use Ctrl+b followed by arrow keys to switch between panes" 115 ''; 116 }; 117 118 # Development shell with tools (no automatic service management) 119 devShells.${system}.default = pkgs.mkShell { 120 buildInputs = with pkgs; [ 121 caddy 122 mkcert 123 curl 124 jq 125 bluesky-pds 126 openssl 127 mailhog 128 postgresql 129 atproto-goat 130 tmux 131 bash 132 ]; 133 134 shellHook = '' 135 echo "🚀 AT Protocol Development Environment" 136 echo "" 137 echo "🌐 Services will be available at:" 138 echo " Bluesky PDS: https://pds.example.org:8443" 139 echo " DID PLC: https://plc.example.org:8444" 140 echo " MailHog: http://localhost:8025" 141 echo "" 142 echo "🛠 Available tools: goat" 143 echo "" 144 echo "💡 Available packages:" 145 echo " nix run .#all - Start all services in tmux (recommended)" 146 echo " nix run .#plc - Start PLC server" 147 echo " nix run .#pds - Start PDS server" 148 echo " nix run .#caddy-proxy - Start Caddy proxy" 149 echo " nix run .#mailhog - Start MailHog" 150 echo " nix run .#generate-certs - Generate SSL certificates" 151 echo "" 152 echo "🚀 Quick start: nix run .#all" 153 echo " Note: You control when services start and stop" 154 echo "" 155 156 # Set custom prompt 157 export PS1='[AT Proto Dev] \u@\h:\w\$ ' 158 ''; 159 }; 160 }; 161}