{ lib, config, pkgs, ... }:
with lib; {
  boot = {
    binfmt.emulatedSystems = [ "armv7l-linux" "aarch64-linux" ];
    # kernelPackages = pkgs.linuxPackages_latest;
    extraModulePackages = with config.boot.kernelPackages; [ v4l2loopback ];
    loader = {
      systemd-boot.enable = true;
      efi.canTouchEfiVariables = true;
    };
    initrd = {
      checkJournalingFS = true;
      luks.devices = {
        root = {
          device = "/dev/nvme0n1p2";
          preLVM = true;
          allowDiscards = true;
          bypassWorkqueues = true;
        };
      };
    };
  };

  console = {
    font = "Lat2-Terminus16";
    keyMap = "us";
  };

  environment = {
    pathsToLink = [ "/share/zsh" ];
    systemPackages = with pkgs; [ kakoune tmux vim ];
  };

  hardware = {
    opengl.enable = true;
    bluetooth.enable = true;
    enableRedistributableFirmware = true;
    pulseaudio.enable = false;
    # nvidia.modesetting.enable = true;
  };

  i18n.defaultLocale = "it_IT.UTF-8";

  programs = {
    dconf.enable = true;
    flashrom.enable = true;
    gamemode = {
      enable = true;
      settings = {
        custom = {
          start = "${pkgs.libnotify}/bin/notify-send 'GameMode started'";
          end = "${pkgs.libnotify}/bin/notify-send 'GameMode ended'";
        };
      };
    };
    gnupg.agent = {
      enable = true;
      enableSSHSupport = true;
    };
    steam = { enable = true; };
    zsh = {
      enable = true;
      syntaxHighlighting.enable = true;
    };
  };

  networking = {
    hostName = "thor"; # Define your hostname.
    interfaces = {
      eno1.useDHCP = true;
      wlp5s0.useDHCP = true;
    };
    networkmanager.enable = true;
    useDHCP = false;

    firewall.allowedTCPPortRanges = [
      { from = 1714; to = 1764; } # KDEConnect
    ];
    firewall.allowedUDPPortRanges = [
      { from = 1714; to = 1764; } # KDEConnect
    ];

    firewall.allowedTCPPorts = [
      27036
      27037 # Steam remote play
    ];
    firewall.allowedUDPPorts = [
      27031
      27036 # Steam remote play
    ];
  };

  time.timeZone = "Europe/Rome";

  services = {
    avahi = {
      enable = true;
      openFirewall = true;
      nssmdns = true;
      publish = {
        enable = true;
        addresses = true;
        domain = true;
        userServices = true;
        workstation = true;
      };
      extraServiceFiles = {
        ssh = "${pkgs.avahi}/etc/avahi/services/ssh.service";
      };
    };
    blueman.enable = true;
    clamav = {
      daemon.enable = true;
      updater.enable = true;
    };
    dbus.packages = with pkgs; [ dconf ];
    gnome.gnome-keyring.enable = true;
    gvfs = {
      enable = true;
      package = pkgs.gnome3.gvfs;
    };
    fwupd.enable = true;
    openssh = {
      enable = true;
      openFirewall = true;
      permitRootLogin = "no";
      passwordAuthentication = false;
      forwardX11 = true;
    };
    pipewire = {
      enable = true;
      pulse.enable = true;
      jack.enable = true;
      alsa = {
        enable = true;
        support32Bit = true;
      };
      # media-session.enable = true;
    };
    power-profiles-daemon.enable = true;
    smartd = {
      enable = true;
      notifications.x11.enable = true;
    };
    snapper = {
      configs =
        let
          common = {
            extraConfig = ''
              ALLOW_USERS="bertof"
              TIMELINE_CREATE=yes
              TIMELINE_CLEANUP=yes
            '';
          };
        in
        {
          bertof_home = recursiveUpdate common { subvolume = "/home/bertof"; };
        };
    };
    thermald.enable = true;
    xserver = {
      enable = true;
      desktopManager = {
        # gnome.enable = true;
        # cinnamon.enable = true;
        # plasma5 = { enable = true; runUsingSystemd = true; useQtScaling = true; };
      };
      windowManager.bspwm = { enable = true; };
      # displayManager.gdm = { enable = true; nvidiaWayland = true; };
      displayManager.sddm = {
        enable = true;
        theme =
          "${pkgs.sddm-theme-clairvoyance}/usr/share/sddm/themes/clairvoyance";
      };

      videoDrivers = [ "nvidia" ];
      layout = "us,it";
      xkbOptions = "eurosign:e,terminate:ctrl_alt_bksp,compose:rctrl,grp:menu_toggle";
      xrandrHeads = [{
        primary = true;
        output = "DP-4";
      }];
    };
    # gnome.gnome-remote-desktop.enable = true;
  };

  services.teamviewer.enable = true;

  security = {
    tpm2.enable = true;
    rtkit.enable = true;
    pam.services = { autounlock_gnome_keyring.enableGnomeKeyring = true; };
    sudo.extraConfig = ''
      Defaults pwfeedback
    '';
  };

  sound.enable = false;

  users.users.bertof = {
    isNormalUser = true;
    extraGroups = [
      "audio"
      "input"
      "docker"
      "flashrom"
      "libvirtd"
      "network"
      "networkmanager"
      "usb"
      "video"
      "wheel"
    ];
    shell = pkgs.zsh;
  };

  virtualisation = {
    docker.enable = true;
    kvmgt.enable = true;
    libvirtd.enable = true;
    podman.enable = true;
    # virtualbox.host.enable = true;
  };

  nixpkgs.config = {
    allowUnfree = true;
    packageOverrides = pkgs: {
      steam = pkgs.steam.override { extraPkgs = pkgs: with pkgs; [ icu ]; };
    };
    # cudaSupport = true;
  };

  system.stateVersion = "22.05"; # Did you read the comment?
}
