Skip to content

🔄 Implement Cross-Platform Portable Development Environments #98

@LarsArtmann

Description

@LarsArtmann

🔄 Cross-Platform Portable Development Environments

🎯 Objective

Create fully portable, cross-platform development environments using Nix wrappers that work seamlessly across macOS, Linux, and Windows (WSL), eliminating the need for platform-specific configuration management.

🌍 Cross-Platform Architecture

1. Platform Detection & Adaptation

# dotfiles/nix/wrappers/cross-platform/platform-detection.nix
{ pkgs, lib, wrappers }:

let
  # Platform detection utilities
  isDarwin = pkgs.stdenv.isDarwin;
  isLinux = pkgs.stdenv.isLinux;
  isAarch64 = pkgs.stdenv.isAarch64;
  isX86_64 = pkgs.stdenv.isx86_64;
  
  # Platform-specific configuration adapters
  adaptForPlatform = { package, config ? {}, platform ? builtins.currentSystem }:
    let
      platformConfig = if lib.hasSuffix "darwin" platform then {
        # macOS-specific adaptations
        font_size = config.font_size or 14;
        theme = config.theme or "Adaptive";
        memory_limit = config.memory_limit or "2G";
      } else if lib.hasSuffix "linux" platform then {
        # Linux-specific adaptations  
        font_size = (config.font_size or 14) - 2;
        theme = config.theme or "Default";
        memory_limit = config.memory_limit or "1G";
      } else {
        # Fallback configuration
        inherit config;
      };
    in
    wrappers.lib.wrapPackage {
      inherit package;
      config = platformConfig;
    };

in
{
  inherit isDarwin isLinux isAarch64 isX86_64 adaptForPlatform;
  
  # Platform-optimized wrapper creators
  createCrossPlatformWrapper = { name, package, baseConfig, platformConfigs ? {} }:
    let
      currentPlatform = builtins.currentSystem;
      platformConfig = platformConfigs.${currentPlatform} or baseConfig;
    in
    wrappers.lib.wrapPackage {
      package = pkgs.${package};
      config = baseConfig // platformConfig;
    };
}

2. Development Environment Templates

# dotfiles/nix/wrappers/cross-platform/dev-environments.nix
{ pkgs, lib, wrappers }:

let
  # Cross-platform Go development environment
  goDevEnv = wrappers.lib.wrapEnvironment {
    name = "go-dev";
    packages = with pkgs; [ go gopls gofumpt ];
    
    # Platform-specific packages
    platformPackages = {
      "x86_64-darwin" = [ pkgs.darwin.apple_sdk.frameworks.Security ];
      "aarch64-darwin" = [ pkgs.darwin.apple_sdk.frameworks.Security ];
      "x86_64-linux" = [ pkgs.gcc ];
      "aarch64-linux" = [ pkgs.gcc ];
    };
    
    # Cross-platform environment variables
    environment = {
      GOPROXY = "https://proxy.golang.org,direct";
      GOSUMDB = "sum.golang.org";
      # Platform-specific cache directories
      GOCACHE = if pkgs.stdenv.isDarwin 
                then "$HOME/Library/Caches/go-build" 
                else "$HOME/.cache/go-build";
      GOMODCACHE = if pkgs.stdenv.isDarwin
                   then "$HOME/Library/Caches/go-mod"  
                   else "$HOME/.cache/go-mod";
    };
    
    # Platform-specific scripts
    scripts = {
      setup-darwin = lib.mkIf pkgs.stdenv.isDarwin ''
        echo "🍎 Setting up Go development on macOS..."
        # macOS-specific setup
        mkdir -p ~/Library/Caches/go-build
        mkdir -p ~/Library/Caches/go-mod
      '';
      
      setup-linux = lib.mkIf pkgs.stdenv.isLinux ''
        echo "🐧 Setting up Go development on Linux..."
        # Linux-specific setup
        mkdir -p ~/.cache/go-build
        mkdir -p ~/.cache/go-mod
        # Set up proper permissions
        chmod 755 ~/.cache
      '';
    };
  };

  # Cross-platform web development environment
  webDevEnv = wrappers.lib.wrapEnvironment {
    name = "web-dev";
    packages = with pkgs; [ nodejs bun typescript ];
    
    platformPackages = {
      "x86_64-darwin" = [ pkgs.darwin.apple_sdk.frameworks.CoreFoundation ];
      "aarch64-darwin" = [ pkgs.darwin.apple_sdk.frameworks.CoreFoundation ];
      "x86_64-linux" = [ pkgs.alsa-lib pkgs.libxkbcommon ];
      "aarch64-linux" = [ pkgs.alsa-lib pkgs.libxkbcommon ];
    };
    
    environment = {
      NODE_OPTIONS = "--max-old-space-size=4096";
      # Platform-specific Node.js optimizations
      NODE_ENV = if pkgs.stdenv.isDarwin then "development-macos" else "development-linux";
      # Platform-specific cache
      NPM_CONFIG_CACHE = if pkgs.stdenv.isDarwin
                         then "$HOME/Library/Caches/npm"
                         else "$HOME/.cache/npm";
    };
  };

in
{
  inherit goDevEnv webDevEnv;
}

