<commit_before>{ stdenv
, autoconf
, automake
, fetchFromGitHub
, fetchTritonPatch
, libtool

, kernel ? null

, channel
, type
}:

let
  inherit (stdenv.lib)
    any
    optionals
    optionalString
    versionOlder;

  buildKernel = any (n: n == type) [ "kernel" "all" ];
  buildUser = any (n: n == type) [ "user" "all" ];

  source = (import ./sources.nix)."${channel}";

  version = if source ? version then source.version else source.date;
in

assert any (n: n == type) [ "kernel" "user" "all" ];
assert buildKernel -> kernel != null;

assert kernel != null -> versionOlder kernel.version source.maxKernelVersion
  || throw "SPL ${version} is too old for kernel ${kernel.version}";

stdenv.mkDerivation rec {
  name = "spl-${type}-${version}${optionalString buildKernel "-${kernel.version}"}";

  src = fetchFromGitHub {
    owner = "zfsonlinux";
    repo = "spl";
    rev = "${if source ? version then "spl-${source.version}" else source.rev}";
    inherit (source) sha256;
    version = source.fetchzipVersion;
  };

  buildInputs = [
    autoconf
    automake
    libtool
  ];

  patches = [
    (fetchTritonPatch {
      rev = "518382a2bbf31f798bf5271105ac4005510f185d";
      file = "s/spl/0001-Fix-constification.patch";
      sha256 = "96345a84fab6c8a989dc85e238887b91c772784121bff0acd500a49951f44dc0";
    })
    (fetchTritonPatch {
      rev = "518382a2bbf31f798bf5271105ac4005510f185d";
      file = "s/spl/0002-Fix-install-paths.patch";
      sha256 = "9527962abe004eeed79dd6d69d5edcf503123c0ca8e086736d3aabb9ef17064b";
    })
    (fetchTritonPatch {
      rev = "7556b2c93e25e3861f30a656fe1c6a55c08a07a1";
      file = "s/spl/0003-Fix-paths.patch";
      sha256 = "214357f623fb397c79cc3ab1ea0a0bc833d828277646928e242a50e76eb26755";
    })
  ];

  preConfigure = ''
    ./autogen.sh
  '';

  configureFlags = [
    "--with-config=${type}"
  ] ++ optionals buildKernel [
    "--with-linux=${kernel.dev}/lib/modules/${kernel.modDirVersion}/source"
    "--with-linux-obj=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build"
  ];

  # We don't want these compiler security features / optimizations
  # when we are building kernel modules
  optFlags = !buildKernel;
  pie = !buildKernel;
  fpic = !buildKernel;
  noStrictOverflow = !buildKernel;
  fortifySource = !buildKernel;
  stackProtector = !buildKernel;
  optimize = !buildKernel;

  passthru = {
    inherit (source) maxKernelVersion;
    inherit channel;
    buildType = type;
  };<commit_msg>spl: Fix impurities<commit_after>{ stdenv
, autoconf
, automake
, fetchFromGitHub
, fetchTritonPatch
, libtool

, kernel ? null

, channel
, type
}:

let
  inherit (stdenv.lib)
    any
    optionals
    optionalString
    versionOlder;

  buildKernel = any (n: n == type) [ "kernel" "all" ];
  buildUser = any (n: n == type) [ "user" "all" ];

  source = (import ./sources.nix)."${channel}";

  version = if source ? version then source.version else source.date;
in

assert any (n: n == type) [ "kernel" "user" "all" ];
assert buildKernel -> kernel != null;

assert kernel != null -> versionOlder kernel.version source.maxKernelVersion
  || throw "SPL ${version} is too old for kernel ${kernel.version}";

stdenv.mkDerivation rec {
  name = "spl-${type}-${version}${optionalString buildKernel "-${kernel.version}"}";

  src = fetchFromGitHub {
    owner = "zfsonlinux";
    repo = "spl";
    rev = "${if source ? version then "spl-${source.version}" else source.rev}";
    inherit (source) sha256;
    version = source.fetchzipVersion;
  };

  buildInputs = [
    autoconf
    automake
    libtool
  ];

  patches = [
    (fetchTritonPatch {
      rev = "518382a2bbf31f798bf5271105ac4005510f185d";
      file = "s/spl/0001-Fix-constification.patch";
      sha256 = "96345a84fab6c8a989dc85e238887b91c772784121bff0acd500a49951f44dc0";
    })
    (fetchTritonPatch {
      rev = "518382a2bbf31f798bf5271105ac4005510f185d";
      file = "s/spl/0002-Fix-install-paths.patch";
      sha256 = "9527962abe004eeed79dd6d69d5edcf503123c0ca8e086736d3aabb9ef17064b";
    })
    (fetchTritonPatch {
      rev = "7556b2c93e25e3861f30a656fe1c6a55c08a07a1";
      file = "s/spl/0003-Fix-paths.patch";
      sha256 = "214357f623fb397c79cc3ab1ea0a0bc833d828277646928e242a50e76eb26755";
    })
  ];

  preConfigure = ''
    ./autogen.sh
  '';

  configureFlags = [
    "--with-config=${type}"
  ] ++ optionals buildKernel [
    "--with-linux=${kernel.dev}/lib/modules/${kernel.modDirVersion}/source"
    "--with-linux-obj=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build"
  ];

  # Fix build impurities
  preFixup = ''
    find "$out" -name Module.symvers -exec sed -i "s,$NIX_BUILD_TOP,/no-such-path,g" {} \;
  '';

  # We don't want these compiler security features / optimizations
  # when we are building kernel modules
  optFlags = !buildKernel;
  pie = !buildKernel;
  fpic = !buildKernel;
  noStrictOverflow = !buildKernel;
  fortifySource = !buildKernel;
  stackProtector = !buildKernel;
  optimize = !buildKernel;

  passthru = {
    inherit (source) maxKernelVersion;
    inherit channel;
    buildType = type;
  };
{'fraction_non_alphanumeric': 0.13467217956290609, 'fraction_numerical': 0.09214412285883047, 'mean_word_length': 3.5438282647584973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '498270', 'n_tokens_mistral': 2113, 'n_tokens_neox': 1826, 'n_words': 346}