{
  lib,
  stdenv,
  fetchzip,
  autoPatchelfHook,
  makeWrapper,
  # Core dependencies
  alsa-lib,
  at-spi2-core,
  cairo,
  cups,
  dbus,
  expat,
  fontconfig,
  freetype,
  gdk-pixbuf,
  glib,
  gtk3,
  libarchive,
  libdrm,
  libGL,
  libx11,
  libxcb,
  libxext,
  libxkbcommon,
  mesa,
  nspr,
  nss,
  pango,
  systemd,
  xorg,
  zlib,
  # Additional CEF/Chromium dependencies
  libnotify,
  libpulseaudio,
  libuuid,
  libva,
  pipewire,
  udev,
  wayland,
  jdk17 # for RuneLite/HDOS
  , # for RS3
}:

stdenv.mkDerivation rec {
  pname = "bolt-launcher";
  version = "0.19.1";

  src = fetchzip {
    url = "https://github.com/Adamcake/Bolt/releases/download/${version}/Bolt-Linux.zip";
    sha256 = "sha256-5tGXz2PIz0HIHMGNF7NpYVsB8hnueitNYKi6LtJpuPw=";
    stripRoot = false;
  };

  nativeBuildInputs = [
    autoPatchelfHook
    makeWrapper
  ];

  buildInputs = [
    alsa-lib
    at-spi2-core
    cairo
    cups
    dbus
    expat
    fontconfig
    freetype
    gdk-pixbuf
    glib
    gtk3
    libarchive
    libdrm
    libGL
    libx11
    libxcb
    libxext
    libxkbcommon
    mesa
    nspr
    nss
    pango
    stdenv.cc.cc.lib # for libstdc++.so.6
    systemd
    zlib
    # Additional CEF/Chromium deps
    libnotify
    libpulseaudio
    libuuid
    libva
    pipewire
    udev
    wayland
  ]
  ++ (with xorg; [
    libXcomposite
    libXcursor
    libXdamage
    libXfixes
    libXi
    libXinerama
    libXrandr
    libXrender
    libXScrnSaver
    libxshmfence
    libXtst
    libXxf86vm
  ]);

  dontConfigure = true;
  dontBuild = true;

  installPhase = ''
    runHook preInstall

    # Create directory structure
    mkdir -p $out/bin
    mkdir -p $out/lib/bolt-launcher
    mkdir -p $out/share/applications
    mkdir -p $out/share/icons

    # Copy all files to lib directory
    cp -r ./bolt-launcher/* $out/lib/bolt-launcher/

    # The main executable is 'bolt' (lowercase)
    chmod +x $out/lib/bolt-launcher/bolt

    # Also make chrome-sandbox executable (needed for CEF)
    if [ -f $out/lib/bolt-launcher/chrome-sandbox ]; then
      chmod +x $out/lib/bolt-launcher/chrome-sandbox
    fi

    # Create wrapper script with proper library paths
    makeWrapper $out/lib/bolt-launcher/bolt $out/bin/bolt-launcher \
      --prefix LD_LIBRARY_PATH : "$out/lib/bolt-launcher:${lib.makeLibraryPath buildInputs}" \
      --set BOLT_SKIP_UPDATE "1" \
      --chdir "$out/lib/bolt-launcher" \
      ${lib.optionalString (jdk17 != null) "--prefix PATH : ${jdk17}/bin"}

    # Create a simple desktop file if none exists
    cat > $out/share/applications/bolt-launcher.desktop << EOF
    [Desktop Entry]
    Name=Bolt Launcher
    Comment=Free open-source third-party implementation of the Jagex Launcher
    Exec=$out/bin/bolt-launcher
    Icon=bolt-launcher
    Type=Application
    Categories=Game;
    EOF

    runHook postInstall
  '';

  # Additional runtime fixes
  postFixup = ''
    # The CEF libraries are in the same directory as the main executable
    # Make sure they can find each other
    patchelf --set-rpath "$out/lib/bolt-launcher:${lib.makeLibraryPath buildInputs}" \
      $out/lib/bolt-launcher/bolt || true

    # Patch the CEF libraries
    for lib in $out/lib/bolt-launcher/*.so*; do
      if [ -f "$lib" ]; then
        patchelf --set-rpath "$out/lib/bolt-launcher:${lib.makeLibraryPath buildInputs}" "$lib" || true
      fi
    done
  '';

  meta = with lib; {
    description = "Free open-source third-party implementation of the Jagex Launcher";
    homepage = "https://bolt.adamcake.com/";
    license = licenses.agpl3Only;
    platforms = [
      "x86_64-linux"
      "aarch64-linux"
    ];
    maintainers = with maintainers; [ ];
    sourceProvenance = with sourceTypes; [ binaryNativeCode ];
  };
}