3. Configuration Synchronization System

# dotfiles/nix/wrappers/cross-platform/sync.nix
{ pkgs, lib, wrappers }:

let
  # Cross-platform configuration synchronization
  syncConfigurations = { localConfig, remoteUrl ? null }:
    let
      syncWrapper = pkgs.writeShellScriptBin "sync-wrappers" ''
        #!/usr/bin/env bash
        set -euo pipefail
        
        echo "🔄 Syncing wrapper configurations..."
        
        # Detect current platform
        PLATFORM=$(nix eval --impure --raw --expr 'builtins.currentSystem')
        echo "🖥️  Detected platform: $PLATFORM"
        
        # Sync from remote if URL provided
        if [ -n "${remoteUrl}" ]; then
          echo "📥 Syncing from remote: ${remoteUrl}"
          nix flake update --update-input wrapper-configs
        fi
        
        # Apply platform-specific configurations
        echo "⚙️  Applying platform-specific configurations..."
        nix run .#apply-platform-configs
        
        # Validate configurations
        echo "🧪 Validating configurations..."
        nix run .#validate-wrappers
        
        echo "✅ Configuration sync complete"
      '';
    in
    syncWrapper;

in
{
  inherit syncConfigurations;
}

🎯 Platform-Specific Adaptations

macOS Adaptations

  • Font Sizes: Larger fonts for Retina displays
  • Memory Management: Higher limits due to memory availability
  • File Paths: Unix-style paths with macOS conventions
  • Security: Apple Silicon vs Intel compatibility

Linux Adaptations

  • Package Dependencies: System library dependencies
  • Display Servers: X11/Wayland compatibility
  • File Permissions: Proper permission handling
  • Package Managers: Integration with distro package managers

Windows (WSL) Adaptations

  • Path Conversion: Windows/Unix path mapping
  • Executable Extensions: .exe handling
  • Environment Variables: Windows environment integration
  • File System: Case sensitivity handling

🚀 Implementation Strategy

Phase 1: Platform Detection (Week 1)

  1. Create platform detection utilities
  2. Implement platform-specific config adapters
  3. Test on macOS and Linux systems
  4. Create WSL compatibility layer

Phase 2: Environment Templates (Week 2)

  1. Develop Go development environment template
  2. Create web development environment template
  3. Build data science environment template
  4. Add system administration environment

Phase 3: Synchronization System (Week 3)

  1. Implement configuration sync utilities
  2. Create cloud storage integration
  3. Add conflict resolution system
  4. Build backup and recovery tools

Phase 4: Testing & Validation (Week 4)

  1. Cross-platform testing matrix
  2. Performance benchmarking
  3. User experience validation
  4. Documentation and examples

📁 Deliverables

dotfiles/nix/wrappers/
├── cross-platform/
│   ├── platform-detection.nix     # Platform utilities
│   ├── dev-environments.nix       # Environment templates
│   ├── sync.nix                   # Synchronization system
│   ├── adapters/
│   │   ├── darwin.nix            # macOS adapters
│   │   ├── linux.nix             # Linux adapters
│   │   └── wsl.nix               # WSL adapters
│   └── environments/
│       ├── go-dev.nix            # Go environment
│       ├── web-dev.nix           # Web environment
│       └── data-science.nix      # Data science environment

🔧 New Just Commands

# Cross-platform commands
platform-info:
    @echo "🖥️  Platform Information:"
    @echo "  System: $(nix eval --impure --raw --expr 'builtins.currentSystem')"
    @echo "  OS: $(uname -s)"
    @echo "  Arch: $(uname -m)"

sync-platforms:
    @echo "🔄 Syncing configurations across platforms..."
    @nix run .#sync-wrappers

setup-go-dev:
    @echo "🐹 Setting up Go development environment..."
    @nix run .#go-dev-env -- setup

setup-web-dev:
    @echo "🌐 Setting up web development environment..."
    @nix run .#web-dev-env -- setup

validate-platform:
    @echo "🧪 Validating platform-specific configurations..."
    @nix run .#validate-platform-configs

📊 Success Metrics

  • 100% compatibility across macOS, Linux, and WSL
  • Single command setup on any platform
  • Automatic synchronization of configurations
  • Platform optimization for maximum performance
  • Zero configuration drift between platforms

🎯 Use Cases

1. Developer Machine Rotation

  • Before: 2-4 hours manual setup per machine
  • After: 5 minutes single command setup

2. Team Environment Standardization

  • Before: Inconsistent environments, configuration drift
  • After: Identical environments across all platforms

3. CI/CD Pipeline Consistency

  • Before: Different environments in dev vs CI
  • After: Identical environments in all contexts

🚨 Risk Mitigation

  1. Platform Testing: Comprehensive testing matrix
  2. Fallback Configurations: Default configurations for edge cases
  3. Gradual Rollout: Test with individual environments first
  4. Backup Systems: Configuration backup and recovery

This cross-platform system will enable truly portable development environments that work consistently across all major platforms, dramatically improving developer productivity and reducing setup friction.


🏷️ Labels: enhancement, cross-platform, area/infrastructure, tech/nix, effort/medium, impact/high, status/planning

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions