{ pkgs, inputs, ... }:

{

  #system.includeBuildDependencies = true; # THIS CAN'T BE INCLUDED IN COMMON YET BECAUSE SERVER DOES NOT HAVE ENOUGH STORAGE

  hardware.graphics = {
    enable = true;
  };

  # Enable the X11 windowing system.
  services.xserver.enable = true;

  # Enable the GNOME Desktop Environment.
  services.displayManager.gdm.enable = false; # use sddm until gnome 50 due to https://github.com/systemd/systemd/issues/39259
  services.displayManager.sddm.enable = true;
  services.desktopManager.gnome.enable = true;

  # Configure keymap in X11
  services.xserver.xkb = {
    layout = "us";
    variant = "";
  };

  hardware.bluetooth.enable = true;
  hardware.bluetooth.powerOnBoot = true;

  # Enable CUPS to print documents.
  services.printing.enable = true;
  services.avahi = {
    enable = true;
    nssmdns4 = true;
    openFirewall = true;
  };
  services.tailscale.enable = true;
  services.printing.drivers = [ pkgs.brlaser pkgs.brgenml1lpr pkgs.brgenml1cupswrapper ];

  # Enable sound with pipewire.
  services.pulseaudio.enable = false;
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
  };

  # Enable automatic login for the user.
  # services.displayManager.autoLogin.enable = true;
  # services.displayManager.autoLogin.user = "admin";
  # services.gnome.gnome-keyring.enable = true;
  # security.pam.services.gdm.enableGnomeKeyring = true; #unlock PAM

  # Workaround for GNOME autologin: https://github.com/NixOS/nixpkgs/issues/103746#issuecomment-945091229
  systemd.services."getty@tty1".enable = false;
  systemd.services."autovt@tty1".enable = false;

  #services.xserver.desktopManager.retroarch.extraArgs = [
  #  "--config=/mnt/services/emulators/retroarch/retroarch.cfg"
  #];

  services.ollama = {
    enable = true;
    acceleration = "cuda";
    openFirewall = true;
    host = "0.0.0.0";
    # Optional: preload models, see https://ollama.com/library
    loadModels = [ "gemma3:4b-it-qat" ];
    package = pkgs.unstable.ollama;

  };

  hardware.keyboard.qmk.enable = true;

  boot = {
    # Graphical boot
    plymouth.enable = true;
    # Allow higher virtual memory for games
    kernel.sysctl."vm.max_map_count" = 2147483642;
  };

  #services.udev.packages = [ pkgs.via ];

  environment.gnome.excludePackages = with pkgs; [
    epiphany # web browser
    geary # email
    decibels #audio player
    gnome-calendar
    gnome-music
    gnome-software
    gnome-tour
    totem # video player
    xterm
    yelp # help
  ];

  nixpkgs.overlays = [
    (final: _: {
      # this allows you to access `pkgs.unstable` anywhere in your config
      unstable = import inputs.nixpkgs-unstable {
        inherit (final.stdenv.hostPlatform) system;
        inherit (final) config;
      };
    })
  ];

  environment.systemPackages = with pkgs; [
    audacity
    brave
    blender
    orca-slicer
    firefox
    gimp
    gnomeExtensions.appindicator
    gnomeExtensions.blur-my-shell
    gnomeExtensions.caffeine
    gnomeExtensions.dash-to-dock
    inkscape
    kiwix
    libreoffice
    obs-studio
    obsidian
    unstable.proton-authenticator
    unstable.protonmail-desktop
    unstable.protonvpn-gui
    prismlauncher
    unstable.signal-desktop
    freecad
    tor-browser
    wireshark
    monero-gui
    moonlight-qt
    nvtopPackages.nvidia
    vscodium
    dconf-editor
    via
    kdePackages.kdenlive
    easyeffects
    element-desktop
    chirp
    scrcpy

    # AI?
    claude-code

    # games
    heroic
    lutris
    cataclysm-dda
    runelite
    zeroad
    unciv
    endless-sky
    wesnoth
    mindustry
    openrct2
    openttd
    xonotic
    osu-lazer-bin
    #ufoai
    #inputs.nix-citizen.packages.${system}.rsi-launcher
    
    # game utils
    mangohud
    umu-launcher

    # emu
    ryubing
    xemu
    xenia-canary
    #retroarch-full
    rpcs3
    dolphin-emu
    cemu

    # backup
    keepassxc
    jdk
  ];

  fonts.packages = with pkgs; [
    noto-fonts
    noto-fonts-cjk-sans
    noto-fonts-color-emoji
    corefonts
    fira-mono
  ];
  fonts.fontconfig = {
    antialias = true;
    cache32Bit = true;
    hinting.enable = true;
    hinting.autohint = true;
  };

  services.sunshine = {
    enable = true;
    autoStart = true;
    capSysAdmin = true;
    openFirewall = true;
    package = pkgs.sunshine.override {
      cudaSupport = true;
    };
  };

  programs = {
    adb.enable = true;
    
    localsend = {
      enable = true;
      openFirewall = true;
    };

    coolercontrol = {
      enable = true;
    };

    gamescope = {
      enable = true;
      capSysNice = true;
    };

    steam = {
      enable = true;
      gamescopeSession.enable = true;
      remotePlay.openFirewall = true; # Open ports in the firewall for Steam Remote Play
      dedicatedServer.openFirewall = true; # Open ports in the firewall for Source Dedicated Server
      localNetworkGameTransfers.openFirewall = true; # Open ports in the firewall for Steam Local Network Game Transfers
    };

    gamemode.enable = true;

    dconf = {
      enable = true;
      profiles.user.databases = [{
          lockAll = true; # prevents overriding
          settings = {
            "org/gnome/shell" = {
              favorite-apps = [
                "org.gnome.Nautilus.desktop"
                "brave-browser.desktop"
                "proton-mail.desktop"
                "obsidian.desktop"
                "signal.desktop"
                "element-desktop.desktop"
                "steam.desktop"
                "org.gnome.Settings.desktop"
                "org.gnome.Console.desktop"
              ];
              enabled-extensions = [
                pkgs.gnomeExtensions.appindicator.extensionUuid
                pkgs.gnomeExtensions.blur-my-shell.extensionUuid
                pkgs.gnomeExtensions.caffeine.extensionUuid
                pkgs.gnomeExtensions.dash-to-dock.extensionUuid
              ];
            };
            "org/gnome/desktop/calendar" = {
              show-weekdate = true;
            };
            "org/gnome/desktop/interface" = {
              clock-show-weekday = true;
              color-scheme = "prefer-dark";
              document-font-name = "Noto Sans 11";
              enable-hot-corners = false;
              font-name = "Noto Sans 11";
              gtk-theme = "Adwaita-dark";
              monospace-font-name = "Fira Mono 12";
            };
            "org/gnome/desktop/peripherals/mouse" = {
              accel-profile = "flat";
            };
            "org/gnome/desktop/wm/preferences" = {
              button-layout = ":minimize,maximize,close";
            };
            "org/gnome/nautilus/list-view" = {
              default-zoom-level = "small";
            };
            "org/gnome/nautilus/preferences" = {
              default-folder-viewer = "list-view";
            };
            "org/gnome/shell/extensions/dash-to-dock" = {
              click-action = "minimize-or-previews";
              dock-fixed = true;
              multi-monitor = true;
              show-mounts = false;
              show-show-apps-button = true;
              apply-custom-theme = true;
            };
            "org/gnome/shell/keybindings" = {
              show-screenshot-ui = ["<Shift><Super>s"];
            };
            #org.gnome.desktop.input-sources sources [('xkb', 'us'), ('xkb', 'dz+ar')]
          };
        }];
    };
  };

  fileSystems."/mnt/media" = {
    device = "//192.168.0.30/media";
    fsType = "cifs";
    options = let
      # this line prevents hanging on network split
      automount_opts = "x-systemd.automount,noauto,x-systemd.idle-timeout=60,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s";

    in ["${automount_opts},username=admin,password=Ch19blizz9,uid=1000,gid=1000"];
  };
  fileSystems."/mnt/services" = {
    device = "//192.168.0.30/services";
    fsType = "cifs";
    options = let
      # this line prevents hanging on network split
      automount_opts = "x-systemd.automount,noauto,x-systemd.idle-timeout=60,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s";

    in ["${automount_opts},username=admin,password=Ch19blizz9,uid=1000,gid=1000"];
  };
  fileSystems."/mnt/data" = {
    device = "//192.168.0.30/data";
    fsType = "cifs";
    options = let
      # this line prevents hanging on network split
      automount_opts = "x-systemd.automount,noauto,x-systemd.idle-timeout=60,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s";

    in ["${automount_opts},username=admin,password=Ch19blizz9,uid=1000,gid=1000"];
  };

  programs.virt-manager.enable = true;
  users.groups.libvirtd.members = ["admin"];
  virtualisation.libvirtd = {
    enable = true;
    qemu.swtpm.enable = true;
  };
  virtualisation.spiceUSBRedirection.enable = true;

  services.flatpak.enable = true;

  # programs.nix-ld = {
  #   enable = true;
  #   libraries = with pkgs; [
  #     (pkgs.runCommand "steamrun-lib" {} "mkdir $out; ln -s ${pkgs.steam-run.fhsenv}/usr/lib64 $out/lib")
  #   ];
  # };
}
