{ config, inputs, outputs, pkgs, ... }: {
  boot = {
    initrd.kernelModules = [ "amdgpu" "zfs" ];
    kernel.sysctl = {
      "kernel.hostname" = "caladan.bitgnome.net";
      "kernel.split_lock_mitigate" = 0; # https://lwn.net/Articles/911219/
      #"net.ipv4.tcp_congestion_control" = "reno";
    };
    kernelPackages = pkgs.kerneol.linuxPackages_7_0;
    #kernelParams = [
    #  "amdgpu.ppfeaturemask=0xfffd3fff"
    #  "split_lock_detect=off"
    #];
    loader = {
      efi = {
        canTouchEfiVariables = true;
        efiSysMountPoint = "/efiboot/efi1";
      };
      systemd-boot = {
        enable = true;
        extraInstallCommands = ''
          ${pkgs.rsync}/bin/rsync -av --delete /efiboot/efi1/ /efiboot/efi2
        '';
        memtest86.enable = true;
      };
      timeout = 3;
    };
    supportedFilesystems = [ "zfs" ];
    zfs = {
      forceImportRoot = false;
      package = pkgs.kerneol.zfs_2_4;
      requestEncryptionCredentials = false;
    };
  };

  environment.systemPackages = [
    pkgs.android-tools
    pkgs.angband
    pkgs.assaultcube
    pkgs.beyond-all-reason
    pkgs.bsdgames
    pkgs.bzflag
    pkgs.extremetuxracer
    pkgs.fastfetch
    #pkgs.frozen-bubble
    pkgs.hedgewars
    pkgs.igir
    pkgs.kobodeluxe
    pkgs.linux-firmware
    pkgs.kerneol.linuxKernel.packages.linux_7_0.turbostat
    pkgs.lrzsz
    pkgs.mailutils
    pkgs.minicom
    #pkgs.moc
    pkgs.ncftp
    pkgs.nethack
    pkgs.openttd
    pkgs.piper
    #pkgs.qbittorrent-nox
    pkgs.rdiff-backup
    pkgs.scorched3d
    pkgs.setserial
    pkgs.signal-desktop
    #pkgs.signalbackup-tools
    pkgs.starsector
    #pkgs.master.supertux
    pkgs.supertuxkart
    pkgs.tio
    pkgs.umoria
    pkgs.unshield
    pkgs.vial
    #pkgs.vice
    #pkgs.warzone2100
    pkgs.wayback-x11
    pkgs.wpa_supplicant
    pkgs.xdg-user-dirs
    pkgs.xonotic-sdl
    #pkgs.master.xpilot-ng
  ];

  imports = [
    ./disks.nix
    ./hardware-configuration.nix
    ./services.nix
    ../common/core
    ../common/optional/db.nix
    ../common/optional/dev.nix
    ../common/optional/ebooks.nix
    ../common/optional/games.nix
    ../common/optional/google-authenticator.nix
    ../common/optional/gui.nix
    ../common/optional/misc.nix
    ../common/optional/multimedia.nix
    ../common/optional/pipewire.nix
    ../common/optional/printer.nix
    ../common/optional/sdr.nix
    ../common/optional/services/chrony.nix
    ../common/optional/services/fwupd.nix
    ../common/optional/services/openssh.nix
    #../common/optional/services/wayland.nix
    ../common/optional/services/xorg.nix
    ../common/optional/sound.nix
    ../common/optional/wdt.nix
    ../common/optional/zfs.nix
    ../common/users/nipsy
    ../common/users/root
  ];

  networking = {
    defaultGateway = {
      address = "192.168.1.1";
      interface = "enp16s0";
    };
    hostId = "8981d1e5";
    hostName = "caladan";
    interfaces = {
      enp16s0 = {
        ipv4.addresses = [
          { address = "192.168.1.4"; prefixLength = 24; }
        ];
      };
    };
    nameservers = [ "192.168.1.1" ];
    nftables.enable = true;
    search = [
      "bitgnome.net"
    ];
    useDHCP = false;
    #wireless = {
    #  enable = true;
    #  networks = {
    #    "Crystal Palace" = {
    #      pskRaw = "ext:psk_crystal_palace";
    #    };
    #  };
    #  secretsFile = "${config.sops.secrets."wpa_supplicant".path}";
    #};
  };

  nixpkgs = {
    config = {
      allowUnfree = true;
    };
    hostPlatform = "x86_64-linux";
    overlays = [
      #inputs.nvidia-patch.overlays.default
      outputs.overlays.additions
      outputs.overlays.modifications
      outputs.overlays.kerneol-packages
      outputs.overlays.master-packages
      #outputs.overlays.my-nixpkgs-packages
      outputs.overlays.pr540730-packages
      outputs.overlays.stable-packages
      #outputs.overlays.staging-packages
      #outputs.overlays.wine9_22-packages
    ];
  };

  programs = {
    nix-ld = {
      enable = true;
      libraries = [
        pkgs.alsa-lib
        pkgs.at-spi2-core
        pkgs.cairo
        pkgs.cups
        pkgs.dbus
        pkgs.fontconfig
        pkgs.freetype
        pkgs.glib
        pkgs.libgbm
        pkgs.libx11
        pkgs.libxcb
        pkgs.libxext
        pkgs.libxfixes
        pkgs.libxkbcommon
        pkgs.libxrandr
        pkgs.nspr
        pkgs.nss
        pkgs.pango
        pkgs.vulkan-loader
        pkgs.libxcomposite
        pkgs.libxdamage
      ];
    };
  };

  services.openssh.settings.X11Forwarding = true;
  services.xserver.videoDrivers = [ "amdgpu" ];

  sops = {
    age.sshKeyPaths = [ "/etc/ssh/ssh_host_ed25519_key" ];
    defaultSopsFile = ../secrets/caladan.yaml;

    secrets = {
      "nftables/ssh" = {};
      "nix-access-token-github" = {};
      "ssh_config".path = "/root/.ssh/config";
      #"wpa_supplicant" = {
      #  group = config.users.users.wpa_supplicant.group;
      #  owner = config.users.users.wpa_supplicant.name;
      #};
    };
  };

  system.stateVersion = "23.11";

  systemd.services = {

    "nftables-extra" = let rules_script = ''
        ${pkgs.nftables}/bin/nft insert rule inet nixos-fw input 'iifname "enp16s0" udp dport { 2456, 2457 } counter accept # Valheim dedicated server'
        ${pkgs.nftables}/bin/nft insert rule inet nixos-fw input 'iifname "enp16s0" udp dport 5121 counter accept # Neverwinter Nights dedicated server'
        ${pkgs.nftables}/bin/nft insert rule inet nixos-fw input 'iifname "enp16s0" udp dport 9876-9878 counter accept # V Rising dedicated server'
        ${pkgs.nftables}/bin/nft insert rule inet nixos-fw input 'iifname "enp16s0" udp dport 15637 counter accept # Enshrouded'
        ${pkgs.nftables}/bin/nft insert rule inet nixos-fw input 'iifname "enp16s0" ip saddr 192.168.1.0/24 udp dport { 27031, 27036 } counter accept # Steam Remote Play'
        ${pkgs.nftables}/bin/nft insert rule inet nixos-fw input 'iifname "enp16s0" ip saddr 192.168.1.0/24 tcp dport { 27036, 27037 } counter accept # Steam Remote Play'
        ${pkgs.nftables}/bin/nft -f ${config.sops.secrets."nftables/ssh".path}
      ''; in {
        description = "nftables extra firewall rules";
        reload = rules_script;
        script = rules_script;
        serviceConfig = {
          RemainAfterExit = true;
          Type = "oneshot";
        };
        unitConfig = {
          ConditionPathExists = [
            config.sops.secrets."nftables/ssh".path
          ];
          ReloadPropagatedFrom = "nftables.service";
        };
        wantedBy = [ "multi-user.target" ];
        after = [ "nftables.service" ];
        partOf = [ "nftables.service" ];
    };

  };

  users.users.root.openssh.authorizedKeys.keys = [
    (builtins.readFile ../common/users/nipsy/keys/id_att.pub)
  ];
}
