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

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

  # Bootloader.
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;
  boot.kernel.sysctl = {
    "net.ipv6.conf.all.disable_ipv6" = 1;
    "net.ipv6.conf.default.disable_ipv6" = 1;
    "net.ipv6.conf.lo.disable_ipv6" = 1;
  };
  # Boot blacklist WiFi drivers
  boot.blacklistedKernelModules = [
  "mt7921e"
  "mt7921_common"
  "mt76_connac_lib"
  "mt76"
  "mt792x_lib"
  "mt7921_lib"
  ];
  hardware.enableRedistributableFirmware = true;
  boot.extraModulePackages = with config.boot.kernelPackages; [
    r8125
  ];
  boot.kernelModules = [ "r8169" ];

  networking.hostName = "homelab"; # Define your hostname.
  networking.networkmanager.wifi.powersave = false;
  networking.wireless.enable = false;  # 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";

  # Enable networking
  networking.networkmanager.enable = true;

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

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

  i18n.extraLocaleSettings = {
    LC_ADDRESS = "ru_RU.UTF-8";
    LC_IDENTIFICATION = "ru_RU.UTF-8";
    LC_MEASUREMENT = "ru_RU.UTF-8";
    LC_MONETARY = "ru_RU.UTF-8";
    LC_NAME = "ru_RU.UTF-8";
    LC_NUMERIC = "ru_RU.UTF-8";
    LC_PAPER = "ru_RU.UTF-8";
    LC_TELEPHONE = "ru_RU.UTF-8";
    LC_TIME = "ru_RU.UTF-8";
  };
  
  # My Services
  services.caddy.enable = true;
  services.caddy.configFile = "/opt/homelab/services/caddy/Caddyfile";
  systemd.tmpfiles.rules = [
  "d /var/log/caddy 0750 caddy caddy -"
  "L+ /home/pvlx/.ssh/conf.d - - - - /home/pvlx/repos/ssh_config"
  ];
  services.fail2ban.jails.sshd = {
  enabled = true;
  settings = {
    port = "ssh";        # or "22"
    backend = "systemd";
    maxretry = 5;
    findtime = "10m";
    bantime = "12h";
    };
  };
  services.fail2ban = {
    enable = true;

    #LAN
    ignoreIP = [
      "127.0.0.1/8"
      "192.168.1.0/24"
    ];
    jails.vaultwarden = ''
      enabled = true
      filter = vaultwarden
      logpath = /var/log/caddy/access.log
      backend = auto
      port = http,https
      bantime = 1h
      findtime = 10m
      maxretry = 5
    '';
    jails.immich = ''
      enabled = true
      filter = immich
      logpath = /var/log/caddy/access.log
      backend = auto
      port = http,https
      maxretry = 5
      findtime = 10m
      bantime = 1h
    '';
    jails.gitea = ''
    enabled = true
    filter = gitea
    logpath = /var/log/caddy/access.log
    backend = auto
    port = http,https
    maxretry = 5
    findtime = 10m
    bantime = 1h
    datepattern = "ts\":Epoch"
   '';
   };
  

  environment.etc."fail2ban/filter.d/vaultwarden.conf".text = ''
    [Definition]
    # Vaultwarden login endpoint
    failregex = ^.*"remote_ip":"<HOST>".*"host":"vault\.sesur\.dev".*"uri":"\/identity\/connect\/token".*"status":(400|401).*$
    ignoreregex =
  '';
  environment.etc."fail2ban/filter.d/immich.conf".text = ''
  [Definition]
  failregex = ^.*"remote_ip":"<HOST>".*"uri":"\/api\/auth\/login".*"status":401.*$
  ignoreregex =
  '';
  environment.etc."fail2ban/filter.d/gitea.conf".text = ''
  [Definition]
  failregex = ^.*"remote_ip":"<HOST>".*"method":"POST".*"uri":"\/user\/login".*"status":200.*$
  ignoreregex =
  '';
  
  # For VsCode
  programs.nix-ld.enable = true;
  
  # Disable suspend of my homelab. Added when my server every ~15 minutes suspend.
  services.logind.settings.Login = {
  HandlePowerKey = "poweroff";
  HandleSuspendKey = "ignore";
  HandleHibernateKey = "ignore";
  HandleLidSwitch = "ignore";
  IdleAction = "ignore";
  };

  # Hard block at systemd level (cannot suspend even if requested)
  systemd.targets.sleep.enable = false;
  systemd.targets.suspend.enable = false;
  systemd.targets.hibernate.enable = false;
  systemd.targets.hybrid-sleep.enable = false;
  
  # Enable the X11 windowing system.
  services.xserver.enable = false;

  # Enable the GNOME Desktop Environment.
  services.xserver.displayManager.gdm.enable = false;
  services.xserver.desktopManager.gnome.enable = false;
  services.xserver.displayManager.gdm.autoSuspend = false;

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

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

  # 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;
    # If you want to use JACK applications, uncomment this
    #jack.enable = true;

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

  # Enable touchpad support (enabled default in most desktopManager).
  # services.xserver.libinput.enable = true;

  # Define a user account. Don't forget to set a password with ‘passwd’.
  users.users.pvlx = {
    isNormalUser = true;
    description = "Pavel";
    extraGroups = [ "networkmanager" "wheel" "docker" ];
    packages = with pkgs; [
    #  thunderbird
    ];
  };

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

  #Enable zsh
  programs.zsh.enable = true;
  # Allow unfree packages
  nixpkgs.config.allowUnfree = true;


  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [
  #  vim # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default.
    # Util
    age
    btop
    dig
    curl
    pciutils
    git
    pkgs.inetutils
    pkgs.lsof
    wget
    zip
    unzip
    pkgs.sysstat
    yt-dlp
    openssl
    # Dev
    docker-compose
    python3
  ];
  system.autoUpgrade = {
    enable = true;
    flags = [ "--upgrade" ];
    dates = "06:00";
    randomizedDelaySec = "30min";
  };
  virtualisation.docker.enable = true;
  
  security.wrappers.btop = {
    owner = "root";
    group = "root";
    capabilities = "cap_sys_rawio+ep";
    source = "${pkgs.btop}/bin/btop";
  };

  # 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;
  services.openssh.settings.PasswordAuthentication = false;
  services.openssh.settings.PermitRootLogin = "no";
  users.users.pvlx = {
    openssh.authorizedKeys.keys = [
    "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDHL6Qz4rVmRwQyx6KY1LZJTw39oN6VKZubKqfx61TJrNcVV/YrnyAIJneJRJVXrXZYTIKs/0mp5orKhTPHeSiJXIPI6xYstz5NobPcNRA2mqamUWXsmJ5W0oiksrB9ZyV2kPjgLDe9/QQK+C5pf6NK3S4q9dfk4yDTqcn7iDbVLbbANxGSLGAzWA1S5yUepVVq7KRBpadKR7kG88ZNtb0uYuGFWxgbZ6DYFNI+Xr7Ackqzn2EKhJeQYB6jUDog6rB2F2gd/iHcCsaSHjkdISrrghMUOosGm6+mOSGba/0QKW2jr8hm581QA6gplOZHFwNNeL1X4cb4S+48PyjKwzmh8dzRaKv0j/4C7XpoxWj2sZm0h4C4l+TZ+tliphg+IC4v/XiVn9yMqg72jvZTon4EAIHLkh4i4oBAGKr2Vn+rHx2owz2Ye9wwI/NEpZ/jMhcm45IT1L+AcLb8I9TNdAfvKA0sRfZwO866ACIVxYfktISK7ZCATVYpNAhq57fNNskrQwqZo5CJOljWKZJJVk1Cqjvhe//S+Nqyi+93LPQZXwROQnT2hayxUZ0XxxUGspvHVAoDCABH9rTOsDDqBRjZtBiNnAek7ho/azNXK0wo1hIJS6/MQm6ER7ybJJDgKsA5qb8jZtJd1UyznbUjSGLvlWhWQZfGXYS0tfOlZfjsYQ== pavel123357@gmail.com"
  ];
};
  programs.git = {
  enable = true;
  config = {
    user.name = "Pavel";
    user.email = "pavel123357@gmail.com";
    init.defaultBranch = "main";
  };
};

  # Open ports in the firewall.
  networking.firewall.allowedTCPPorts = [22 80 443 53 3389 3724 8086];
  networking.firewall.allowedUDPPorts = [53 3389 ];
  # 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.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "25.11"; # Did you read the comment?

}
