// NIP Configuration with USE Flags and Build Settings
// This is an example configuration showing the USE flag system

nip {
    // Basic Directory Configuration
    programs-dir "/Programs"
    links-dir "/System/Links"
    cache-dir "/var/nip/cache"
    db-file "/var/nip/db/packages.json"

    // Global Options
    auto-symlink true
    check-conflicts true
    verbose false

    // ============================================
    // Global USE Flags
    // ============================================
    // These apply to all packages unless overridden

    use-flags {
        // Init System (mutually exclusive)
        // Choose ONE: systemd, dinit, openrc, runit
        init {
            -systemd      // Disable systemd support
            +dinit        // Enable dinit support
            -openrc       // Disable OpenRC support
            -runit        // Disable runit support
        }

        // GUI Toolkit Preferences
        gui {
            -X            // Disable X11 support
            +wayland      // Enable Wayland support
            +gtk          // Enable GTK support
            -qt           // Disable Qt support
        }

        // Audio System
        audio {
            -pulseaudio   // Disable PulseAudio
            +pipewire     // Enable PipeWire
            +alsa         // Enable ALSA
        }

        // Optimization Flags
        optimization {
            +lto          // Enable Link-Time Optimization
            +pgo          // Enable Profile-Guided Optimization
            -debug        // Disable debug symbols
            +strip        // Strip binaries
        }

        // Security Hardening
        security {
            +hardened     // Enable hardening
            +pie          // Position Independent Executables
            +relro        // RELRO (Relocation Read-Only)
            +stack-protector  // Stack protection
        }

        // Common Features
        features {
            +ipv6         // Enable IPv6 support
            +ssl          // Enable SSL/TLS
            +zstd         // Enable Zstandard compression
            +lz4          // Enable LZ4 compression
            -doc          // Disable documentation
            -examples     // Disable examples
            +dbus         // Enable D-Bus support
        }

        // Language Bindings
        bindings {
            +python       // Enable Python bindings
            -ruby         // Disable Ruby bindings
            -perl         // Disable Perl bindings
            +lua          // Enable Lua bindings
        }

        // ============================================
        // Nexus-Specific Categories
        // ============================================

        // Nexus Fleet Command
        nexus-fleet {
            +fleet-agent      // Enable fleet agent
            +fleet-discovery  // Enable service discovery
            +fleet-mesh       // Enable mesh networking
            -fleet-controller // Disable controller (not needed on nodes)
        }

        // System Bootstrapping
        nexus-bootstrap {
            -bootstrap        // Not building from scratch
            +cross-compile    // Enable cross-compilation support
            -stage1           // Not stage1 bootstrap
        }

        // Container Technologies
        container {
            +nipcells         // Enable NipCells (Nexus containers)
            -docker           // Disable Docker
            +containerd       // Enable containerd
            +crun             // Enable crun runtime
        }

        // Virtualization
        virtualization {
            +kvm              // Enable KVM support
            +qemu             // Enable QEMU
            +libvirt          // Enable libvirt
            -xen              // Disable Xen
        }

        // Mesh Networking
        mesh {
            +mesh-network     // Enable mesh networking
            +wireguard        // Enable WireGuard
            +libp2p           // Enable libp2p
            -ipfs             // Disable IPFS
        }

        // Gaming
        gaming {
            +vulkan           // Enable Vulkan
            +mesa             // Enable Mesa
            +amd              // AMD GPU support
            -nvidia           // No NVIDIA
            -steam            // No Steam
        }

        // AI/ML and NPU
        ai-ml {
            +rocm             // Enable ROCm (AMD)
            +opencl           // Enable OpenCL
            -cuda             // No CUDA (NVIDIA)
            +onnx             // Enable ONNX runtime
        }

        // Developer Tools
        developer {
            +debugger         // Enable debugging support
            +lsp              // Enable LSP support
            +profiler         // Enable profiling
            -sanitizer        // Disable sanitizers (performance)
        }

        // Nexus Integration
        nexus-integration {
            +nexus-api        // Enable Nexus API
            +nexus-sync       // Enable sync service
            +nexus-monitor    // Enable monitoring
            +nexus-security   // Enable security features
        }
    }

    // ============================================
    // Compiler and Linker Flags
    // ============================================

    compiler {
        // C Compiler Flags
        CFLAGS "-O3 -march=native -pipe -fomit-frame-pointer"

        // C++ Compiler Flags
        CXXFLAGS "-O3 -march=native -pipe -fomit-frame-pointer"

        // Linker Flags
        LDFLAGS "-Wl,-O1 -Wl,--as-needed -Wl,--hash-style=gnu"

        // Make Flags (parallel jobs)
        MAKEFLAGS "-j8"

        // Additional flags
        RUSTFLAGS "-C opt-level=3 -C target-cpu=native"
        GOFLAGS "-buildmode=pie"
    }

    // ============================================
    // Build Profiles
    // ============================================
    // Named collections of USE flags and compiler settings

    profiles {
        // Currently active profile
        active "performance"

        // Minimal Profile - Small footprint
        profile "minimal" {
            description "Minimal features for embedded or containers"
            base "default"

            use-flags {
                // Disable almost everything
                -systemd -dinit -openrc
                -X -wayland -gtk -qt
                -pulseaudio -pipewire
                -doc -examples -dbus
                // Keep essentials
                +ssl +ipv6
            }

            compiler {
                CFLAGS "-Os -pipe"
                CXXFLAGS "-Os -pipe"
                LDFLAGS "-Wl,-O1 -Wl,--as-needed -Wl,--strip-all"
                MAKEFLAGS "-j4"
            }
        }

        // Desktop Profile - Full GUI support
        profile "desktop" {
            description "Full desktop environment with GUI"
            base "default"

            use-flags {
                +dinit
                +wayland +gtk +qt
                +pipewire +alsa
                +dbus +ipv6 +ssl
                +python +lua
            }

            compiler {
                CFLAGS "-O2 -pipe"
                CXXFLAGS "-O2 -pipe"
                LDFLAGS "-Wl,-O1"
                MAKEFLAGS "-j8"
            }
        }

        // Performance Profile - Maximum optimization
        profile "performance" {
            description "Maximum performance with aggressive optimizations"
            base "default"

            use-flags {
                +lto +pgo
                -debug +strip
                +hardened +pie
            }

            compiler {
                CFLAGS "-O3 -march=native -flto -fomit-frame-pointer -pipe"
                CXXFLAGS "-O3 -march=native -flto -fomit-frame-pointer -pipe"
                LDFLAGS "-Wl,-O1 -Wl,--as-needed -flto"
                MAKEFLAGS "-j16"
                RUSTFLAGS "-C opt-level=3 -C target-cpu=native -C lto=fat"
            }
        }

        // Server Profile - Headless server
        profile "server" {
            description "Server configuration without GUI"
            base "default"

            use-flags {
                +systemd
                -X -wayland -gtk -qt
                -pulseaudio -pipewire
                +ssl +ipv6 +zstd
                +hardened +pie +relro
            }

            compiler {
                CFLAGS "-O2 -pipe -fstack-protector-strong"
                CXXFLAGS "-O2 -pipe -fstack-protector-strong"
                LDFLAGS "-Wl,-O1 -Wl,-z,relro -Wl,-z,now"
                MAKEFLAGS "-j8"
            }
        }

        // Development Profile - Debug symbols
        profile "development" {
            description "Development with debug symbols"
            base "default"

            use-flags {
                +debug
                -strip -lto
                +doc +examples
            }

            compiler {
                CFLAGS "-O0 -g -pipe"
                CXXFLAGS "-O0 -g -pipe"
                LDFLAGS "-Wl,-O1"
                MAKEFLAGS "-j8"
            }
        }
    }

    // ============================================
    // Per-Package Configuration
    // ============================================
    // Override USE flags and compiler settings for specific packages

    package "firefox" {
        description "Firefox web browser"

        use-flags {
            +wayland -X        // Wayland-only build
            +alsa -pulseaudio  // ALSA instead of PulseAudio
            +lto               // Enable LTO for Firefox
            -debug +strip      // Release build
        }

        compiler {
            CFLAGS "-O3 -march=native -flto"
            CXXFLAGS "-O3 -march=native -flto"
            LDFLAGS "-Wl,-O1 -flto"
            MAKEFLAGS "-j8"
        }
    }

    package "vim" {
        description "Vim text editor"

        use-flags {
            +python +lua       // Enable scripting
            -ruby -perl        // Disable other languages
            +gtk               // GUI support
            -X                 // No X11
        }
    }

    package "nginx" {
        description "Nginx web server"

        use-flags {
            +ssl +http2 +http3  // Modern protocols
            +zstd +lz4          // Compression
            -debug              // Production build
            +hardened           // Security hardening
        }

        compiler {
            CFLAGS "-O3 -flto -fstack-protector-strong"
            LDFLAGS "-Wl,-O1 -Wl,-z,relro -Wl,-z,now -flto"
        }
    }

    package "ffmpeg" {
        description "FFmpeg multimedia framework"

        use-flags {
            +lto +pgo          // Maximum performance
            +vaapi +vdpau      // Hardware acceleration
            +x264 +x265 +av1   // Video codecs
            +opus +aac         // Audio codecs
        }

        compiler {
            CFLAGS "-O3 -march=native -flto -fomit-frame-pointer"
            MAKEFLAGS "-j16"
        }
    }

    package "python" {
        description "Python interpreter"

        use-flags {
            +lto +pgo          // Optimize Python itself
            +ssl +ipv6         // Network support
            -debug             // Release build
        }

        compiler {
            CFLAGS "-O3 -march=native -flto"
            LDFLAGS "-Wl,-O1 -flto"
        }
    }

    // ============================================
    // Nexus-Specific Packages
    // ============================================

    package "nexus-fleet" {
        description "Nexus fleet orchestration system"

        use-flags {
            +fleet-agent +fleet-controller +fleet-mesh
            +wireguard +libp2p
            +nexus-api +nexus-sync
            +lto -debug
        }

        compiler {
            CFLAGS "-O3 -march=native -flto"
            LDFLAGS "-Wl,-O1 -flto"
        }
    }

    package "nipcells" {
        description "NipCells container runtime"

        use-flags {
            +nipcells +crun +containerd
            +nexus-security +nexus-monitor
            +lto +hardened
        }

        compiler {
            CFLAGS "-O3 -flto -fstack-protector-strong"
            LDFLAGS "-Wl,-O1 -Wl,-z,relro -Wl,-z,now -flto"
        }
    }

    package "nexus-compiler" {
        description "Nexus system compiler with bootstrap support"

        use-flags {
            +bootstrap +cross-compile
            +stage1 +stage2 +stage3
            +lto +pgo
            +developer +debugger
        }

        compiler {
            CFLAGS "-O3 -march=native"
            LDFLAGS "-Wl,-O1"
        }
    }

    package "blender" {
        description "3D creation suite with GPU acceleration"

        use-flags {
            +vulkan +opencl +rocm
            +python +lua
            +wayland -X
        }

        compiler {
            CFLAGS "-O3 -march=native"
            MAKEFLAGS "-j16"
        }
    }

    package "pytorch" {
        description "Machine learning framework with NPU support"

        use-flags {
            +rocm +opencl +onnx
            +python
            +lto
        }

        compiler {
            CFLAGS "-O3 -march=native"
            MAKEFLAGS "-j16"
        }
    }

    package "qemu" {
        description "Virtualization with KVM support"

        use-flags {
            +kvm +libvirt
            +virgl +vulkan
            +gtk +wayland
        }

        compiler {
            CFLAGS "-O3 -march=native"
        }
    }

    // ============================================
    // Adapter Configuration
    // ============================================

    adapters {
        nix {
            enabled true
            priority 10
        }

        pkgsrc {
            enabled true
            priority 20
            // PKGSRC-specific: enable source building
            build-from-source true
        }

        pacman {
            enabled true
            priority 30
        }
    }
}

// ============================================
// Common USE Flag Combinations
// ============================================

// Wayland Desktop:
// +wayland -X +gtk +pipewire +dinit

// X11 Desktop:
// +X -wayland +gtk +pulseaudio +systemd

// Minimal Server:
// -X -wayland -gtk -qt -pulseaudio +ssl +ipv6

// Performance Workstation:
// +lto +pgo +march-native +wayland +pipewire

// Development Machine:
// +debug -strip +doc +examples +python +lua
