# Edit this configuration file to define what should be installed on
# your system.  Help is available in the configuration-knot.nix(5) man page
# and in the NixOS manual (accessible by running ‘nixos-help’).
{
  config,
  pkgs,
  lib,
  ...
}: let
  home-manager = builtins.fetchTarball "https://github.com/nix-community/home-manager/archive/release-25.11.tar.gz";
in {
  # Home Manager Configuration
  imports = [
    # Include the results of the hardware scan.
    ./hardware-configuration.nix
    (import "${home-manager}/nixos")
  ];

  # Use Lix instead of Nix
  nixpkgs.overlays = [
    (final: prev: {
      inherit
        (prev.lixPackageSets.stable)
        nixpkgs-review
        nix-eval-jobs
        nix-fast-build
        colmena
        ;
    })
  ];

  nix.package = pkgs.lixPackageSets.stable.lix;

  # Bootloader.
  boot.loader.systemd-boot.enable = false;
  boot.loader.grub = {
    default = 0;
    enable = true;
    useOSProber = true;
    efiSupport = true;
    efiInstallAsRemovable = true;
    device = "nodev";
  };

  #boot.loader.efi.canTouchEfiVariables = true;
  boot.loader.efi.efiSysMountPoint = "/boot";

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

  # Enable Rsymc
  services.rsync.enable = true;

  # 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
  networking.networkmanager.enable = true;
  networking.interfaces.wlo1.ipv4.addresses = [
    {
      address = "192.168.1.15";
      prefixLength = 32;
    }
  ];

  # Enable SSH
  services.openssh = {
    enable = true;
    ports = [22];
    settings = {
      PasswordAuthentication = false;
      AllowUsers = null;
      UseDns = true;
      PermitRootLogin = "no";
    };
  };

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

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

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

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

  # Enable Seatd for Wayland sessions
  # IMPORTANT: Enable seatd service for River WM
  services.seatd = {
    enable = true;
    logLevel = "info";
  };

  # Enable the gnome-keyring secrets vault.
  # Will be exposed through DBus to programs willing to store secrets.
  services.gnome.gnome-keyring.enable = true;

  # Enable Sway window manager
  programs.sway = {
    enable = true;
    wrapperFeatures.gtk = true;
  };

  services.greetd = {
    enable = true;
    settings = {
      default_session = {
        command = "${pkgs.tuigreet}/bin/tuigreet --time --cmd sway";
        user = "greeter";
      };
    };
  };

  # Configure security to allow seatd access
  security.polkit.enable = true;

  # Configure GPU support
  hardware.graphics = {
    enable = true;
    enable32Bit = true;
  };

  # Define a user account. Don't forget to set a password with ‘passwd’.
  # Copy this later to generate more custom users.
  users.users.nixosp = {
    isNormalUser = true;
    description = "NixOS Playground";
    extraGroups = ["networkmanager" "wheel" "seat"];
    packages = with pkgs; [];
  };

  home-manager.users.nixosp = {pkgs, ...}: {
    home.packages = [
      pkgs.atool
      pkgs.httpie
      pkgs.alacritty # Terminal emulator
      pkgs.hyfetch # Add fetching packages
      pkgs.macchina
      pkgs.wayland # Wayland display server
      pkgs.wlroots # Wayland compositor library
      pkgs.maple-mono.NF # Font for better terminal appearance
      pkgs.wl-clipboard # Clipboard utilities for Wayland
      pkgs.mako # Wayland Sway Notification Daemon
      pkgs.btop # Resource monitor
    ];

    # Set Alacritty as the default terminal emulator
    home.sessionVariables = {
      TERMINAL = "alacritty";
    };

    # Use Zsh as the default shell
    programs.zsh.enable = true;

    # Configure Alacritty as the default terminal emulator
    programs.alacritty = {
      enable = true;
      settings = {
        # Window configuration
        window = {
          opacity = 1.0;
          padding = {
            x = 10;
            y = 10;
          };
        };

        # Font configuration - fixes spacing issues
        font = {
          normal = {
            family = "Maple Mono NF";
            style = "Regular";
          };
          bold = {
            family = "Maple Mono NF";
            style = "Bold";
          };
          italic = {
            family = "Maple Mono NF";
            style = "Italic";
          };
          bold_italic = {
            family = "Maple Mono NF";
            style = "Bold Italic";
          };
          size = 14.0;
        };

        # Colors (optional - using default Alacritty colors)
        colors = {
          primary = {
            background = "#1e1e2e";
            foreground = "#cdd6f4";
          };
        };
      };
    };

    # Configure Hyfetch system info fetcher
    programs.hyfetch = {
      enable = true;
      settings = {
        preset = "lesbian"; # Use lesbian flag preset
        mode = "rgb"; # Use RGB color mode
        lightness = 0.55; # Set to 55% brightness
        backend = "fastfetch"; # Use macchina as the backend
        logo_size = "small"; # Make small logo
        pride_month_disable = false; # Enable pride month mode (or true to disable)
        pride_month_shown = []; # List of shown pride month flags
        color_align = {
          mode = "horizontal";
        };
      };
    };

    # The state version is required and should stay at the version you
    # originally installed.
    home.stateVersion = "25.11";
  };

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

  # Passwordless sudo for wheel group
  security.sudo.wheelNeedsPassword = false;

  # Hardware U2F support - Passwordless sudo with hardware key
  security.pam.u2f = {
    enable = true;
    settings = {
      authfile = "/etc/u2f_keys";
      cue = true;
      pinverification = 0; # No PIN verification
      userpresence = 1; # Require user presence (touch)
    };
  };

  # SSH Agent authentication
  security.pam.sshAgentAuth.enable = true;

  # Automatic upgrades
  system.autoUpgrade = {
    enable = true; # Set to true for automatic updates
    dates = "daily";
    allowReboot = false;
  };

  # System packages
  environment.systemPackages = with pkgs; [
    # Network tools
    wget
    curl
    dig
    tcpdump
    ethtool
    iptables
    nftables
    iproute2
    bridge-utils
    netcat-gnu
    traceroute
    mtr

    # Monitoring
    btop
    htop
    iotop
    bandwhich

    # Editors
    micro
    vim
    helix

    # System info
    fastfetch
    lshw
    pciutils
    usbutils
  ];

  # Enable zram swap
  zramSwap = {
    enable = true;
    memoryPercent = 50;
  };

  # 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 = [ ... ];
  # networking.firewall.allowedUDPPorts = [ ... ];
  # 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-knot.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "25.11"; # Did you read the comment?
}
