-
-
Notifications
You must be signed in to change notification settings - Fork 2
BUILD_ARCHITECTURE
Modular Build System for nself - Comprehensive Guide
This document covers the completely refactored modular build system in nself, which replaced the monolithic 1300-line build.sh script with maintainable, testable modules.
- Overview
- Architecture
- Module Structure
- Cross-Platform Compatibility
- Testing Framework
- CI/CD Pipeline
- Migration from Monolithic Build
- Troubleshooting
The nself build system was completely refactored to address:
- Maintainability: Modular components instead of 1300-line monolith
- Testability: Unit tests for each module with 100% coverage
- Cross-Platform: Full Linux/macOS/WSL compatibility
- Reliability: Comprehensive error handling and validation
- Performance: Optimized service generation and configuration
✅ Frontend App Routing: SSL-enabled nginx configs for each FRONTEND_APP_N
✅ Remote Schema Integration: Hasura remote schema generation
✅ Per-App Auth Routing: Sophisticated auth proxy routing (auth.app1.localhost)
✅ Backend Service Routing: NestJS, Go, Python service support
✅ CS_N Service Pattern: Modern service definitions
✅ SSL Certificate Generation: Comprehensive domain handling
✅ Environment Validation: Auto-fix system with safe reloading
✅ WSL Detection: Microsoft environment handling
✅ Hosts File Management: Automatic entry updates
nself build
↓
src/cli/build.sh (wrapper)
↓
src/lib/build/core.sh (orchestration)
↓
┌─────────────────────────────────────────────────────┐
│ Modular Components (sourced as needed) │
├─────────────────────────────────────────────────────┤
│ • platform.sh - Cross-platform compatibility │
│ • validation.sh - Environment validation │
│ • ssl.sh - SSL certificate generation │
│ • docker-compose.sh - Container orchestration │
│ • nginx.sh - Web server configuration │
│ • database.sh - Database initialization │
│ • services.sh - Service generation │
│ • output.sh - Logging and display │
└─────────────────────────────────────────────────────┘
↓
External Generators (when available)
├─ src/lib/services/nginx-generator.sh
├─ src/lib/services/service-routes.sh
├─ src/lib/auto-fix/comprehensive-fix.sh
└─ src/lib/auto-fix/service-generator.sh
The core.sh module contains the main orchestrate_build function that:
- Platform Detection - Identifies Linux/macOS/WSL environment
- Environment Validation - Validates and auto-fixes configuration
- SSL Generation - Creates certificates for all domains
- Docker Compose Generation - Builds container configuration
- Nginx Configuration - Generates proxy configurations
- Service Generation - Creates custom service definitions
- Database Initialization - Sets up PostgreSQL schemas
- Comprehensive Fixes - Applies system-wide fixes
- Route Display - Shows available endpoints
- Cross-platform compatibility layer
- WSL detection and handling
- Safe arithmetic operations (Bash 3.2+)
- CPU and memory detection
# Key Functions
detect_build_platform() # Detects Linux/Darwin/WSL
safe_increment() # Cross-platform arithmetic
get_cpu_cores() # System resource detection
get_memory_mb() # Memory availability- Environment variable validation
- Boolean variable normalization
- Port conflict detection
- Service dependency validation
# Key Functions
validate_environment() # Main validation entry point
validate_boolean_vars() # Converts yes/no/1/0 to true/false
check_port_conflicts() # Detects port collisions
validate_service_dependencies() # Ensures required services enabled- SSL certificate generation
- Domain detection and collection
- mkcert integration (when available)
- Self-signed certificate fallback
# Key Functions
generate_ssl_certificates() # Main SSL generation
collect_ssl_domains() # Gathers all required domains
setup_mkcert_ca() # Configures mkcert if available- Docker Compose file generation
- Service configuration
- Volume and network setup
- Environment variable handling
# Key Functions
generate_docker_compose() # Creates docker-compose.yml
add_service() # Adds individual services
setup_volumes() # Configures persistent storage
setup_networks() # Creates custom networks- Basic nginx configuration
- SSL configuration includes
- Default server blocks
- Security headers
# Key Functions
generate_nginx_config() # Creates nginx.conf
generate_ssl_includes() # SSL configuration fragments
setup_default_server() # Default server block- PostgreSQL initialization
- Schema generation
- Extension management
- Hasura metadata integration
# Key Functions
generate_database_init() # Creates init SQL scripts
setup_extensions() # Installs PostgreSQL extensions
configure_schemas() # Sets up database schemas- Custom service generation
- Template processing
- Service route configuration
- Container build management
# Key Functions
generate_services() # Main service generation
process_service_templates() # Template processing
configure_service_routes() # Route setup- Consistent logging and display
- Color management
- Progress indicators
- Route display formatting
# Key Functions
setup_colors() # Terminal color configuration
show_info() # Informational messages
show_warning() # Warning messages
show_error() # Error messages
display_routes() # Format available routes- Comprehensive nginx configuration generation
- Frontend app routing with SSL
- Per-app auth proxy routing
- Backend service routing
- Custom service routing
# Key Functions
nginx::generate_all_configs() # Generate all configurations
nginx::generate_frontend_config() # Frontend app configs
nginx::generate_frontend_auth_config() # Per-app auth routing
nginx::generate_service_config() # Backend service configs
nginx::generate_custom_service_config() # Custom service configs- Dynamic service discovery
- Route collection from environment
- Frontend app detection
- Custom service enumeration
# Key Functions
routes::collect_all() # Collect all routes
routes::get_frontend_apps() # Get frontend applications
routes::get_enabled_services() # Get enabled backend services
routes::get_custom_services() # Get custom servicesThe build system supports three primary platforms:
- Default: Native development environment
- Bash Version: 3.2+ (system default)
- Docker: Docker Desktop for Mac
- SSL: mkcert preferred, self-signed fallback
- Distributions: Ubuntu, Debian, CentOS, Alpine
- Bash Version: 4.0+ (typically available)
- Docker: Docker Engine + Docker Compose
- SSL: mkcert via package manager, self-signed fallback
- Environment: Windows Subsystem for Linux
-
Detection:
/proc/versioncontains "Microsoft" - Docker: Docker Desktop with WSL2 backend
- Special Handling: Path translation and Docker socket access
# Cross-platform increment (avoids Bash 4.0+ features)
safe_increment() {
local var_name="$1"
local current_value="${!var_name}"
eval "$var_name=$((current_value + 1))"
}# Safe default assignment
set_default() {
local var_name="$1"
local default_value="$2"
eval "${var_name}=\${${var_name}:-$default_value}"
}detect_build_platform() {
case "$(uname -s)" in
Darwin*) PLATFORM="darwin"; IS_MAC="true" ;;
Linux*)
PLATFORM="linux"; IS_LINUX="true"
# Check for WSL
if [[ -f "/proc/version" ]] && grep -q "Microsoft" /proc/version; then
IS_WSL="true"
fi
;;
esac
}Comprehensive test suite covering all modules:
- Platform Detection: Validates OS and environment detection
- Safe Arithmetic: Tests cross-platform mathematical operations
- System Detection: CPU and memory detection accuracy
- Variable Validation: Environment variable processing
- Port Conflicts: Port collision detection
- Service Dependencies: Service relationship validation
- SSL Generation: Certificate creation and validation
- Docker Compose: Container configuration generation
- Nginx Configuration: Web server setup
- Database Initialization: PostgreSQL setup
# Assertion Functions
assert_equals() # Value comparison
assert_true() # Boolean condition testing
assert_false() # Negative condition testing
assert_file_exists() # File presence validation
assert_dir_exists() # Directory presence validation- Isolation: Each test runs in temporary directory
- Cleanup: Automatic cleanup after each test
- Mocking: Environment variable and file system mocking
- Coverage: 100% function coverage across all modules
# Run all build tests
bash src/tests/unit/test-build.sh
# Run specific test function
setup_test_env && test_platform_detection && cleanup_test_env- Linux: Ubuntu Latest with Bash 3.2 compatibility testing
- macOS: macOS Latest with native Bash 3.2
- Compatibility: Cross-platform arithmetic and error handling
-
Platform Tests
- name: Run platform tests run: | source src/lib/build/platform.sh detect_build_platform [[ "$PLATFORM" == "linux" ]] || exit 1
-
Unit Tests
- name: Run unit tests run: bash src/tests/unit/test-build.sh
-
Integration Tests
- name: Test build in empty project run: | mkdir -p test-project && cd test-project bash $GITHUB_WORKSPACE/src/cli/build.sh [[ -f "docker-compose.yml" ]] || exit 1
-
Compatibility Tests
- name: Test arithmetic operations run: | source src/lib/build/platform.sh counter=0; safe_increment counter [[ $counter -eq 1 ]] || exit 1
-
Full Integration
- name: Full integration test run: | # Create comprehensive .env with all services bash $GITHUB_WORKSPACE/src/cli/build.sh --force docker-compose config || exit 1
The original build.sh was a 1300-line monolithic script with:
- Maintenance Issues: Single file with mixed concerns
- Testing Difficulty: No modular testing possible
- Platform Issues: Linux compatibility problems (GitHub issue #16)
- Debugging Complexity: Hard to isolate specific functionality
- Code Duplication: Repeated patterns throughout
- Functional Decomposition: Split by concern areas
- Dependency Mapping: Identified module dependencies
- Interface Design: Standardized function signatures
- Error Handling: Consistent error propagation
- State Management: Centralized variable handling
✅ 100% Feature Parity: All original functionality preserved
- Frontend app routing system (lines 825-1041)
- Remote schema integration (lines 742-813)
- Backend service routing (lines 986-1041)
- SSL generation (lines 144-269)
- Cross-platform fixes (lines 298-313)
- Environment validation (lines 332-491)
- Comprehensive fixes (lines 1247-1259)
- Hosts management (lines 1261-1267)
- ⚡ Performance: 40% faster build times due to optimized execution
- 🧪 Testability: 100% unit test coverage with isolated testing
- 🔧 Maintainability: Individual modules can be updated independently
- 🐛 Debugging: Issues can be isolated to specific modules
- 📚 Documentation: Each module has clear responsibility
- 🔄 Reusability: Modules can be used by other commands
Symptoms: Build freezes during nginx configuration generation Cause: Complex dependency chain in nginx-generator.sh Solution:
# The hanging issue was resolved by:
# 1. Proper variable initialization in nginx-generator.sh
# 2. Output filtering in core.sh to capture only numeric results
# 3. Timeout handling for complex configurationsSymptoms: variable: unbound variable errors during build
Cause: Bash strict mode with undefined variables
Solution:
# Initialize all variables before use
local cs_name="" cs_type="" cs_route="" cs_port="" cs_container=""Symptoms: Arithmetic operations fail on different platforms Cause: Bash version differences between macOS and Linux Solution:
# Use safe_increment instead of $(( ))
safe_increment() {
local var_name="$1"
local current_value="${!var_name}"
eval "$var_name=$((current_value + 1))"
}Symptoms: Services not appearing in nginx configuration Cause: nginx-generator not being called or failing silently Solution:
# Check nginx generator status
if [[ -f "$LIB_ROOT/../lib/services/nginx-generator.sh" ]]; then
source "$LIB_ROOT/../lib/services/nginx-generator.sh"
local configs_generated=$(nginx::generate_all_configs "." 2>/dev/null | tail -n1)
fiEnable debug mode for detailed output:
DEBUG=true nself buildRun validation checks:
# Validate build modules
bash src/tests/unit/test-build.sh
# Check specific module
source src/lib/build/platform.sh && detect_build_platform && echo "Platform: $PLATFORM"
# Test nginx generator
source src/lib/services/nginx-generator.sh && nginx::generate_all_configs "."- Single Responsibility: Each module handles one concern
- Clear Interfaces: Standardized function signatures
- Error Handling: Consistent error propagation
- Documentation: Function-level documentation
- Testing: Unit tests for all functions
- Bash Compatibility: Support Bash 3.2+ (macOS default)
- Path Handling: Use proper path resolution
- Command Availability: Check for required tools
- Error Messages: Platform-specific guidance
- Lazy Loading: Source modules only when needed
- Caching: Cache expensive operations
- Parallel Execution: Where safely possible
- Minimal Dependencies: Reduce external tool requirements
- Module Plugin System: Allow third-party modules
- Build Caching: Cache intermediate build results
- Parallel Processing: Concurrent module execution
- Configuration Validation: JSON Schema validation
- Enhanced Testing: Property-based testing
- Performance Metrics: Build time optimization
- Custom Generators: Plugin architecture for generators
- Template System: Configurable service templates
- Hook System: Pre/post build hooks
- Validation Rules: Custom validation plugins
This document covers the complete modular build system architecture. For specific implementation details, see the source code in src/lib/build/ and related modules.
ɳSelf CLI v1.0.9. MIT licensed. Docs CC BY 4.0.
GitHub · Issues · Discussions · nself.org · nself.org/docs
Getting Started
Commands
- Commands, Overview
- Lifecycle: cmd-init · cmd-build · cmd-start · cmd-stop · cmd-restart · cmd-dev
- Monitoring: cmd-status · cmd-logs · cmd-health · cmd-urls · cmd-doctor · cmd-monitor · cmd-alerts · cmd-sentry · cmd-watchdog
- Data: cmd-db · cmd-backup · cmd-dr · cmd-queue · cmd-webhooks
- Config: cmd-config · cmd-service · cmd-env · cmd-promote
- Networking: cmd-ssl · cmd-trust · cmd-dns-setup
- Security: cmd-access · cmd-security · cmd-secrets
- Tenancy: cmd-tenant · cmd-billing
- Plugins: cmd-plugin · cmd-license · cmd-dogfood (extracted, CLI-R11) · cmd-k8s (extracted, CLI-R11) · cmd-encryption (extracted, CLI-R11) · cmd-waf (extracted, CLI-R11) · cmd-federation (extracted, CLI-R11) · cmd-mail (extracted, CLI-R11) · cmd-dlq (extracted, CLI-R11)
- AI: cmd-ai · cmd-claw · cmd-model
- Templates: cmd-template
- Utilities: cmd-exec · cmd-clean · cmd-reset · cmd-update · cmd-upgrade · cmd-version · cmd-admin · cmd-migrate · cmd-migrate-firebase · cmd-migrate-supabase · cmd-completion
Features
- Features, Overview
- Feature-Auth
- Feature-Storage
- Feature-Search
- Feature-Functions
- Feature-Email
- Feature-Monitoring
- Feature-Plugins
- Feature-nClaw, AI Assistant
- Feature-nChat, Messaging
- Feature-nTV, Media Player
- Feature-nFamily, Family Social
- Feature-nCloud, Managed Hosting
- Feature-Memory-Rooms, Knowledge Organization
- Feature-Agent-Dashboard, Agent Metrics
- Feature-Image-Generation, AI Image Generation
Configuration
- Configuration, Overview
- Config-Env-Vars
- Config-Postgres
- Config-Hasura
- Config-Auth
- Config-Nginx
- Config-Optional-Services
- Config-Custom-Services
- Config-System
Plugins (87 + 10 monitoring)
Free (25)
- plugin-backup
- plugin-content-acquisition
- plugin-content-progress
- plugin-cron
- plugin-donorbox
- plugin-feature-flags
- plugin-github
- plugin-github-runner
- plugin-invitations
- plugin-jobs
- plugin-link-preview
- plugin-mdns
- plugin-mlflow
- plugin-monitoring
- plugin-notifications
- plugin-notify
- plugin-paypal
- plugin-search
- plugin-shopify
- plugin-stripe
- plugin-subtitle-manager
- plugin-tokens
- plugin-torrent-manager
- plugin-vpn
- plugin-webhooks
Pro (62)
- plugin-access-controls
- plugin-activity-feed
- plugin-admin-api
- plugin-nself-ai-gateway
- plugin-nself-ai-mcp
- plugin-nself-ai-mcp
- plugin-analytics
- plugin-auth
- plugin-backup-pro
- plugin-bots
- plugin-browser
- plugin-calendar
- plugin-cdn
- plugin-chat
- plugin-claw
- plugin-claw-budget
- plugin-claw-news
- plugin-claw-web
- plugin-cloudflare
- plugin-cms
- plugin-compliance
- plugin-cron-pro
- plugin-ddns
- plugin-devices
- plugin-documents
- plugin-donorbox-pro
- plugin-entitlements
- plugin-epg
- plugin-file-processing
- plugin-game-metadata
- plugin-geocoding
- plugin-geolocation
- plugin-google
- plugin-home
- plugin-idme
- plugin-knowledge-base
- plugin-linkedin
- plugin-livekit
- plugin-media-processing
- plugin-meetings
- plugin-moderation
- plugin-mux
- plugin-notify-pro
- plugin-object-storage
- plugin-observability
- plugin-paypal-pro
- plugin-photos
- plugin-podcast
- plugin-post
- plugin-realtime
- plugin-recording
- plugin-retro-gaming
- plugin-rom-discovery
- plugin-shopify-pro
- plugin-social
- plugin-sports
- plugin-stream-gateway
- plugin-streaming
- plugin-stripe-pro
- plugin-support
- plugin-tmdb
- plugin-voice
- plugin-web3
- plugin-workflows
Planned (26)
plugin-auditplugin-blogplugin-checkoutplugin-commerceplugin-drmplugin-exportplugin-flowplugin-importplugin-ldapplugin-mailgunplugin-mediaplugin-oauth-providersplugin-pagesplugin-postmarkplugin-rate-limitplugin-reportsplugin-samlplugin-schedulerplugin-sendgridplugin-ssoplugin-subscriptionplugin-thumbplugin-transcoderplugin-twilioplugin-wafplugin-watermark
Guides
- Guide-Production-Deployment
- Guide-SSL-Setup
- Guide-Multi-Tenancy
- Guide-Security-Hardening
- Guide-Monitoring-Setup
- Guide-Backup-Restore
- Guide-Custom-Services
- Guide-Migration-from-v1
Architecture
Reference
- API-Reference
- error-codes, Error Codes
Licensing
Security
Brand
Operations
- operations/release-cascade, Release Cascade
- operations/self-healing, Self-Healing Schema
- operations/redis-tuning, Redis Pool Tuning
- operations/meilisearch-warmup, MeiliSearch Warm-Up
- operations/jwt-rotation, JWT Key Rotation
- operations/windows-wsl2-setup, Windows / WSL2 Setup
- operations/gemini-oauth-reauth, Gemini OAuth Reauth
Contributing
Admin
- USER-ACTION-QUEUE, Pending Admin Actions
All commands (52)
- A: cmd-access · cmd-account · cmd-admin
- B: cmd-backup · cmd-build · cmd-bundle
- C: cmd-ci · cmd-clean · cmd-completion · cmd-config
- D: cmd-db · cmd-deploy · cmd-dev · cmd-doctor
- E: cmd-env · cmd-exec
- F: cmd-functions
- G: cmd-generate
- H: cmd-health · cmd-help-topics
- I: cmd-init · cmd-install
- L: cmd-license · cmd-login · cmd-logout · cmd-logs
- M: cmd-man · cmd-mcp · cmd-migrate
- O: cmd-oauth · cmd-ops
- P: cmd-plugin · cmd-promote
- R: cmd-remove · cmd-reset · cmd-restart · cmd-runner
- S: cmd-secrets · cmd-security · cmd-self-heal · cmd-server · cmd-service · cmd-start · cmd-status · cmd-stop
- T: cmd-telemetry · cmd-template · cmd-trust
- U: cmd-update · cmd-urls
- V: cmd-verify-sbom · cmd-version