# Plasma Bigscreen — TV interface for Plasma 6
# Not yet released or packaged in nixpkgs; built from upstream master.
#
# TODO: Remove this file once plasma-bigscreen lands in nixpkgs.
#   Tracking issue: https://github.com/NixOS/nixpkgs/issues/428077
#   Draft nixpkgs PR: https://github.com/NixOS/nixpkgs/pull/428353
#   When available, switch to pkgs.kdePackages.plasma-bigscreen.
#
# Upstream: https://invent.kde.org/plasma/plasma-bigscreen
{
  mkKdeDerivation,
  lib,
  fetchFromGitLab,
  pkg-config,
  # KDE Frameworks 6
  ki18n,
  kdeclarative,
  kcmutils,
  knotifications,
  kio,
  kwayland,
  kwindowsystem,
  ksvg,
  kiconthemes,
  kglobalaccel,
  kdbusaddons,
  # KDE Plasma 6
  plasma-workspace,
  plasma-nano,
  plasma-nm,
  plasma-activities,
  plasma-activities-stats,
  milou,
  libkscreen,
  kdeconnect-kde,
  # Qt 6
  qtdeclarative, # needed for Qt6::QmlPrivate — see QCoro workaround in postPatch
  qtmultimedia,
  qtwebengine,
  # Other
  bluez-qt,
  qcoro,
  plasma-wayland-protocols,
  wayland,
  sdl3,
}:

mkKdeDerivation {
  pname = "plasma-bigscreen";
  version = "unstable-2026-03-07";

  src = fetchFromGitLab {
    domain = "invent.kde.org";
    owner = "plasma";
    repo = "plasma-bigscreen";
    rev = "bd143fea7e386bac1652b8150a3ed3d5ef7cf93c";
    hash = "sha256-y439IX7e0+XqxqFj/4+P5le0hA7DiwA+smDsD0UH/fI=";
  };

  extraNativeBuildInputs = [
    pkg-config
  ];

  extraBuildInputs = [
    # KDE Frameworks
    ki18n
    kdeclarative
    kcmutils
    knotifications
    kio
    kwayland
    kwindowsystem
    ksvg
    kiconthemes
    kglobalaccel
    kdbusaddons
    # Plasma
    plasma-workspace
    plasma-nano
    plasma-nm
    plasma-activities
    plasma-activities-stats
    milou
    libkscreen
    kdeconnect-kde
    # Qt — qtdeclarative is needed for Qt6::QmlPrivate (see postPatch)
    qtdeclarative
    qtmultimedia
    qtwebengine
    # Other
    bluez-qt
    qcoro
    plasma-wayland-protocols
    wayland
    sdl3
  ];

  postPatch = ''
    substituteInPlace bin/plasma-bigscreen-wayland.in \
      --replace @KDE_INSTALL_FULL_LIBEXECDIR@ "${plasma-workspace}/libexec"

    # WORKAROUND: Plasma version numbers must match; we're building an
    # unreleased package against a stable Plasma release. Remove once
    # bigscreen is part of the Plasma release cycle.
    substituteInPlace CMakeLists.txt \
      --replace-fail 'set(PROJECT_VERSION "6.5.80")' 'set(PROJECT_VERSION "${plasma-workspace.version}")'

    # WORKAROUND: QCoro6Qml's cmake config links against Qt6::QmlPrivate but
    # doesn't call find_package to import the target. This is arguably a QCoro
    # packaging bug in nixpkgs (it should propagate qtdeclarative). Remove
    # once QCoro or the nixpkgs plasma-bigscreen package is fixed upstream.
    substituteInPlace CMakeLists.txt \
      --replace-fail 'find_package(QCoro6' 'find_package(Qt6 ''${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS QmlPrivate)
find_package(QCoro6'
  '';

  preFixup = ''
    wrapQtApp $out/bin/plasma-bigscreen-wayland
  '';

  passthru.providedSessions = [
    "plasma-bigscreen-wayland"
  ];

  meta = {
    description = "Plasma shell for TVs (Plasma Bigscreen)";
    homepage = "https://plasma-bigscreen.org";
    license = lib.licenses.gpl2Plus;
    platforms = lib.platforms.linux;
  };
}
