{
  config,
  lib,
  pkgs,
  ...
}:

{
  imports = [ ];

  options.me = {
    optimizations.enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      example = true;
      description = "Whether we want to enable CPU optimizations (will trigger a rebuild from source).";
    };

    optimizations.arch = lib.mkOption {
      type = lib.types.str;
      default = null;
      example = "znver4";
      description = "The CPU arch for which programs should be optimized.";
    };

    optimizations.build_arch = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
      example = "znver4";
      description = "The CPU arch for which programs should be optimized.";
    };

    optimizations.system_features = lib.mkOption {
      type = lib.types.listOf lib.types.str;
      default = [ ];
      example = [
        "gccarch-znver4"
        "gccarch-znver5"
        "gccarch-skylake"
        "gccarch-x86-64-v3"
        "gccarch-x86-64-v4"
        "benchmark"
        "big-parallel"
        "kvm"
        "nixos-test"
      ];
      description = "The list of CPU features that should be enabled on this machine.";
    };
  };

  config = lib.mkMerge [
    (lib.mkIf (!config.me.optimizations.enable) (
      lib.mkMerge [
        {
          boot.kernelPackages = pkgs.linuxPackagesFor pkgs.linux_6_18;
          # boot.kernelPackages = pkgs.linuxPackagesFor pkgs.linux;
        }
      ]
    ))
    (lib.mkIf config.me.optimizations.enable (
      lib.mkMerge [
        {
          boot.kernelPackages = pkgs.linuxPackagesFor pkgs.linux_me;

          nixpkgs.hostPlatform = {
            gcc.arch = config.me.optimizations.arch;
            gcc.tune = config.me.optimizations.arch;
          };

          nixpkgs.overlays = [
            (
              final: prev:
              let
                addConfig =
                  additionalConfig: pkg:
                  pkg.override (oldconfig: {
                    structuredExtraConfig = pkg.structuredExtraConfig // additionalConfig;
                  });
              in
              {
                linux_me = addConfig {
                  # Server | No preemption - Run until the next tick. Highest throughput but can cause stutter.
                  # PREEMPT = lib.mkOverride 60 lib.kernel.no;
                  # Desktop | Preempt kernel threads only at pre-defined places that call cond_resched().
                  PREEMPT_VOLUNTARY = lib.mkOverride 60 lib.kernel.no;
                  # Low-latency desktop | Full preemption - Kernel threads can be preempted unless they hold a spinlock or are in a no-preemption section.
                  PREEMPT = lib.mkOverride 60 lib.kernel.yes;
                  # RT - All kernel code is preemptible except for a few critical sections.
                  # Middle ground | Real-time tasks preempt immediately like FULL, normal tasks run until the next tick.
                  PREEMPT_LAZY = lib.mkOverride 90 lib.kernel.no;

                  # Google's BBRv3 TCP congestion Control
                  TCP_CONG_BBR = lib.kernel.yes;
                  DEFAULT_BBR = lib.kernel.yes;

                  # Preemptive Full Tickless Kernel at 300Hz
                  HZ = lib.kernel.freeform "300";
                  HZ_300 = lib.kernel.yes;
                  HZ_1000 = lib.kernel.no;
                } prev.linux_6_18; # or prev.linux
              }
            )
            (final: prev: {
              inherit (final.unoptimized)
                assimp
                binaryen
                gsl
                rapidjson
                ffmpeg-headless
                ffmpeg
                pipewire
                chromaprint
                gtkmm
                ;
            })
          ];
        }
      ]
    ))
    (lib.mkIf (config.me.optimizations.build_arch != null) (
      lib.mkMerge [
        {
          # Enable cross-compiling
          nixpkgs.buildPlatform = {
            gcc.arch = config.me.optimizations.build_arch;
            gcc.tune = "generic";
            system = "x86_64-linux";
          };
        }
      ]
    ))
    (lib.mkIf (config.me.optimizations.system_features != [ ]) (
      lib.mkMerge [
        {
          nix.settings.system-features = lib.mkForce config.me.optimizations.system_features;
        }
      ]
    ))
  ];
}
