{ config, lib, pkgs, hostName, ... }:
{
hardware.cpu.intel.updateMicrocode = true;
hardware.opengl.extraPackages = [
  pkgs.intel-media-driver # LIBVA_DRIVER_NAME=iHD
  pkgs.vaapiIntel         # LIBVA_DRIVER_NAME=i965 (older but works better for Firefox/Chromium)
  pkgs.vaapiVdpau
  pkgs.libvdpau-va-gl
];
powerManagement.cpuFreqGovernor = lib.mkDefault "ondemand";
services.thinkfan = {
  enable = true;
  smartSupport = true;
  levels = [
    [0     0      57]
    [1     56     60]
    [2     59     63]
    [3     62     65]
    [4     64     67]
    [5     66     72]
    [6     71     78]
    [7     77     82]
    [127   81     32767]
  ];
};
hardware.acpilight.enable = true;
services.acpid = {
  enable = true;
  handlers = {
    brightnessDown = {
      event = "video/brightnessdown.*";
      action = "${pkgs.acpilight}/bin/xbacklight -dec 5";
    };
    brightnessUp = {
      event = "video/brightnessup.*";
      action = "${pkgs.acpilight}/bin/xbacklight -inc 5";
    };
  };
  acEventCommands = ''
    vals=($1)
    case ''${vals[3]} in
      00000000) # unplugged
        ${pkgs.linuxPackages.cpupower}/bin/cpupower frequency-set -g powersave;;
      00000001) # plugged in
        ${pkgs.linuxPackages.cpupower}/bin/cpupower frequency-set -g ondemand;;
    esac
  '';
};

# https://bugzilla.kernel.org/show_bug.cgi?id=110941
boot.kernelParams = [ "intel_pstate=no_hwp" ];
boot.kernelModules = [
  "kvm-intel"
];
boot.cleanTmpDir = true;
boot.tmpOnTmpfs = true;
boot.extraModulePackages = [
  #config.boot.kernelPackages.exfat-nofuse
];
/*
boot.loader.efi = {
  canTouchEfiVariables = true;
  efiSysMountPoint = "/boot/efi";
};
*/
# No pstore backend available on this system.
systemd.services.mount-pstore.enable = false;
boot.loader.grub = {
  enable  = true;
  version = 2;
  device  = "/dev/disk/by-id/ata-Samsung_SSD_850_PRO_128GB_S1SMNSAFC36436X";
  configurationLimit = 3;

  #zfsSupport = true;
  #efiSupport = true;
  #enableCryptodisk = true;
};

fileSystems."/boot" =
  { device = "/dev/disk/by-partlabel/${hostName}_ssd_boot";
    fsType = "ext2";
  };
fileSystems."/boot/efi" =
  { device = "/dev/disk/by-partlabel/${hostName}_ssd_efi";
    fsType = "vfat";
  };
swapDevices = [
  { device = "/dev/disk/by-partlabel/${hostName}_ssd_swap";
    randomEncryption = {
      enable = true;
      cipher = "aes-xts-plain64";
      source = "/dev/urandom";
    };
  }
];
zramSwap = {
  enable = true;
  algorithm = lib.mkDefault "zstd";
  # There is little point creating a zram of greater
  # than twice the size of memory
  # since we expect a 2:1 compression ratio.
  # Note that zram uses about 0.1% of the size of the disk
  # when not in use so a huge zram is wasteful.
  memoryPercent = lib.mkDefault 150;
  # Linux supports multithreaded compression for 1 device since 3.15.
  # See https://lkml.org/lkml/2014/2/28/404 for details.
  swapDevices = lib.mkDefault 1;
};
boot.kernel.sysctl = {
  # Increase cache pressure, which increases the tendency of the kernel to
  # reclaim memory used for caching of directory and inode objects. You will use
  # less memory over a longer period of time. The performance hit is negated by
  # the downside of swapping sooner.
  "vm.vfs_cache_pressure" = lib.mkDefault 500;

  # Increasing how aggressively the kernel will swap memory pages since we are
  # using ZRAM first.
  "vm.swappiness" = lib.mkDefault 100;

  # Background processes will start writing right away when it hits the 1% limit
  "vm.dirty_background_ratio" = lib.mkDefault 1;

  # The system won’t force synchronous I/O until it gets to 50% dirty_ratio.
  "vm.dirty_ratio" = lib.mkDefault 50;
};

# The 32-bit host id of the host, formatted as 8 hexadecimal characters.
# You should try to make this id unique among your hosts.
# Manually generated with : head -c4 /dev/urandom | od -A none -t x4 | cut -d ' ' -f 2
networking.hostId = "ce53d0c3";

# none is the recommended elevator with ZFS (which has its own I/O scheduler)
# and/or for SSD, whereas HDD could use mq-deadline.
services.udev.extraRules = ''
  # set none scheduler for non-rotating disks
  ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="none"
'';

boot.supportedFilesystems = [ "ntfs" "vfat" "zfs" ];
boot.initrd.supportedFilesystems = [ "zfs" ];
boot.initrd.availableKernelModules = [
  "ahci"
  "drbg"
  "ehci_pci"
  "gf128mul"
  "hmac"
  "sd_mod"
];

boot.zfs.forceImportAll  = false;
boot.zfs.forceImportRoot = false;
boot.zfs.enableUnstable = false;
boot.zfs.requestEncryptionCredentials = [ hostName ];
services.zfs.autoScrub.enable = true;

fileSystems."/" =
  { device = "${hostName}/root";
    fsType = "zfs";
  };
fileSystems."/nix" =
  { device = "${hostName}/nix";
    fsType = "zfs";
  };
fileSystems."/var" =
  { device = "${hostName}/var";
    fsType = "zfs";
  };

}