# 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"
    ];
    nixPath = [ "nixpkgs=${inputs.nixpkgs}" ];
  };

  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.stable.rocmPackages.clr.icd ]; # Needed for davinci-resolve to utilize the gpu, otherwise it refuses to launch
  };
  # Enable networking
  networking.networkmanager.enable = true;
  services.tailscale.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 PPD to improve battery life maybe
  services.power-profiles-daemon.enable = true;

  # Enable greetd with ReGreet
  services.greetd = {
    enable = true;
    #    settings.default_session = {
    #      command = "${pkgs.greetd.regreet}/bin/regreet";
    #    };
  };
  programs.regreet = {
    enable = true;
    cageArgs = [ "-m" "last" ];
  };

  # Enable the Hyprland WM functionality for NixOS (xdg portals etc.)
  programs.hyprland.enable = true;
  
  # Enable fwupd for BIOS updates
  services.fwupd.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;
  };

  musnix.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;

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

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

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

  # Enable bluetooth and blueman
  hardware.bluetooth = {
    enable = true;
    settings = {
      General = {
        Enable = "Source,Sink,Media,Socket";
      };
    };
  };
  services.blueman.enable = true;

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

  # Enable flatpak for nix-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;
    }
  ];

  system.stateVersion = "24.05"; 
}