{ ... }: {
  flake.nixosModules.systemChromiumHevcVaapi =
    { config, lib, pkgs, ... }:
    let
      cfg = config.chiasson.system.chromiumHevc;
    in
    {
      options.chiasson.system.chromiumHevc = {
        enable = lib.mkEnableOption ''
          VA-API packages for Chromium HEVC (Intel iHD + optional NVIDIA nvidia-vaapi-driver).
          Pair with `wisdomBrowsersChromiumHevc` on the user side.
        '';

        gpu = lib.mkOption {
          type = lib.types.enum [
            "intel"
            "nvidia"
          ];
          default = "intel";
          description = ''
            VA-API stack for the `*-hevc` browser launchers. Use **intel** for Jellyfin
            (Chromium + nvidia-vaapi-driver is unsupported upstream and fails with
            `failed Initialize()ing the frame pool` in Jellyfin). **nvidia** adds the
            nvidia-vaapi-driver and keep renderD129 + VaapiOnNvidiaGPUs for experiments only.
          '';
        };
      };

      config = lib.mkIf cfg.enable {
        hardware.graphics.enable = lib.mkDefault true;
        hardware.graphics.extraPackages = lib.mkAfter (
          lib.optionals (cfg.gpu == "nvidia") [
            pkgs.nvidia-vaapi-driver
          ]
        );
      };
    };

  flake.homeManagerModules.wisdomBrowsersChromiumHevc =
    { config, lib, pkgs, osConfig ? { }, ... }:
    let
      root = config.chiasson.home;
      cfg = config.chiasson.home.browsers.chromiumHevc;
      gpu = lib.attrByPath [ "chiasson" "system" "chromiumHevc" "gpu" ] "intel" osConfig;

      browserCatalog = {
        "google-chrome" = {
          package = pkgs.google-chrome;
          binary = "google-chrome-stable";
          launcher = "google-chrome-hevc";
          desktopName = "Google Chrome (HEVC)";
          icon = "google-chrome";
        };
        chromium = {
          package = pkgs.chromium;
          binary = "chromium";
          launcher = "chromium-hevc";
          desktopName = "Chromium (HEVC)";
          icon = "chromium";
        };
        "microsoft-edge" = {
          package = pkgs.microsoft-edge;
          binary = "microsoft-edge-stable";
          launcher = "microsoft-edge-hevc";
          desktopName = "Microsoft Edge (HEVC)";
          icon = "microsoft-edge";
        };
      };

      gpuProfiles = {
        intel = {
          driver = "iHD";
          drmDevice = "/dev/dri/renderD128";
          enableFeatures = [
            "VaapiVideoDecodeLinuxGL"
            "VaapiVideoDecoder"
            "VaapiIgnoreDriverChecks"
            "PlatformHEVCDecoderSupport"
            "UseMultiPlaneFormatForHardwareVideo"
            "AcceleratedVideoDecodeLinuxGL"
          ];
          disableFeatures = [
            "AcceleratedVideoDecodeLinuxZeroCopyGL"
          ];
        };
        nvidia = {
          driver = "nvidia";
          drmDevice = "/dev/dri/renderD129";
          enableFeatures = [
            "VaapiVideoDecoder"
            "VaapiIgnoreDriverChecks"
            "PlatformHEVCDecoderSupport"
            "VaapiOnNvidiaGPUs"
            "AcceleratedVideoDecodeLinuxGL"
          ];
          disableFeatures = [
            "AcceleratedVideoDecodeLinuxZeroCopyGL"
            "UseMultiPlaneFormatForHardwareVideo"
            "VaapiVideoDecodeLinuxGL"
          ];
        };
      };

      activeGpu = gpuProfiles.${gpu};
      enableFeatures = lib.concatStringsSep "," activeGpu.enableFeatures;
      disableFeatures = lib.concatStringsSep "," activeGpu.disableFeatures;

      mkDesktopItem =
        spec:
        pkgs.makeDesktopItem {
          name = spec.launcher;
          desktopName = spec.desktopName;
          genericName = "Web Browser";
          exec = "${spec.launcher} %U";
          icon = spec.icon;
          categories = [
            "Network"
            "WebBrowser"
          ];
          mimeTypes = [
            "text/html"
            "text/xml"
            "application/xhtml+xml"
            "x-scheme-handler/http"
            "x-scheme-handler/https"
          ];
        };

      mkChromiumHevc =
        packageName:
        let
          spec = browserCatalog.${packageName};
          browser = spec.package;
          launcherName = spec.launcher;
          desktopItem = mkDesktopItem spec;
        in
        pkgs.runCommand launcherName
          {
            inherit (browser) version;
            nativeBuildInputs = [ pkgs.makeWrapper ];
            passthru = { inherit browser; };
          }
          ''
            mkdir -p $out/bin $out/share/applications

            makeWrapper ${browser}/bin/${spec.binary} $out/bin/${launcherName} \
              --set LIBVA_DRIVER_NAME ${lib.escapeShellArg activeGpu.driver} \
              --set LIBVA_DRM_DEVICE ${lib.escapeShellArg activeGpu.drmDevice} \
              --set NVD_BACKEND direct \
              --add-flags "--enable-features=${enableFeatures}" \
              --add-flags "--disable-features=${disableFeatures}" \
              ${lib.concatMapStringsSep " " (a: "--add-flags ${lib.escapeShellArg a}") cfg.extraCommandLineArgs}

            cp ${desktopItem}/share/applications/${launcherName}.desktop \
              $out/share/applications/${launcherName}.desktop
          '';

      selectedPackages = lib.filter (
        name: lib.meta.availableOn pkgs.stdenv.hostPlatform browserCatalog.${name}.package
      ) cfg.packages;

      wrappers = map mkChromiumHevc selectedPackages;
    in
    {
      options.chiasson.home.browsers.chromiumHevc = {
        enable =
          lib.mkEnableOption ''
            `google-chrome-hevc`: Chromium with VA-API HEVC for Jellyfin / MSE playback.

            GPU stack comes from `chiasson.system.chromiumHevc.gpu` (default **intel**).
            Requires `chiasson.system.chromiumHevc.enable` on NixOS.
          ''
          // {
            default = true;
          };

        packages = lib.mkOption {
          type = lib.types.listOf (lib.types.enum (lib.attrNames browserCatalog));
          default = if pkgs.stdenv.hostPlatform.isAarch64 then [ "chromium" ] else [ "google-chrome" ];
          description = "Chromium-based browsers to wrap.";
        };

        extraCommandLineArgs = lib.mkOption {
          type = lib.types.listOf lib.types.str;
          default = [ ];
          description = "Extra Chromium flags appended after the HEVC profile flags.";
        };
      };

      config = lib.mkIf (root.enable && cfg.enable) {
        home.packages = wrappers;
      };
    };
}
