# Do not modify this file! It was generated by ‘nixos-generate-config’
# and may be overwritten by future invocations. Please make changes
# to /etc/nixos/configuration.nix instead.

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

let  
  name = "tolga";
  extraBackends = [ pkgs.epkowa ];  
in

{
  imports = [ (modulesPath + "/installer/scan/not-detected.nix") ];

  boot = {
    initrd = {
    #---------------------------------------------------------------------
    # Initrd configuration, enables systemd services in the initial ramdisk (initrd).
    #---------------------------------------------------------------------  
      systemd.enable = true;    # Enables systemd in the initrd for a modern boot process
      verbose = false;          # Silent boot, change to true if you want detailed boot messages
      availableKernelModules = [
        "ahci"            # For SATA devices, likely needed.
        "battery"         # For battery management, likely needed for a laptop.
        "ehci_pci"        # Kernel module for PCIe USB 2.0 controllers.
        "nvme"            # For NVMe SSDs, check your hardware.
        "rtsx_pci_sdmmc"  # For Realtek card reader, check your hardware.
        "sd_mod"          # For SCSI disk devices, likely needed.
        "sdhci_pci"       # Kernel module for PCIe SDHCI controllers.
        "uas"             # Kernel module for USB Attached SCSI.
        "usb_storage"     # For USB storage devices, likely needed.
        "xhci_pci"        # For USB 3.0 controllers, likely needed.
        # "i915"            # For Intel integrated graphics, likely needed.
      ];

    };

    kernelParams = [ 
      "mem_sleep_default=deep" 
      "elevator=kyber" # Change IO scheduler to Kyber
      "io_delay=none" # Disable I/O delay accounting
      "iomem=relaxed" # Allow more relaxed I/O memory access
      "iommu=pt"
      "irqaffinity=0-3" # Set IRQ affinity to CPUs 0-3 (Intel Core i7-3667U specific)
      "loglevel=3" # Set kernel log level to 3 (default)
      "mitigations=off" # Disable CPU mitigations for security vulnerabilities
      "noirqdebug" # Disable IRQ debugging
      "pti=off" # Disable Kernel Page Table Isolation (PTI)
      "quiet" # Suppress verbose kernel messages during boot
      "rd.systemd.show_status=false" # Disable systemd boot status display
      "rd.udev.log_level=3" # Set udev logging level to 3
      "rootdelay=0" # No delay when mounting root filesystem
      "splash" # Enable graphical boot splash screen
      "threadirqs" # Enable threaded interrupt handling
      "udev.log_level=3" # Set udev logging level to 3
      "vt.global_cursor_default=0" # Disable blinking cursor in text mode
        "zswap.enabled=1"
        "pcie_aspm=off"
         "nmi_watchdog=0"
         "video.allow_duplicates=1"  
      # "systemd.show_status=auto"   # Commented out, not used in this configuration
    
    ];

    kernelModules = [
      "i965"                      # Kernel module for Intel integrated graphics.
      "i965.modeset=1"            # Enables modesetting for the Intel i915 driver.
      "kvm-intel"                 # Kernel module for Intel hardware virtualization support (KVM).
      # "fbcon=nodefer"           # Prevents the kernel from blanking Plymouth out of the framebuffer.
      # "logo.nologo"             # Disables boot logo if any.
      # "video=eDP-1:1920x1200@60" # Sets the display resolution and refresh rate for the specified display.
    ];

    extraModprobeConfig = lib.mkMerge [
      # Control and optimize how an application utilizes the processor resources based on i7-3667U
      "options nptl Thread.ProcessorCount=4 Thread.MaxProcessorCount=4 Thread.MinFreeProcessorCount=1 Thread.JobThreadPriority=0"
      
      "options i965 enable_dc=4 enable_fbc=1 enable_guc=2 enable_psr=1 disable_power_well=1"    # Configuration for Intel integrated graphics.
      "options iwlmvm power_scheme=3"                               # Sets a power-saving scheme for Intel Wi-Fi drivers.
      "options iwlwifi power_save=1 uapsd_disable=1 power_level=5"  # Manages power-saving features for Intel Wi-Fi drivers.
      "options snd_hda_intel power_save=1 power_save_controller=Y"  # Configures power-saving for Intel High Definition Audio (HDA) hardware.
    ];
    
    extraModulePackages = [ ];

    kernel.sysctl = {
      "kernel.panic" = "60";
      "kernel.pty.max" = 24000;                       # Sets the maximum number of pseudo-terminal (pty) devices.
      "kernel.sysrq" = 1;                             # Enables the SysRq key, which can be used for various low-level system commands.
      "net.ipv4.tcp_congestion_control" = "westwood"; # Sets the TCP congestion control algorithm to Westwood for IPv4 in the Linux kernel.
      "vm.dirty_background_bytes" = 268435456;        # Sets the amount of dirty memory at which background writeback starts (256 MB).
      "vm.dirty_bytes" = 536870912;                   # Sets the amount of dirty memory at which a process generating dirty memory will itself start writeback (512 MB).
      "vm.dirty_ratio" = "25"; # 25% of all memory optionally as write cache
      "vm.max_map_count" = 1000000;
      "vm.swappiness" = 10;                           # Reduces the tendency of the kernel to swap out inactive memory pages.
      "vm.vfs_cache_pressure" = 50;                   # Controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects.
      "net.core.default_qdisc" = "cake";            # Sets the default queuing discipline (qdisc) for network interfaces to CAKE for improved network fairness and latency.
      # "vm.page-cluster" = 1;                        # Controls the number of pages read in a single attempt, impacting swap read-ahead. 

    };

  };

  # Define the file systems
  fileSystems = {
    "/" = {
      device = "/dev/disk/by-uuid/81da14b4-f15b-4ab4-b0f1-d6c06c42709e";
      fsType = "ext4";
      # extraArgs = [ "-f" "--compression=lz4" "--discard"  ];
      options = [
        "defaults"            # Applies the default options for mounting.
        "discard"             # Enables the TRIM command for SSDs.
        "noatime"             # Disables updating access times for files.
        "nodiratime"          # Disables updating directory access times.
        "relatime"            # Minimizes the performance impact compared to atime.
        # "compression=lz4"
        # "data=ordered"      # Ensures data ordering for reliability and performance.
        # "discard=async"     # Maintains SSD performance over time.
        # "errors=remount-ro" # Remounts the file system as read-only in case of errors.
      ];
    };

    "/boot" = {
      device = "/dev/disk/by-uuid/CC29-1361";
      fsType = "vfat";
      options = [ "nofail" ];
    };
  };

  powerManagement = {
    cpuFreqGovernor = lib.mkDefault "balanced";

    #---------------------------------------------------------------------------
    # Disable hid driver (gyro/accel) while sleeping for extra power-saving
    #---------------------------------------------------------------------------

    # removes the intel_hid kernel module, which typically handles input from Intel HID devices, such as gyroscope and accelerometer sensors.
    powerDownCommands = ''
      ${pkgs.kmod}/bin/modprobe -r intel_hid
    '';
    # reloads the intel_hid kernel module, re-enabling the Intel HID devices.
    resumeCommands = ''
      ${pkgs.kmod}/bin/modprobe intel_hid
    '';
  };

  swapDevices = [
  { device = "/dev/disk/by-uuid/909cd58a-d4f1-422a-a054-f8e096efdd23"; }
  {
    device = "/var/lib/swapfile";
    size = (1024 * 8) + (1024 * 2); # 8GB + 2GB
    priority = 10;
  }
];


  networking = {
    useDHCP = lib.mkDefault true;
    # interfaces.enp0s25.useDHCP = lib.mkDefault true;
    # interfaces.wlo1.useDHCP = lib.mkDefault true;
  };

  nixpkgs.config.allowUnfree = true;
  nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";

  #---------------------------------------------------------------------
  # Hardware Configuration
  #---------------------------------------------------------------------
  hardware = {    
    # bluetooth.enable = false;
    bluetooth.powerOnBoot = false;      # Power management & Analyze power consumption on Intel-based laptops
    cpu.intel.updateMicrocode = lib.mkDefault config.hardware.enableRedistributableFirmware;      
    enableAllFirmware = true;
    pulseaudio.enable = false; 
    usb-modeswitch.enable = true;
    logitech.wireless.enable = true;
    logitech.wireless.enableGraphical = true;

    sane = {
      enable = true;                    #   Scanner and printing drivers
      extraBackends = extraBackends;    #   Scanner and printing drivers
    };
     
  };

  # Earlyoom killer
  systemd.oomd.enable = false;
  services.earlyoom.enable = true;
}
