# Edit this configuration file to define what should be installed on
# your system.  Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running ‘nixos-help’).

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

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

  nix.package = pkgs.lix;  

  nix.settings.trusted-users = [ "alyx" "root" ];

  #services.udev.extraRules = builtins.readFile ./udev.rules;
  services.gnome.gnome-keyring.enable = true;
  security.pam.services.greetd.enableGnomeKeyring = true;
  security.pam.services.greetd.gnupg.enable = true;

  security.pam.services.login = {
    enableGnomeKeyring = true;
    gnupg.enable = true;
  };

  programs.gnupg.agent = {
    enable = true;
    enableSSHSupport = true;
  };

  programs.nh = {
    enable = true;
    clean.enable = true;
    clean.extraArgs = "--keep-since 4d --keep 3";
    flake = "/home/alyx/.config/nixos";
  };

  hardware.wooting.enable = true;
  hardware.openrazer.enable = true;

  environment.pathsToLink = [ "/share/xdg-desktop-portal" ];

  systemd = {
    user.services.polkit-gnome-authentication-agent-1 = {
      description = "polkit-gnome-authentication-agent-1";
      wantedBy = [ "graphical-session.target" ];
      wants = [ "graphical-session.target" ];
      after = [ "graphical-session.target" ];
      serviceConfig = {
          Type = "simple";
          ExecStart = "${pkgs.polkit_gnome}/libexec/polkit-gnome-authentication-agent-1";
          Restart = "on-failure";
          RestartSec = 1;
          TimeoutStopSec = 10;
      };
    };
  };

  security.polkit.enable = true;

  security.sudo.package = pkgs.sudo.override { withInsults = true; };

  programs.steam.extraCompatPackages = with pkgs; [ proton-ge-rtsp-bin ];

  programs.bash.shellAliases = {
    nano = "nvim";
  };

  programs = {
    adb.enable = true;
    flashrom.enable = true;
    nix-ld.enable = true;
    nix-ld.libraries = with pkgs; [xorg.libxcb libao xorg.libX11 libusb1 cargo rustc pkg-config cacert libpulseaudio libsecret file];
  };

  services.journald.extraConfig = ''
    SystemMaxUse=2G
  '';

  services.tailscale = {
    enable = true;
    useRoutingFeatures = "both";
    openFirewall = true;
    interfaceName = "userspace-networking";
  };

  networking.firewall.checkReversePath = "loose";

  networking.useDHCP = false;

  services.resolved.enable = true;
  
  systemd.network.enable = true;
  
  systemd.network.networks."enp0s25" = {
    matchConfig.Name = "enp0s25";
    networkConfig.DHCP = "yes";
  };

  services.networkd-dispatcher = {
    enable = true;
    rules."50-tailscale" = {
      onState = ["routable"];
      script = ''
        "${pkgs.ethtool} NETDEV=$(ip -o route get 8.8.8.8 | cut -f 5 -d " ") | -K enp0s25 rx-udp-gro-forwarding on rx-gro-list off"
      '';
    };
  };
  
  boot.supportedFilesystems = ["exfat" "ntfs" "xfs"];
  boot.loader.limine.enable = true;
  boot.loader.limine.style.wallpapers = [];
  boot.loader.limine.additionalFiles = {
    "efi/memtest86.efi" = "${pkgs.memtest86-efi}/BOOTX64.efi";
    "efi/shell.efi" = "${pkgs.edk2-uefi-shell}/shell.efi";
  };
  boot.loader.efi.canTouchEfiVariables = true;
  boot.initrd.systemd.enable = true;
  boot.loader.efi.efiSysMountPoint = "/boot/";
  boot.kernelParams = ["amdgpu.ppfeaturemask=0xffffffff" "amdgpu.seamless=1" "amdgpu.freesync_video=1" "initcall_blacklist=simpledrm_platform_driver_init" "pcie_acs_override=downstream,multifunction" "preempt=voluntary"];
  boot.extraModulePackages = [
    config.boot.kernelPackages.v4l2loopback.out
    config.boot.kernelPackages.hid-t150.out
  ];
  boot.kernelPackages = pkgs.linuxKernel.packages.linux_xanmod_latest;
  boot.kernel.sysctl = {
    "vm.max_map_count" = 2147483642;
  };

  hardware.display.outputs."DP-4".mode = "2560x1080@75";

  services.monado = {
    enable = true;
    defaultRuntime = true;
  };
 
  systemd.user.services.monado.environment = {
    STEAMVR_LH_ENABLE = "1";
    XRT_COMPOSITOR_COMPUTE = "1";
    WMR_HANDTRACKING = "0";
    AMD_VULKAN_ICD="RADV";
  };

  programs.virt-manager.enable = true;
  systemd.packages = with pkgs; [lact];
  systemd.services.lactd.wantedBy = ["multi-user.target"];
  networking.hostName = "absolution"; # Define your hostname.

  powerManagement.cpuFreqGovernor = "performance";

  # Configure network proxy if necessary
  # networking.proxy.default = "http://user:password@proxy:port/";
  # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";

  # Enable networking
  hardware.graphics = {
    enable = true;
    enable32Bit = true;
    extraPackages = with pkgs; [mesa.opencl libvdpau-va-gl vaapiVdpau vulkan-validation-layers];
    extraPackages32 = with pkgs; [driversi686Linux.amdvlk driversi686Linux.mesa.opencl];
  };

  hardware.bluetooth.enable = true;
  services.blueman.enable = true;

  #networking.networkmanager = {
  #  enable = true;
  #  wifi.backend = "iwd";
  #};

    # Set your time zone.
  time.timeZone = "Europe/London";

  # Select internationalisation properties.
  i18n.defaultLocale = "en_GB.UTF-8";

  services.udisks2.enable = true;

  i18n.extraLocaleSettings = {
    LC_ADDRESS = "en_GB.UTF-8";
    LC_IDENTIFICATION = "en_GB.UTF-8";
    LC_MEASUREMENT = "en_GB.UTF-8";
    LC_MONETARY = "en_GB.UTF-8";
    LC_NAME = "en_GB.UTF-8";
    LC_NUMERIC = "en_GB.UTF-8";
    LC_PAPER = "en_GB.UTF-8";
    LC_TELEPHONE = "en_GB.UTF-8";
    LC_TIME = "en_GB.UTF-8";
  };

  nix.settings.experimental-features = [ "nix-command" "flakes" ];

  nix.gc = {
    automatic = true;
    dates = "weekly";
    options = "--delete-older-than 30d";
  };

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

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

  # Enable CUPS to print documents.
  services.printing.enable = true;
  services.printing.drivers = [pkgs.gutenprint];

  nixpkgs.overlays = [
    (self: super: {
      vlc = super.vlc.override {
        libbluray = super.libbluray.override {
          withAACS = true;
          withBDplus = true;
        };
      };
    })
  ];

  security = {
    rtkit.enable = true;
  };

  # Enable sound with pipewire.
  services.pulseaudio.enable = false;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
    # If you want to use JACK applications, uncomment this
    jack.enable = true;
    extraConfig.pipewire.noresample = { "context.properties" = { "default.clock.allowed-rates" = [ 44100 48000 192000 ]; }; };
    extraConfig.pipewire."92-low-latency" = {
      "context.properties" = {
        "default.clock.rate" = 192000;
        #"default.clock.quantum" = 288;
        #"default.clock.min-quantum" = 32;
        #"default.clock.max-quantum" = 288;
      };
    };
    extraConfig.pipewire-pulse."92-low-latency" = {
      context.modules = [
        {
          name = "libpipewire-module-protocol-pulse";
          args = {
             pulse.min.req = "32/48000";
             pulse.default.req = "32/48000";
             pulse.max.req = "192000";
          #   pulse.min.quantum = "32/48000";
          #   pulse.max.quantum = "288/192000";
          };
        }
      ];
      stream.properties = {
        node.latency = "32/48000";
        resample.quality = 1;
      };
    };
  };

  # Define a user account. Don't forget to set a password with ‘passwd’.
  
  users.users.alyx = {
    isNormalUser = true;
    description = "Alyx";
    extraGroups = [ "openrazer" "pipewire" "gamemode" "inputs" "networkmanager" "wheel" "libvirtd" "camera" "qemu-libvirtd" "lxd" ];
    packages = with pkgs; [
    ];
  };

  services.libinput.enable = true;

  # Allow unfree packages
  nixpkgs.config.allowUnfree = true;

  services.gvfs.enable = true;
 
  environment.systemPackages = with pkgs; [
    hyprlock
    file
    libsecret
    wget
    jmtpfs
    nautilus
    inputs.niri.packages.${pkgs.stdenv.hostPlatform.system}.xwayland-satellite-stable
    openrazer-daemon
    polychromatic
    ethtool
    networkd-dispatcher
    oversteer
    neovim
    cbfstool
    steamtinkerlaunch
    librewolf
    vulkan-tools
    r2modman
    index_camera_passthrough
    wayvr-dashboard
    wlx-overlay-s
    r2mod_cli
    git
    lact
    looking-glass-client
    keepassxc
    pulseaudio
    winetricks
    p7zip
    cabextract
    unzip
    wineWowPackages.stable
    (pkgs.python3.withPackages (ps: with ps; [tkinter]))
  ];

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

}
