{ stdenv, lib, fetchFromGitHub, nukeReferences, linuxPackages }:
let
  kernel = linuxPackages.kernel;
in
stdenv.mkDerivation rec {
  name = "linux-nfc-lenovo-${version}-${kernel.version}";
  version = "0.1";

  src = ./src;
  nativeBuildInputs = kernel.moduleBuildDependencies ++ [ nukeReferences ];

  postUnpack = ''
     cp -r $src .
     ls -lah
   '';

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

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

  meta = with lib; {
    description = "kernel driver for using Lenovo NFC modules with libnfc";
    platforms = platforms.linux;
  };
}
