From 4301518f60f480af7b59c79fd562bc236c315645 Mon Sep 17 00:00:00 2001 From: Marc Sanchis Date: Sun, 8 Jun 2025 17:34:07 +0200 Subject: [PATCH] chore: enhance development environment with Makefile and improved shell.nix --- Makefile | 158 +++++++++++++++++++++++++++++++++++++ shell.nix | 229 ++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 330 insertions(+), 57 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..928f9e100 --- /dev/null +++ b/Makefile @@ -0,0 +1,158 @@ +# Hyperloop H10 Control Station Makefile + +.PHONY: all install clean build-backend build-common-front build-control-station build-ethernet-view +.PHONY: backend common-front control-station ethernet-view +.PHONY: dev-backend dev-control-station dev-ethernet-view +.PHONY: test test-backend test-frontend +.PHONY: ethernet-view-tmux control-station-tmux + +# Colors for output +GREEN := \033[0;32m +YELLOW := \033[0;33m +BLUE := \033[0;34m +RED := \033[0;31m +NC := \033[0m # No Color + +# Build directories +BACKEND_DIR := backend +COMMON_FRONT_DIR := common-front +CONTROL_STATION_DIR := control-station +ETHERNET_VIEW_DIR := ethernet-view + +# Output binary +BACKEND_BIN := $(BACKEND_DIR)/cmd/backend + +# Default target +all: install build + +# Install all dependencies +install: install-backend install-frontend + @echo "$(GREEN)✓ All dependencies installed$(NC)" + +install-backend: + @echo "$(BLUE)Installing backend dependencies...$(NC)" + @cd $(BACKEND_DIR) && go mod download + @echo "$(GREEN)✓ Backend dependencies installed$(NC)" + +install-frontend: + @echo "$(BLUE)Installing frontend dependencies...$(NC)" + @cd $(COMMON_FRONT_DIR) && npm install + @cd $(CONTROL_STATION_DIR) && npm install + @cd $(ETHERNET_VIEW_DIR) && npm install + @echo "$(GREEN)✓ Frontend dependencies installed$(NC)" + +# Build all components +build: build-backend build-frontend + @echo "$(GREEN)✓ All components built successfully$(NC)" + +build-frontend: build-common-front build-control-station build-ethernet-view + +# Individual build targets +backend build-backend: + @echo "$(BLUE)Building backend...$(NC)" + @cd $(BACKEND_DIR)/cmd && go build -o backend + @echo "$(GREEN)✓ Backend built: $(BACKEND_BIN)$(NC)" + +common-front build-common-front: + @echo "$(BLUE)Building common-front...$(NC)" + @cd $(COMMON_FRONT_DIR) && npm run build + @echo "$(GREEN)✓ Common-front built$(NC)" + +control-station build-control-station: build-common-front + @echo "$(BLUE)Building control-station...$(NC)" + @cd $(CONTROL_STATION_DIR) && npm run build + @echo "$(GREEN)✓ Control-station built$(NC)" + +ethernet-view build-ethernet-view: build-common-front + @echo "$(BLUE)Building ethernet-view...$(NC)" + @cd $(ETHERNET_VIEW_DIR) && npm run build + @echo "$(GREEN)✓ Ethernet-view built$(NC)" + +# Development servers (individual) +dev-backend: + @echo "$(YELLOW)Starting backend development server...$(NC)" + @cd $(BACKEND_DIR)/cmd && ./backend + +dev-control-station: + @echo "$(YELLOW)Starting control-station development server...$(NC)" + @cd $(CONTROL_STATION_DIR) && npm run dev + +dev-ethernet-view: + @echo "$(YELLOW)Starting ethernet-view development server...$(NC)" + @cd $(ETHERNET_VIEW_DIR) && npm run dev + +# Testing +test: test-backend test-frontend + +test-backend: + @echo "$(BLUE)Running backend tests...$(NC)" + @cd $(BACKEND_DIR) && go test -v -timeout 30s ./... + +test-frontend: + @echo "$(BLUE)Running frontend tests...$(NC)" + @cd $(ETHERNET_VIEW_DIR) && npm test || true + @echo "$(YELLOW)Note: Only ethernet-view has tests configured$(NC)" + +# Clean build artifacts +clean: + @echo "$(YELLOW)Cleaning build artifacts...$(NC)" + @rm -f $(BACKEND_BIN) + @rm -rf $(COMMON_FRONT_DIR)/dist + @rm -rf $(CONTROL_STATION_DIR)/dist + @rm -rf $(ETHERNET_VIEW_DIR)/dist + @echo "$(GREEN)✓ Clean complete$(NC)" + +# Combined tmux sessions +ethernet-view-tmux: build-backend + @echo "$(BLUE)Starting backend + ethernet-view in tmux...$(NC)" + @tmux new-session -d -s ethernet-view-session -n main + @tmux send-keys -t ethernet-view-session:main "cd $(BACKEND_DIR)/cmd && ./backend" C-m + @tmux split-window -t ethernet-view-session:main -h + @tmux send-keys -t ethernet-view-session:main.1 "cd $(ETHERNET_VIEW_DIR) && npm run dev" C-m + @tmux select-pane -t ethernet-view-session:main.0 + @tmux attach-session -t ethernet-view-session + +control-station-tmux: build-backend + @echo "$(BLUE)Starting backend + control-station in tmux...$(NC)" + @tmux new-session -d -s control-station-session -n main + @tmux send-keys -t control-station-session:main "cd $(BACKEND_DIR)/cmd && ./backend" C-m + @tmux split-window -t control-station-session:main -h + @tmux send-keys -t control-station-session:main.1 "cd $(CONTROL_STATION_DIR) && npm run dev" C-m + @tmux select-pane -t control-station-session:main.0 + @tmux attach-session -t control-station-session + +# Help target +help: + @echo "Hyperloop H10 Control Station - Build System" + @echo "===========================================" + @echo "" + @echo "$(YELLOW)Installation:$(NC)" + @echo " make install - Install all dependencies" + @echo " make install-backend - Install backend dependencies only" + @echo " make install-frontend - Install frontend dependencies only" + @echo "" + @echo "$(YELLOW)Building:$(NC)" + @echo " make all - Install deps and build everything" + @echo " make build - Build all components" + @echo " make backend - Build backend only" + @echo " make common-front - Build common frontend library" + @echo " make control-station - Build control station" + @echo " make ethernet-view - Build ethernet view" + @echo "" + @echo "$(YELLOW)Development:$(NC)" + @echo " make dev-backend - Run backend dev server" + @echo " make dev-control-station - Run control station dev server" + @echo " make dev-ethernet-view - Run ethernet view dev server" + @echo "" + @echo "$(YELLOW)Combined Sessions:$(NC)" + @echo " make ethernet-view-tmux - Run backend + ethernet-view in tmux" + @echo " make control-station-tmux - Run backend + control-station in tmux" + @echo "" + @echo "$(YELLOW)Testing:$(NC)" + @echo " make test - Run all tests" + @echo " make test-backend - Run backend tests" + @echo " make test-frontend - Run frontend tests" + @echo "" + @echo "$(YELLOW)Maintenance:$(NC)" + @echo " make clean - Remove build artifacts" + @echo " make help - Show this help message" \ No newline at end of file diff --git a/shell.nix b/shell.nix index be513d83a..a18bab877 100644 --- a/shell.nix +++ b/shell.nix @@ -1,85 +1,200 @@ { pkgs ? import {} }: -pkgs.mkShell { +let + # Pin nixpkgs for reproducibility + pinnedPkgs = import (builtins.fetchTarball { + url = "https://github.com/NixOS/nixpkgs/archive/nixos-23.11.tar.gz"; + sha256 = "1ndiv385w1qyb3b18vw13991fzb9wg4cl21wglk89grsfsnra41k"; + }) {}; + + # Use pinned packages by default + finalPkgs = if pkgs == import {} then pinnedPkgs else pkgs; + +in finalPkgs.mkShell { name = "hyperloop-h10-dev"; - - buildInputs = with pkgs; [ + + buildInputs = with finalPkgs; [ # Go development go gotools gopls - + delve + # Node.js development - nodejs + nodejs_20 nodePackages.npm nodePackages.typescript - - # Python for testadj + nodePackages.typescript-language-server + + # Python for testing python3 - + # System dependencies libpcap pkg-config - + # Build tools gnumake gcc - - # Utilities + + # Development utilities git ripgrep jq curl wget - - # Shell enhancements - starship - eza - bat - fzf + tmux + + # Optional productivity tools (can be removed for minimal setup) + watchman ]; - + shellHook = '' - echo "🚄 Hyperloop H10 Development Environment" - echo "📁 Repository root: $(pwd)" + # Hyperloop H10 Development Environment + + # Set up environment variables + export GOPATH="$HOME/go" + export PATH="$GOPATH/bin:$PATH" + export NODE_ENV="development" + export CGO_ENABLED=1 + + # Create command functions for running services in tmux + ethernet-view() { + # Check if backend is built + if [ ! -f "backend/cmd/backend" ]; then + echo "Building backend first..." + make backend + fi + + # Kill existing session if it exists + tmux kill-session -t ethernet-view 2>/dev/null || true + + # Create new session + tmux new-session -d -s ethernet-view -n main + + # Start backend in first pane + tmux send-keys -t ethernet-view:main "cd backend/cmd && ./backend" C-m + + # Split horizontally and start ethernet-view + tmux split-window -t ethernet-view:main -h -p 50 + tmux send-keys -t ethernet-view:main.1 "cd ethernet-view && npm run dev" C-m + + # Add status pane at bottom + tmux split-window -t ethernet-view:main.0 -v -p 20 + tmux send-keys -t ethernet-view:main.2 "echo 'Logs/Status - Press Enter for shell'; read; bash" C-m + + # Configure pane titles + tmux select-pane -t ethernet-view:main.0 -T "Backend" + tmux select-pane -t ethernet-view:main.1 -T "Ethernet View (http://localhost:5174)" + tmux select-pane -t ethernet-view:main.2 -T "Logs/Shell" + tmux set-option -t ethernet-view pane-border-status top + + echo "Starting ethernet-view session..." + echo "Backend running on configured port" + echo "Ethernet View: http://localhost:5174" + + # Attach to session + tmux attach-session -t ethernet-view + } + + control-station() { + # Check if backend is built + if [ ! -f "backend/cmd/backend" ]; then + echo "Building backend first..." + make backend + fi + + # Kill existing session if it exists + tmux kill-session -t control-station 2>/dev/null || true + + # Create new session + tmux new-session -d -s control-station -n main + + # Start backend in first pane + tmux send-keys -t control-station:main "cd backend/cmd && ./backend" C-m + + # Split horizontally and start control-station + tmux split-window -t control-station:main -h -p 50 + tmux send-keys -t control-station:main.1 "cd control-station && npm run dev" C-m + + # Add status pane at bottom + tmux split-window -t control-station:main.0 -v -p 20 + tmux send-keys -t control-station:main.2 "echo 'Logs/Status - Press Enter for shell'; read; bash" C-m + + # Configure pane titles + tmux select-pane -t control-station:main.0 -T "Backend" + tmux select-pane -t control-station:main.1 -T "Control Station (http://localhost:5173)" + tmux select-pane -t control-station:main.2 -T "Logs/Shell" + tmux set-option -t control-station pane-border-status top + + echo "Starting control-station session..." + echo "Backend running on configured port" + echo "Control Station: http://localhost:5173" + + # Attach to session + tmux attach-session -t control-station + } + + # Export functions so they're available in the shell + export -f ethernet-view + export -f control-station + + # Ensure clean terminal state + clear + + # Check if dependencies need installation + check_dependencies() { + local install_needed=false + + if [ ! -d "common-front/node_modules" ]; then + echo "⚠️ Missing common-front dependencies" + install_needed=true + fi + + if [ ! -d "control-station/node_modules" ]; then + echo "⚠️ Missing control-station dependencies" + install_needed=true + fi + + if [ ! -d "ethernet-view/node_modules" ]; then + echo "⚠️ Missing ethernet-view dependencies" + install_needed=true + fi + + if [ "$install_needed" = true ]; then + echo "" + echo "Run 'make install' to install all dependencies" + fi + } + + # Display environment info + echo "Hyperloop H10 Development Environment" + echo "=====================================" + echo "" + echo "Environment:" + echo " Go version: $(go version | cut -d' ' -f3)" + echo " Node version: $(node --version)" + echo " NPM version: $(npm --version)" echo "" - echo "Available commands:" - echo " make all - Build all components" - echo " make backend - Build Go backend" - echo " make common-front - Build shared component library" - echo " make control-station - Build main control interface" - echo " make ethernet-view - Build network monitoring interface" + echo "Quick Start:" + echo " make install - Install all dependencies" + echo " make all - Build all components" + echo " ethernet-view - Run backend + Ethernet view in tmux" + echo " control-station - Run backend + Control station in tmux" echo "" - echo "Development servers:" - echo " cd backend/cmd && ./backend - Run backend (after building)" - echo " cd control-station && npm run dev - Run control station" - echo " cd ethernet-view && npm run dev - Run ethernet view" - echo " cd packet-sender && go run . - Run packet sender" + echo "Individual Commands:" + echo " make backend - Build backend" + echo " make common-front - Build common frontend library" + echo " make control-station - Build control station" + echo " make ethernet-view - Build ethernet view" echo "" - - # Setup Node modules if needed - if [ ! -d "common-front/node_modules" ]; then - echo "📦 Installing common-front dependencies..." - (cd common-front && npm install) - fi - - if [ ! -d "ethernet-view/node_modules" ]; then - echo "📦 Installing ethernet-view dependencies..." - (cd ethernet-view && npm install) - fi - - if [ ! -d "control-station/node_modules" ]; then - echo "📦 Installing control-station dependencies..." - (cd control-station && npm install) - fi - - # Set up starship prompt if available - if command -v starship >/dev/null 2>&1; then - eval "$(starship init bash)" - fi + + check_dependencies ''; - - # Environment variables - GOPATH = "$HOME/go"; - NODE_ENV = "development"; -} \ No newline at end of file + + # Prevent impurities + pure = true; + + # Additional environment variables for pure builds + NIX_ENFORCE_PURITY = 1; +}