{ fetchFromGitHub, runCommand, kernel, writeShellScriptBin, nukeReferences }:

with kernel.stdenv.lib;
let
  out = "${placeholder "out"}/lib/modules/${kernel.modDirVersion}/kernel/drivers/net/wireless";
in kernel.stdenv.mkDerivation rec {
  name = "rtl8723cs-${kernel.version}-${version}";
  version = "2018-11-20";

  src = fetchFromGitHub {
    owner = "icenowy";
    repo = "rtl8723cs";
    rev = "48ec8c30159de494672dab17b21d67d680da73b7";
    sha256 = "0z2m33237i3gmz0xwl6f6q45w997ifld9g8lj1smcx52ammaljn3";
  };

  nativeBuildInputs = [
    nukeReferences
  ];

  postPatch = ''
    sed -i 's/^.*depmod.*$//g' Makefile
  '';

  makeFlags = concatStringsSep " " [
    "ARCH=${kernel.stdenv.hostPlatform.platform.kernelArch}"
    # "CROSS_COMPILE=${kernel.stdenv.hostPlatform.config}-"
    "KSRC=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build"
    "MODDESTDIR=${out}"
  ];

  preInstall = ''
    mkdir -p ${out}
  '';

  postInstall = ''
    nuke-refs $(find $out -name "*.ko")
  '';
}