Building a kernel module with FHS build scripts

I have a capture card, a Datapath Vision-E1S.
Datapath provide a Linux Kernel Module, but unfortunately it’s all wrapped up in a very weird and proprietary build process.

The intended method of installation is running ./scripts/install.kernel, but the entire thing assumes normal kernel paths and results in a huge torrent of errors on NixOS.

This is what I have so far;

{ stdenv, lib, fetchurl, kernel }:

stdenv.mkDerivation rec {
  pname = "datapath-vision";
  version = "7.28.0";

  src = fetchurl {
    url = "http://www.datapathsoftware.com/supportdownloads/linux/vision-driver-app/VisionInstall-7.28.0.7402.tar.gz";
    sha256 = "sha256-7IVliarjvhCUAsQF11fc5HvRe8jzFaY+ejk29ozzdU0=";
  };

  sourceRoot = ".";

  nativeBuildInputs = kernel.moduleBuildDependencies;

  postPatch = ''
    patchShebangs scripts/
    substituteInPlace ./scripts/rgb133config.sh --replace-fail 'PREEMPT_RT=`cat /boot/config-$KVER | grep "PREEMPT_RT"`' 'PREEMPT_RT=$(uname -v | grep -i "PREEMPT RT")'
  '';

  preBuild = ''
    ./scripts/rgb133config.sh gcc x86_64 ${kernel.dev}/lib/modules/${kernel.modDirVersion} $(pwd)/include test_linux_features
  '';

  makeFlags = [
    "-C ${kernel.dev}/lib/modules/${kernel.modDirVersion}/build"
    "M=$(PWD)"
    "TYPE=133"
    "ccflags-y=-I$(PWD)/include"
  ];

  buildFlags = [ "modules" ];

  installPhase = ''
    ls -la include/
    mkdir -p $out/lib/modules/${kernel.modDirVersion}/extra
    cp rgb133.ko $out/lib/modules/${kernel.modDirVersion}/extra/
  '';

  meta = with lib; {
    description = "Datapath Vision Linux Kernel Driver";
    homepage = "https://www.datapath.co.uk/";
    license = licenses.unfree;
    platforms = platforms.linux;
  };
}

I have managed to fix some errors with rgb133config.sh so it at least makes rgb133config.h and starts to build, but it totally falls apart with all sorts of errors that seem to imply the headers aren’t being imported correctly.

I’ve also tried running it in an fhs environment, but I ran into all sorts of issues with it trying to build against my kernel instead of the nixpkgs default when testing and everything ending up throwing a torrent of errors.

Theoretically it should work, the drivers have support for at least 6.8.0 so they should build on modern kernels. I’m nowhere near knowledgeable enough with regards to the kernel to actually understand any issues, though.

If anyone could give me some help here I would really appreciate it, thanks.