#
# Per-system configuration.
#
# vim: et:ts=2:sw=2:
#
{
  config,
  lib,
  pkgs,
  deprekages,
  modulesPath,
  normalizeModule,
  ...
}:
{
  imports = [
    (modulesPath + "/installer/scan/not-detected.nix")
    ./audio.nix
    ./ec_kmod.nix
    (normalizeModule ./wluma.hm.nix)
  ];

  # Bootloader.
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  # Networking.
  networking.hostName = "chrysalis";
  networking.networkmanager = {
    enable = true;
    wifi.backend = "iwd";
  };
  networking.firewall.enable = false;

  # This is a local machine, rather than our typical network-accesed ones.
  # Run an ssh-agent locally.
  programs.ssh.startAgent = true;

  # Support our fingerprint reader.
  services.fprintd.enable = true;

  # Support framework updates.
  services.fwupd.enable = true;

  # Tools for the framework EC.
  environment.systemPackages = [
    pkgs.framework-tool
    deprekages.framework-13-icm
  ];

  #
  # Niri configuration for our monitors.
  #
  home-manager.users.deprekated.programs.niri.settings = {
    outputs."eDP-1".scale = 1.5;
  };

  # Override stylix font sizes.
  stylix.fonts.sizes.desktop = lib.mkForce 16;

  # Use wluma to manage backlight with the ALS and screen content.
  deprekated.niri.extraStartupCommands = "niri msg action spawn -- ${lib.getExe pkgs.wluma}";

  # Accomodate the curved screen top by moving things in just a little.
  deprekated.waybar.hostSpecificCss = ''
    .modules-left > :first-child > * {
        padding-left: 3ex;
    }

    .modules-right > :last-child > * {
        padding-right: 3ex;
    }
  '';

  # Provide memtest86, since that's nice.
  boot.loader.systemd-boot.memtest86.enable = true;

  #
  # Hardware config.
  #

  # Use the latest kernel to try and ward off WiFi issues.
  boot.kernelPackages = pkgs.linuxPackages_latest;

  boot.extraModprobeConfig = ''
    # Ensure we have a regulatory domain set for our wifi.
    options cfg80211 ieee80211_regdom=NL

    # Support microphones via the TRRS jack.
    options snd-hda-intel model=dell-headset-multi
  '';
  boot.initrd.availableKernelModules = [
    "xhci_pci"
    "thunderbolt"
    "vmd"
    "nvme"
    "usbhid"
    "usb_storage"
    "sd_mod"
  ];
  boot.initrd.kernelModules = [ ];
  boot.kernelModules = [
    "thunderbolt"
  ];

  boot.kernelParams = [
    # Bluetooth stability tweaks.
    "mt7925e.disable_aspm=1"

    # N.b. if we start experiencing GPU stalls, disabling PSR can help.
    #"amdgpu.dcdebugmask=0x10"
  ];

  # Allow use of our brightness sensor for screen auto-leveling.
  hardware.sensor.iio.enable = lib.mkDefault true;

  # Fix suspend with the ethernet expansion card.
  services.udev.extraRules = ''
    # Ethernet expansion card support
    ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="0bda", ATTR{idProduct}=="8156", ATTR{power/autosuspend}="20"
  '';

  # Support ICC color profiles.
  services.colord.enable = true;

  # Support thunderbolt.
  services.hardware.bolt.enable = true;

  # Enable audio DSP filtering.
  hardware.framework.laptop13.audioEnhancement = {
    enable = true;
    rawDeviceName = "alsa_output.pci-0000_c1_00.6.analog-stereo";
  };

  # Support the framework EC from sysfs.
  hardware.framework.enableKmod = true;

  # Support bluetooth.
  hardware.bluetooth = {
    enable = true;
    powerOnBoot = false;

    settings = {
      General = {

        # Support A2DP.
        Enable = "Source,Sink,Media,Socket";

        # Enable experimental featurees, like reading device battery levels.
        Experimental = true;
      };

    };
  };

  fileSystems."/" = {
    device = "/dev/disk/by-uuid/bb880b80-992f-4e56-bb80-c5c4df0ddd72";
    fsType = "ext4";
  };

  fileSystems."/boot" = {
    device = "/dev/disk/by-uuid/5B37-C691";
    fsType = "vfat";
    options = [
      "fmask=0022"
      "dmask=0022"
    ];
  };

  swapDevices = [ { device = "/dev/disk/by-uuid/50f20263-9632-439d-b57d-b9ee8f13d62b"; } ];

  # Enables DHCP on each ethernet and wireless interface. In case of scripted networking
  # (the default) this is the recommended approach. When using systemd-networkd it's
  # still possible to use this option, but it's recommended to use it in conjunction
  # with explicit per-interface declarations with `networking.interfaces.<interface>.useDHCP`.
  networking.useDHCP = lib.mkDefault true;
  # networking.interfaces.wlp0s20f3.useDHCP = lib.mkDefault true;

  nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";
  hardware.cpu.amd.updateMicrocode = true;
}
