#
# Per-system configuration for Hinata.
#
# vim: et:ts=2:sw=2:
#
{
  config,
  deprekages,
  pkgs,
  lib,
  modulesPath,
  ...
}:
let
  # libfprint with patches for the GPD-provided reader
  libfprint-gpd = pkgs.libfprint.overrideAttrs (prev: {
    version = "git";

    src = pkgs.fetchFromGitHub {
      owner = "ericlinagora";
      repo = "libfprint-CS9711";
      rev = "03ace5b20146eb01c77fb3ea63e1909984d6d377";
      sha256 = "sha256-gr3UvFB6D04he/9zawvQIuwfv0B7fEZb6BGiNAbLids=";
    };

    nativeBuildInputs = prev.nativeBuildInputs ++ (with pkgs; [
      opencv
      cmake
      doctest
      nss
    ]);

    installCheckPhase = "";

  }).overrideAttrs (prev: rec {
      version = "1.94.4";
      src = pkgs.fetchFromGitLab {
        domain = "gitlab.freedesktop.org";
        owner = "libfprint";
        repo = "fprintd";
        rev = "refs/tags/v${version}";
        hash = "sha256-B2g2d29jSER30OUqCkdk3+Hv5T3DA4SUKoyiqHb8FeU=";
      };
  });

in
{
  system.stateVersion = "23.11";

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

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

  # Networking.
  networking.hostName = "hinata";
  networking.networkmanager.enable = true;
  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;

  #
  # 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 18;

  # Restart waybar when niri starts.
  home-manager.users.deprekated.programs.niri.settings.spawn-at-startup = [
    {
      command = [
        "modem-manager-gui"
        "--invisible"
      ];
    }
  ];

  # Fingerprint reader support.
  services.fprintd = {
    enable = true;
    package = (pkgs.fprintd.override {
      libfprint = libfprint-gpd;
    }).overrideAttrs (prev: rec {
      version = "1.94.4";
      src = pkgs.fetchFromGitLab {
        domain = "gitlab.freedesktop.org";
        owner = "libfprint";
        repo = "fprintd";
        rev = "refs/tags/v${version}";
        hash = "sha256-B2g2d29jSER30OUqCkdk3+Hv5T3DA4SUKoyiqHb8FeU=";
      };
    });
  };

  # 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;
      };

    };
  };

  #
  # Hardware config.
  #
  boot.initrd.availableKernelModules = [
    "nvme"
    "xhci_pci"
    "thunderbolt"
    "usbhid"
    "usb_storage"
    "sd_mod"
  ];
  boot.initrd.kernelModules = [ ];
  boot.kernelModules = [ "kvm-amd" ];
  boot.extraModulePackages = [ ];

  fileSystems."/" = {
    device = "/dev/disk/by-uuid/af2b4691-8c2d-42a5-8201-84e3cfea5465";
    fsType = "ext4";
  };

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

  swapDevices = [ ];

  # 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.wlp2s0.useDHCP = lib.mkDefault true;
  # networking.interfaces.wwan0.useDHCP = lib.mkDefault true;

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