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

{
  imports =
    [
      # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  networking = {
    hostName = "nixos-desktop";
  };

  environment.systemPackages = with pkgs; [
    config.boot.kernelPackages.v4l2loopback
    v4l-utils
  ];

  services = {
    hardware.openrgb = {
      enable = true;
    };

    # foldingathome = {
    #   enable = true;
    #   user = "codebam";
    # };

    # ollama = {
    #   enable = true;
    #   acceleration = "rocm";
    #   environmentVariables = {
    #     HSA_OVERRIDE_GFX_VERSION = "11.0.0";
    #   };
    # };
  };

  programs = {
    # steam = {
    #   enable = true;
    #   remotePlay.openFirewall = true;
    #   dedicatedServer.openFirewall = true;
    #   localNetworkGameTransfers.openFirewall = true;
    # };
    # gamescope = {
    #   enable = true;
    # };
    corectrl = {
      enable = true;
      gpuOverclock.enable = true;
      gpuOverclock.ppfeaturemask = "0xffffffff";
    };
  };

  hardware = {
    fancontrol = {
      enable = false;
      config = ''
        # Configuration file generated by pwmconfig, changes will be lost
        INTERVAL=10
        DEVPATH=hwmon5=devices/pci0000:00/0000:00:03.1/0000:08:00.0/0000:09:00.0/0000:0a:00.0 hwmon6=devices/platform/nct6775.656
        DEVNAME=hwmon5=amdgpu hwmon6=nct6798
        FCTEMPS=hwmon6/pwm7=hwmon6/temp7_input hwmon6/pwm6=hwmon6/temp6_input hwmon6/pwm5=hwmon6/temp5_input hwmon6/pwm4=hwmon6/temp4_input hwmon6/pwm3=hwmon6/temp3_input hwmon6/pwm2=hwmon6/temp2_input hwmon6/pwm1=hwmon6/temp1_input
        FCFANS=hwmon6/pwm7=hwmon5/fan1_input hwmon6/pwm6=hwmon5/fan1_input hwmon6/pwm5=hwmon5/fan1_input hwmon6/pwm4=hwmon6/fan4_input+hwmon5/fan1_input hwmon6/pwm3=hwmon6/fan3_input+hwmon5/fan1_input hwmon6/pwm2=hwmon6/fan2_input+hwmon5/fan1_input hwmon6/pwm1=hwmon6/fan1_input
        MINTEMP=hwmon6/pwm7=20 hwmon6/pwm6=20 hwmon6/pwm5=20 hwmon6/pwm4=20 hwmon6/pwm3=20 hwmon6/pwm2=20 hwmon6/pwm1=20
        MAXTEMP=hwmon6/pwm7=60 hwmon6/pwm6=60 hwmon6/pwm5=60 hwmon6/pwm4=60 hwmon6/pwm3=60 hwmon6/pwm2=60 hwmon6/pwm1=60
        MINSTART=hwmon6/pwm7=150 hwmon6/pwm6=150 hwmon6/pwm5=150 hwmon6/pwm4=150 hwmon6/pwm3=150 hwmon6/pwm2=150 hwmon6/pwm1=150
        MINSTOP=hwmon6/pwm7=100 hwmon6/pwm6=100 hwmon6/pwm5=100 hwmon6/pwm4=100 hwmon6/pwm3=100 hwmon6/pwm2=100 hwmon6/pwm1=0
      '';
    };
    graphics = {
      extraPackages = with pkgs; [
        rocmPackages.clr.icd
      ];
    };
  };

  nixpkgs.overlays = [
    (self: super: {
      linuxPackages_latest = super.linuxPackages_latest.extend (lpself: lpsuper: {
        rtl8814au = super.linuxPackages_latest.rtl8814au.overrideAttrs (oldAttrs: {
          version = "${config.boot.kernelPackages.kernel.version}-unstable-2024-09-17";
          src = pkgs.fetchFromGitHub {
            owner = "morrownr";
            repo = "8814au";
            rev = "d8208c83ecfd9b286f3ea45a7eb7d78d10560670";
            hash = "sha256-lKTxWpmC17ecKr9oBHgkyKumR0rvsZoBklq7TKjI6L4=";
          };
        });
      });
    })
  ];

  # nixpkgs.overlays = [
  #   (final: prev: {
  #     # linuxPackages_testing = inputs.rc2.legacyPackages.${pkgs.system}.linuxPackages_testing;
  #     # linuxPackages_latest = inputs.linux-latest-update.legacyPackages.${pkgs.system}.linuxPackages_testing;
  #     # bcachefs-tools = inputs.bcachefs-fix.packages.${pkgs.system}.bcachefs;
  #   })
  # ];

  nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [
    "steam"
    "steam-original"
    "steam-run"
  ];

  system = {
    autoUpgrade = {
      enable = false;
      flake = "github:codebam/nixos#nixos-desktop";
      dates = "09:00";
    };
    stateVersion = "23.11";
  };
}
