#!/bin/bash # NIP Musl Static Build Script -ann" Optimized ๐ŸŽ…โœจ # Builds the smallest possible static binary using musl libc set -e echo "๐ŸŽ… Building NIP v0.2.0 'Weihnachtsmann' - Musl Optimized Edition" echo "==================================================================" echo "" # Check if Nim is available if ! command -v nim &> /dev/null; then echo "โŒ Error: Nim compiler not found" echo " Please install Nim: https://nim-lang.org/install.html" exit 1 fi # Show Nim version echo "๐Ÿ“‹ Nim version: $(nim --version | head -1)" echo "" # Find musl-gcc MUSL_GCC="" if command -v x86_64-linux-musl-gcc &> /dev/null; then MUSL_GCC="x86_64-linux-musl-gcc" echo "โœ… Found musl-gcc in PATH" elif [ -f "/opt/cross/x86_64-linux-musl-cross/bin/x86_64-linux-musl-gcc" ]; then MUSL_GCC="/opt/cross/x86_64-linux-musl-cross/bin/x86_64-linux-musl-gcc" echo "โœ… Found musl-gcc at: $MUSL_GCC" else echo "โŒ Error: musl-gcc not found" echo " Install musl-cross-bin: yay -S musl-cross-bin" exit 1 fi echo "" # Clean previous builds echo "๐Ÿงน Cleaning previous builds..." rm -f nip nip-musl nip-static-musl echo "" # Build optimized musl static binary echo "๐Ÿ”จ Building optimized musl static binary..." echo " This may take a few minutes..." echo "" nim c \ --define:static \ --define:release \ --define:danger \ --opt:speed \ --mm:orc \ --threads:on \ --passC:-flto \ --passL:-flto \ --passL:-static \ --passL:-s \ --passC:-ffunction-sections \ --passC:-fdata-sections \ --passL:-Wl,--gc-sections \ --passC:-march=x86-64-v2 \ --passC:-mtune=generic \ --gcc.exe:"$MUSL_GCC" \ --gcc.linkerexe:"$MUSL_GCC" \ --hints:off \ --warnings:off \ --out:nip-musl \ nip.nim if [ ! -f "nip-musl" ]; then echo "โŒ Build failed!" exit 1 fi # Show binary info echo "" echo "โœ… Build successful!" echo "" echo "๐Ÿ“Š Binary Information:" ls -lh nip-musl echo "" # Verify static linking echo "๐Ÿ” Verifying static linking..." if ldd nip-musl 2>&1 | grep -q "not a dynamic executable"; then echo "โœ… Binary is fully static (no dynamic dependencies)" FULLY_STATIC=true elif ldd nip-musl 2>&1 | grep -q "statically linked"; then echo "โœ… Binary is statically linked" FULLY_STATIC=true else echo "โš ๏ธ Binary has dynamic dependencies:" ldd nip-musl 2>&1 | head -10 FULLY_STATIC=false fi echo "" # Show file details echo "๐Ÿ“‹ File Details:" file nip-musl echo "" # Size comparison echo "๐Ÿ“ Size Comparison:" if [ -f "nip-static" ]; then GLIBC_SIZE=$(ls -lh nip-static | awk '{print $5}') echo " Glibc static: $GLIBC_SIZE" fi MUSL_SIZE=$(ls -lh nip-musl | awk '{print $5}') echo " Musl static: $MUSL_SIZE" echo "" # Test the binary echo "๐Ÿงช Testing binary..." if ./nip-musl --version > /dev/null 2>&1; then echo "โœ… Binary test passed" ./nip-musl --version 2>/dev/null || echo " (version check requires root)" else echo "โš ๏ธ Binary test failed (but this is expected without root)" fi echo "" # Check for UPX availability echo "๐Ÿ” Checking for UPX compression..." if command -v upx &> /dev/null; then echo "โœ… UPX found - attempting compression..." echo "" # Create compressed version cp nip-musl nip-musl-upx upx --best --lzma nip-musl-upx 2>&1 | tail -5 if [ -f "nip-musl-upx" ]; then echo "" echo "๐Ÿ“ฆ UPX Compression Results:" ORIGINAL=$(ls -lh nip-musl | awk '{print $5}') COMPRESSED=$(ls -lh nip-musl-upx | awk '{print $5}') echo " Original: $ORIGINAL" echo " Compressed: $COMPRESSED" echo "" # Test compressed binary echo "๐Ÿงช Testing compressed binary..." if ./nip-musl-upx --version > /dev/null 2>&1; then echo "โœ… Compressed binary works!" else echo "โš ๏ธ Compressed binary test failed" fi echo "" fi else echo "โ„น๏ธ UPX not found - skipping compression" echo " Install UPX for additional size reduction: sudo pacman -S upx" echo "" fi # Create deployment package RELEASE_DIR="nip-v0.2.0-weihnachtsmann-musl-$(uname -s)-$(uname -m)" echo "๐Ÿ“ฆ Creating deployment package: $RELEASE_DIR" mkdir -p "$RELEASE_DIR" # Copy musl binary cp nip-musl "$RELEASE_DIR/nip" chmod +x "$RELEASE_DIR/nip" # Copy compressed version if it exists if [ -f "nip-musl-upx" ]; then cp nip-musl-upx "$RELEASE_DIR/nip-compressed" chmod +x "$RELEASE_DIR/nip-compressed" fi # Create README cat > "$RELEASE_DIR/README.md" << 'EOF' # NIP v0.2.0 "Weihnachtsmann" ๐ŸŽ…โœจ - Musl Optimized Edition ## Ultra-Minimal Static Binary This is a **fully static binary built with musl libc** - the smallest possible NIP binary for maximum portability. ### Features - โœ… Fully static (no dependencies) - โœ… Musl libc (smallest possible) - โœ… Link-Time Optimization (LTO) - โœ… Section garbage collection - โœ… Symbol stripping - โœ… Works on any Linux kernel 4.19+ ### Quick Start ```bash # Install sudo cp nip /usr/local/bin/nip sudo chmod +x /usr/local/bin/nip # Initialize sudo nip setup # Start using nip build nginx +http3 nip graft aur firefox ``` ### Compressed Version If `nip-compressed` is included, it's UPX-compressed for even smaller size: ```bash sudo cp nip-compressed /usr/local/bin/nip ``` **Trade-off:** Slightly slower startup (~100-200ms decompression) ### Documentation For full documentation, visit: https://git.maiwald.work/Nexus/NexusToolKit EOF # Create install script cat > "$RELEASE_DIR/install.sh" << 'EOF' #!/bin/bash set -e if [ "$EUID" -ne 0 ]; then echo "โš ๏ธ Please run as root: sudo ./install.sh" exit 1 fi echo "๐ŸŽ… Installing NIP v0.2.0 'Weihnachtsmann' (Musl Edition)" # Choose binary if [ -f "nip-compressed" ]; then echo "๐Ÿ“ฆ Found compressed binary - using for smaller size" cp nip-compressed /usr/local/bin/nip else cp nip /usr/local/bin/nip fi chmod +x /usr/local/bin/nip echo "โœ… Installed to /usr/local/bin/nip" echo "" echo "๐Ÿš€ Run 'nip setup' to initialize" EOF chmod +x "$RELEASE_DIR/install.sh" # Create tarball echo "๐Ÿ“ฆ Creating release tarball..." tar -czf "$RELEASE_DIR.tar.gz" "$RELEASE_DIR" echo " Created: $RELEASE_DIR.tar.gz" echo "" # Final summary echo "๐ŸŽ‰ Musl build complete!" echo "" echo "๐Ÿ“‹ Build Summary:" echo " Binary: nip-musl" if [ "$FULLY_STATIC" = true ]; then echo " Status: โœ… Fully static (no dependencies)" else echo " Status: โš ๏ธ Has some dynamic dependencies" fi echo " Size: $(ls -lh nip-musl | awk '{print $5}')" echo " Libc: musl (optimal)" echo "" echo "๐Ÿ“ฆ Deployment Package:" echo " Directory: $RELEASE_DIR/" echo " Tarball: $RELEASE_DIR.tar.gz" echo "" echo "๐ŸŽ… Musl optimized build ready! โœจ"