{ lib, config, ... }:
let
  inherit (lib.modules)
    mkIf
    mkForce
    mkMerge
    mkDefault
    ;
  inherit (lib.options) mkEnableOption;

  cfg = config.garden.system.boot;
in
{
  options.garden.system.boot = {
    initrd = {
      tweaks.enable = mkEnableOption "quality of life tweaks for the initrd stage" // {
        default = true;
      };

      optimizeCompressor =
        mkEnableOption ''
          initrd compression algorithm optimizations for size.
          Enabling this option will force initrd to use zstd (default) with
          level 19 and -T0 (STDIN). This will reduce thee initrd size greatly
          at the cost of compression speed.
          Not recommended for low-end hardware.
        ''
        // {
          default = config.garden.profiles.workstation.enable;
          defaultText = "config.garden.profiles.workstation.enable";
        };
    };
  };

  config.boot = {
    consoleLogLevel = 3;

    extraModprobeConfig = mkDefault "options hid_apple fnmode=1";

    # shared config between bootloaders
    # they are set unless system.boot.loader != none
    loader = {
      # if set to 0, space needs to be held to get the boot menu to appear
      timeout = mkForce 2;

      # copy boot files to /boot so that /nix/store is not required to boot
      # it takes up more space but it makes my messups a bit safer
      generationsDir.copyKernels = true;

      # we need to allow installation to modify EFI variables
      efi.canTouchEfiVariables = true;
    };

    # initrd and kernel tweaks
    # read what each parameter or module does before doing so, it will defo break something otherwise
    initrd = mkMerge [
      (mkIf cfg.initrd.tweaks.enable {
        # Verbosity of the initrd
        # disabling verbosity removes only the mandatory messages generated by the NixOS
        verbose = false;

        kernelModules = [
          "nvme"
          "xhci_pci"
          "ahci"
          "btrfs"
          "sd_mod"
          "dm_mod"
        ];

        availableKernelModules = [
          "vmd"
          "usbhid"
          "sd_mod"
          "sr_mod"
          "dm_mod"
          "uas"
          "usb_storage"
          "rtsx_usb_sdmmc"
          "rtsx_pci_sdmmc" # Realtek SD card interface (btw i hate realtek)
          "ata_piix"
          "virtio_pci"
          "virtio_scsi"
          "ehci_pci"
        ];
      })

      (mkIf cfg.initrd.optimizeCompressor {
        compressor = "zstd";
        compressorArgs = [
          "-19"
          "-T0"
        ];
      })
    ];
  };
}
