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

with lib;
let

in {
  imports = [ <nixpkgs/nixos/modules/installer/scan/not-detected.nix> ];

  system.stateVersion = "21.05";

  boot = {
    loader = {
      systemd-boot.enable = true;
      efi.canTouchEfiVariables = true;
    };

    initrd = {
      availableKernelModules =
        [ "xhci_pci" "ahci" "nvme" "usbhid" "usb_storage" "sd_mod" ];
      kernelModules = [ "amdgpu" ];
    };

    kernelModules = [ "kvm-amd" ];
    kernelPackages = pkgs.linuxPackages_latest;

    # kernelPatches = [{
    #   name = "big-navi";
    #   patch = null;
    #   extraConfig = ''
    #     DRM_AMD_DC_DCN3_0 y
    #     DRM_AMD_DC_DCN y
    #   '';
    # }];
  };

  fileSystems = {
    "/boot" = {
      device = "/dev/disk/by-label/SYS3-BOOT";
      fsType = "vfat";
      options = [ "noexec" ];
    };

    "/" = {
      device = "system3-root";
      fsType = "tmpfs";
      options = [ "mode=755" ];
    };

    "/nix" = {
      device  = "/dev/disk/by-label/system3";
      fsType  = "btrfs";
      options = [ "subvol=nix" "compress=zstd" "noatime" ];
    };

    "/var/log" = {
      device  = "/dev/disk/by-label/system3";
      fsType  = "btrfs";
      options = [ "subvol=log" "compress=zstd" "noatime" "noexec" ];
      neededForBoot = true;
    };

    "/state" = {
      device  = "/dev/disk/by-label/system3";
      fsType  = "btrfs";
      options = [ "subvol=state" "compress=zstd" "noatime" ];
    };

    "/home" = {
      device  = "/dev/disk/by-label/system3";
      fsType  = "btrfs";
      options = [ "subvol=home" "compress=zstd" "noatime" ];
    };
  };

  swapDevices = [{ device = "/dev/disk/by-label/system3-swap"; }];

  hardware = {
    bluetooth.enable = true;

    cpu.amd.updateMicrocode = true;

    opengl = {
      driSupport = true;
      driSupport32Bit = true;
      extraPackages = with pkgs; [
        rocm-opencl-icd
        rocm-opencl-runtime
        amdvlk
        driversi686Linux.amdvlk
      ];
      setLdLibraryPath = true;
    };

    pulseaudio = {
      support32Bit = true;
      package = pkgs.pulseaudioFull;
    };

    enableRedistributableFirmware = true;

    enableAllFirmware = true;
  };

  services = {
    xserver.videoDrivers = [ "amdgpu" ];

    compton.backend = "xrender";
  };

  networking = {
    hostId = substring 0 8 (fileContents /state/etc/machine-id);

    useDHCP = false;

    macvlans = {
      intif0 = {
        interface = "enp7s0";
        mode = "bridge";
      };
    };

    interfaces = {
      intif0 = {
        # output of: echo lambda-intif0|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:\1:\2:\3:\4:\5/'
        macAddress = "02:0d:df:2d:46:90";
      };
    };
  };

  systemd.targets = {
    sleep.enable = false;
    suspend.enable = false;
    hibernate.enable = false;
    hybrid-sleep.enable = false;
  };
}