#
# Kernel module for i915 sr-iov support.
#
# vim: et:ts=2:sw=2:
#
{
  stdenv,
  lib,
  nukeReferences,
  fetchFromGitHub,
  linuxPackages,
}:
let
  kernel = linuxPackages.kernel;
in
stdenv.mkDerivation rec {
  name = "i915-sriov-${version}-${kernel.version}";
  version = "2024-05-07";

  src = fetchFromGitHub {
    owner = "strongtz";
    repo = "i915-sriov-dkms";
    rev = "3d7a1b3fa4706d8da316d8e794d54db96856a2b9";
    hash = "sha256-TJk0zmXA2sUhbDu0jsA/1kFeIba8ZIZ0a4joPK4v8Ck=";
  };

  kernelVersion = kernel.version;
  nativeBuildInputs = kernel.moduleBuildDependencies ++ [ nukeReferences ];

  postUnpack = ''
    cp -r $src/drivers/gpu/drm/i915/* .
    ls -lah
  '';

  buildPhase = ''
    make -j$(nproc) -C ${kernel.dev}/lib/modules/${kernel.modDirVersion}/build M=$(pwd) KVER=${kernel.modDirVersion}
  '';

  installPhase = ''
    mkdir -p $out/lib/modules/$kernelVersion/misc
    for x in $(find . -name '*.ko'); do
      nuke-refs $x
      cp $x $out/lib/modules/$kernelVersion/misc/
    done
  '';

  meta = with lib; {
    description = "A kernel module to enable SR-IOV on modern intel devices";
    license = licenses.gpl2;
    platforms = platforms.linux;
  };
}
