# 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, lib, pkgs, inputs, ... }:

{

  imports =
  [
    ./hardware-configuration.nix
  ];

  # Bootloader.
#  boot.loader.efi.canTouchEfiVariables = true;

  boot = {
    initrd.systemd.enable = true;

    loader.systemd-boot.enable = lib.mkForce false;

    lanzaboote = {
      enable = true;
      pkiBundle = "/etc/secureboot";
    };
  };

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

  # default kernel causes issues with wpa_supplicant meaning shutdown times are > 10 mins updating to kernel 69+ fixes this
# boot.kernelPackages = pkgs.linuxKernel.packages.linux_zen;

  # I was told that adding these helps but it doesnt seemt to work as monitors are still buggy at 144hz, may as well keep them
  boot.kernelParams = [
    "video=DP-2:1920x1080@144"
    "video=DP-3:1920x1080@144"
    "video=eDP-2:2560x1200@165"
  ];

  boot.initrd.kernelModules = [ "amdgpu" ];
  boot.extraModulePackages = [ pkgs.linuxPackages.v4l2loopback ];
  boot.kernelModules = [ "v4l2loopback" ];

  networking.hostName = "gabbielaptop"; # Define your hostname.
  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.

  # Configure network proxy if necessary
  # networking.proxy.default = "http://user:password@proxy:port/";
  # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";
  hardware.graphics = {
    enable = true;
    enable32Bit = true;
    extraPackages = [ pkgs.rocmPackages.clr.icd ]; # Needed for davinci-resolve to utilize the gpu, otherwise it refuses to launch
  };
  # Enable networking
  networking.networkmanager.enable = true;

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

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

  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";
  };

  # Enable the X11 windowing system.
  # You can disable this if you're only using the Wayland session.
#  services.xserver.enable = true;

  # Enable the KDE Plasma Desktop Environment.
  services.displayManager.sddm.enable = true;
  services.displayManager.sddm.wayland.enable = true;
#  services.desktopManager.plasma6.enable = true;

  # Enable the Hyprland WM functionality for NixOS (xdg portals etc.)
  programs.hyprland.enable = true;  
#  programs.waybar.enable = true;

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

  # Configure console keymap
  console.keyMap = "uk";

  # Enable CUPS to print documents.
  services.printing.enable = true;

  # Enable sound with pipewire.
  hardware.pulseaudio.enable = false;
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true; # ardour does not seem to be a fan of this
    jack.enable = true;
    wireplumber = {
      enable = true;
      extraConfig.enableMIDI = {
        "wireplumber.settings" = {
          node.features.audio.control-port = true; # This is needed for ardour to recognise my USB MIDI keyboard
        };
      }; # This is JSON
    };
    # use the example session manager (no others are packaged yet so this is enabled by default,
    # no need to redefine it in your config for now)
    #media-session.enable = true;
  };

# pam.services = [
#   { name = "gnome_keyring"
#     text = ''
#       auth     optional    ${gnome3.gnome_keyring}/lib/security/pam_gnome_keyring.so
#       session  optional    ${gnome3.gnome_keyring}/lib/security/pam_gnome_keyring.so auto_start
#
#       password  optional    ${gnome3.gnome_keyring}/lib/security/pam_gnome_keyring.so
#     '';
#   }
# ];

  services.gnome.gnome-keyring.enable = true;

  programs.zsh.enable = true;
  users.users.blue = {
    isNormalUser = true;
    description = "Gabriella Bere";
    extraGroups = [ "networkmanager" "wheel" "dialout" ];
    shell = pkgs.zsh;
  };

#  home-manager = {
#    extraSpecialArgs = { inherit inputs; };
#    users = {
#      "blue" = import ../../home-manager/home.nix;
#    };
#  };

  # Enable fingerprint daemon
  services.fprintd.enable = true;

  # Enable bluetooth and blueman
  hardware.bluetooth.enable = true;
  services.blueman.enable = true;

  # Install firefox.
  programs.firefox.enable = true;

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

  # Enable flatpak for delcarative-flatpak used in home-manager
  services.flatpak.enable = true;

  # Enable qmk for managing framework laptop keyboards
  hardware.keyboard.qmk.enable = true;

  # Enable steam and enable steam remoteplay
  programs.steam.enable = true;
  programs.steam.remotePlay.openFirewall = true;  

  # This desperately needs moving to home.nix but the home-manager module is not yet available
  
  services.protonmail-bridge = {
    enable = true;
    path = [ pkgs.gnome-keyring ];
    logLevel = "debug";
  };

  # protonmail daemon config
#  systemd.user.services.protonmail-bridge = {
#    enable = true;
#    wantedBy = [ "default.target" ];
#    description = "autostart protonmail-bridge on login as a daemon";
#    serviceConfig = {
#      Type = "simple";
#      ExecStart = "${lib.getExe pkgs.protonmail-bridge} --noninteractive --no-window";
#    };
#  };

  # Some programs need SUID wrappers, can be configured further or are
  # started in user sessions.
  # programs.mtr.enable = true;
  # programs.gnupg.agent = {
  #   enable = true;
  #   enableSSHSupport = true;
  # };

  # List services that you want to enable:

  # Enable the OpenSSH daemon.
  # services.openssh.enable = true;

  # Open ports in the firewall.
  networking.firewall.allowedTCPPorts = [ 
    43391 # used to forward minecraft traffic to minecraft.gabbie.blue:33991
    4455 # used for OBS websocket
    7777 # used for Satisfactory Server
  ];
  networking.firewall.allowedUDPPorts = [ 7777 ];
  networking.firewall.allowedUDPPortRanges = [
    {
      from = 45000;
      to = 60000;
    }
  ];
  # Or disable the firewall altogether.
  # networking.firewall.enable = false;

  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It‘s perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "24.05"; # Did you read the comment?

}