{
  stdenv,
  pkgs,
  lib,
  callPackage,
  zfs_2_3,
  # 1. Add 'inputs' generic argument (passed by Snowfall Lib)
  inputs ? { },
  # 2. Make 'self' optional, defaulting to inputs.self (handles both Overlay and Snowfall cases)
  self ? inputs.self,
  ...
}@args: # 3. Rename this capture from 'inputs' to 'args' to avoid variable name collision

let
  inherit (stdenv) isx86_64 isAarch64 isLinux;
  inherit (lib.trivial) importJSON;

  arch = (if isx86_64 then "x86_64" else "aarch64");

  # ######################################################
  #                   Source Versions                    #
  # ######################################################
  mainVersions = importJSON ./versions.json;
  ltsVersions = importJSON ./versions-lts.json;
  rcVersions = importJSON ./versions-rc.json;
  hardenedVersions = importJSON ./versions-hardened.json;

  # ######################################################
  #                   Base LTO Config                    #
  # ######################################################
  ltoKernelAttrs = {
    taste = "linux-cachyos";
    pname = "cachyos-lto";
    configPath = ./config-nix/${arch}-linux/cachyos-lto.${arch}-linux.nix;

    # Pass the unified arguments (including the resolved 'self') to llvm-pkgs
    inherit (import ./lib/llvm-pkgs.nix (args // { inherit self; })) callPackage;
    useLTO = "thin";

    packagesExtend = import ./lib/llvm-module-overlay.nix args;

    zfsOverride = {
      inherit (pkgs)
        autoreconfHook269
        util-linux
        coreutils
        perl
        udevCheckHook
        zlib
        libuuid
        python3
        attr
        openssl
        libtirpc
        nfs-utils
        gawk
        gnugrep
        gnused
        systemd
        smartmontools
        sysstat
        pkg-config
        curl
        pam
        nix-update-script
        ;
    };

    description = "Linux EEVDF-BORE scheduler Kernel by CachyOS built with LLVM and Thin LTO";
  };

  serverLtoKernelAttrs = ltoKernelAttrs // {
    taste = "linux-cachyos-server";
    pname = "cachyos-server-lto";
    configPath = ./config-nix/${arch}-linux/cachyos-server-lto.${arch}-linux.nix;
    basicCachy = false;
    cpuSched = "eevdf";
    ticksHz = 300;
    tickRate = "idle";
    preempt = "server";
    hugePages = "madvise";
    withDAMON = true;
    withNTSync = false;
    withHDR = false;
  };

  # ######################################################
  #                     Misc Utils                       #
  # ######################################################

  # Evaluation hack
  brokenReplacement = pkgs.hello.overrideAttrs (prevAttrs: {
    meta = prevAttrs.meta // {
      platform = [ ];
      broken = true;
    };
  });

  isUnsupported = !(isx86_64 || isAarch64) || !isLinux;

  mkCachyKernel =
    if isUnsupported then
      # Evaluation hack
      _attrs: {
        kernel = brokenReplacement;
        recurseForDerivations = false;
      }
    else
      {
        callPackage ? args.callPackage,
        ...
      }@attrs:
      callPackage ./packages-for.nix (
        {
          versions = mainVersions;
          inherit inputs;
          cachyOverride = newAttrs: mkCachyKernel (attrs // newAttrs);
        }
        // attrs
      );

  # ######################################################
  #                   Base GCC Config                    #
  # ######################################################

  gccKernelAttrs = {
    taste = "linux-cachyos";
    pname = "cachyos-gcc";
    configPath = ./config-nix/${arch}-linux/cachyos-gcc.${arch}-linux.nix;
    # since all flavors use the same versions.json, we just need the updateScript in one of them
    withUpdateScript = "stable";
  };

  # ######################################################
  #                   Base LTS Config                    #
  # ######################################################

  ltsAttrs = {
    versions = ltsVersions;
    withUpdateScript = "lts";

    # Prevent building kernel modules for LTS kernel
    packagesExtend =
      _kernel: _final: prev:
      prev // { recurseForDerivations = false; };
  };

  # ######################################################
  #                    Base RC Config                    #
  # ######################################################

  rcAttrs = {
    versions = rcVersions;
    withUpdateScript = "rc";

    # Prevent building kernel modules for rc kernel
    packagesExtend =
      _kernel: _final: prev:
      prev // { recurseForDerivations = false; };
  };

  # ######################################################
  #                  Base Server Config                  #
  # ######################################################

  serverAttrs = {
    basicCachy = false;
    cpuSched = "eevdf";
    ticksHz = 300;
    tickRate = "idle";
    preempt = "server";
    hugePages = "madvise";
    withDAMON = true;
    withNTSync = false;
    withHDR = false;
    description = "Linux EEVDF scheduler Kernel by CachyOS targeted for Servers";
  };

  # ######################################################
  #                 Base Hardened Config                 #
  # ######################################################

  hardenedAttrs = {
    cpuSched = "hardened";

    versions = hardenedVersions;
    withUpdateScript = "hardened";

    withNTSync = false;
    withHDR = false;
  };
in
{
  inherit
    mainVersions
    rcVersions
    hardenedVersions
    mkCachyKernel
    ;

  # ######################################################
  #                      GCC Kernels                     #
  # ######################################################

  # Base GCC
  cachyos-gcc = mkCachyKernel gccKernelAttrs;

  # GCC LTS
  cachyos-lts = mkCachyKernel (
    gccKernelAttrs
    // ltsAttrs
    // {
      taste = "linux-cachyos-lts";
      pname = "cachyos-lts";
      configPath = ./config-nix/${arch}-linux/cachyos-lts.${arch}-linux.nix;
    }
  );

  # GCC RC
  cachyos-rc = mkCachyKernel (
    gccKernelAttrs
    // rcAttrs
    // {
      taste = "linux-cachyos-rc";
      pname = "cachyos-rc";
      configPath = ./config-nix/${arch}-linux/cachyos-rc.${arch}-linux.nix;
    }
  );

  # Server GCC
  cachyos-server = mkCachyKernel (
    gccKernelAttrs
    // serverAttrs
    // {
      taste = "linux-cachyos-server";
      pname = "cachyos-server";
      configPath = ./config-nix/${arch}-linux/cachyos-server.${arch}-linux.nix;
    }
  );

  # Hardened GCC
  cachyos-hardened = mkCachyKernel (
    gccKernelAttrs
    // hardenedAttrs
    // {
      taste = "linux-cachyos-hardened";
      pname = "cachyos-hardened";
      configPath = ./config-nix/${arch}-linux/cachyos-hardened.${arch}-linux.nix;
    }
  );

  # Deckify GCC
  cachyos-deckify = mkCachyKernel (
    gccKernelAttrs
    // {
      taste = "linux-cachyos-deckify";
      pname = "cachyos-deckify";
      configPath = ./config-nix/${arch}-linux/cachyos-deckify.${arch}-linux.nix;
    }
  );

  # ######################################################
  #                      LTO Kernels                     #
  # ######################################################

  # Base LTO
  cachyos-lto = mkCachyKernel ltoKernelAttrs;

  # LTO Full
  cachyos-lto-full = mkCachyKernel (
    ltoKernelAttrs
    // {
      configPath = ./config-nix/${arch}-linux/cachyos-lto-full.${arch}-linux.nix;
      useLTO = "full";
    }
  );

  # LTO Zen Version 4
  cachyos-lto-znver4 = mkCachyKernel (
    ltoKernelAttrs
    // {
      pname = "cachyos-lto-znver4";
      configPath = ./config-nix/${arch}-linux/cachyos-znver4.${arch}-linux.nix;
    }
  );

  # LTS LTO
  cachyos-lts-lto = mkCachyKernel (
    ltoKernelAttrs
    // ltsAttrs
    // {
      taste = "linux-cachyos-lts";
      pname = "cachyos-lts-lto";
      configPath = ./config-nix/${arch}-linux/cachyos-lts-lto.${arch}-linux.nix;
    }
  );

  # Hardened LTO
  cachyos-hardened-lto = mkCachyKernel (
    ltoKernelAttrs
    // hardenedAttrs
    // {
      taste = "linux-cachyos-hardened";
      pname = "cachyos-hardened-lto";
      configPath = ./config-nix/${arch}-linux/cachyos-hardened-lto.${arch}-linux.nix;
    }
  );

  # RC LTO
  cachyos-rc-lto = mkCachyKernel (
    ltoKernelAttrs
    // rcAttrs
    // {
      taste = "linux-cachyos-rc";
      pname = "cachyos-rc-lto";
      configPath = ./config-nix/${arch}-linux/cachyos-rc-lto.${arch}-linux.nix;
    }
  );

  # RC LTO Zen Version 4
  cachyos-rc-lto-znver4 = mkCachyKernel (
    ltoKernelAttrs
    // rcAttrs
    // {
      taste = "linux-cachyos-rc";
      pname = "cachyos-rc-lto-znver4";
      configPath = ./config-nix/${arch}-linux/cachyos-rc-znver4.${arch}-linux.nix;
    }
  );

  # Server LTO
  cachyos-server-lto = mkCachyKernel (
    ltoKernelAttrs
    // serverAttrs
    // {
      taste = "linux-cachyos-server";
      pname = "cachyos-server-lto";
      configPath = ./config-nix/${arch}-linux/cachyos-server-lto.${arch}-linux.nix;
    }
  );

  # Server LTO
  cachyos-server-lto-16k = mkCachyKernel (
    ltoKernelAttrs
    // serverAttrs
    // {
      taste = "linux-cachyos-server";
      pname = "cachyos-server-lto";
      configPath = ./config-nix/${arch}-linux/cachyos-server-lto-16k.${arch}-linux.nix;
      pageSize = "16k";
    }
  );

  # Server LTO Zen Version 4
  cachyos-server-lto-znver4 = mkCachyKernel (
    serverLtoKernelAttrs
    // {
      configPath = ./config-nix/${arch}-linux/cachyos-server-znver4.${arch}-linux.nix;
      pname = "cachyos-server-lto-znver4";
    }
  );

  # Deckify LTO
  cachyos-deckify-lto = mkCachyKernel (
    ltoKernelAttrs
    // {
      pname = "linux-cachyos-deckify-lto";
      configPath = ./config-nix/${arch}-linux/cachyos-deckify-lto.${arch}-linux.nix;
    }
  );

  # ######################################################
  #                       Cachy ZFZ                      #
  # ######################################################

  zfs = zfs_2_3.overrideAttrs (prevAttrs: {
    src = if isUnsupported then brokenReplacement else (mkCachyKernel gccKernelAttrs).zfs_cachyos.src;
    patches = [ ];
    passthru = prevAttrs.passthru // {
      kernelModuleAttribute = "zfs_cachyos";
    };
    postPatch = builtins.replaceStrings [ "grep --quiet '^Linux-M" ] [ "# " ] prevAttrs.postPatch;
  });
}
