{ config, pkgs, ... }:

let
  my-python-packages = ps: with ps; [
    numpy pandas matplotlib scipy scikit-learn
    (tensorflow.override { cudaSupport = true; })
    (pytorch.override { cudaSupport = true; })
    jupyterlab ipython
    seaborn plotly
    transformers
    pillow
    opencv4
    pytest
    black flake8 mypy
  ];
in
{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  # Bootloader.
  boot.loader.grub.enable = true;
  boot.loader.grub.device = "/dev/sdb";
  boot.loader.grub.useOSProber = true;

  networking.hostName = "nixos"; # Define your hostname.
  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";
  };

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

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

  # Configure keymap in X11
  services.xserver.xkb = {
    layout = "us,ru";
    variant = "";
    options = "grp:alt_shift_toggle";
  };

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

  # Enable sound with pipewire.
  sound.enable = true;
  hardware.pulseaudio.enable = false;
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
  };

  # Define a user account. Don't forget to set a password with 'passwd'.
  users.users.m = {
    isNormalUser = true;
    description = "m";
    extraGroups = [ "networkmanager" "wheel" "docker" "audio" "video" ];
    packages = with pkgs; [
      # Add user-specific packages here
    ];
  };

  # Enable automatic login for the user.
  services.displayManager.autoLogin.enable = true;
  services.displayManager.autoLogin.user = "m";

  # Workaround for GNOME autologin: https://github.com/NixOS/nixpkgs/issues/103746#issuecomment-945091229
  systemd.services."getty@tty1".enable = false;
  systemd.services."autovt@tty1".enable = false;

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

  # List packages installed in system profile.
  environment.systemPackages = with pkgs; [
    # Basic utilities
    vim neovim
    wget curl
    git
    htop
    zsh
    oh-my-zsh

    # Web browsers
    firefox
    google-chrome

    # Python and data science tools
    (python3.withPackages my-python-packages)

    # Development tools
    vscode
    jetbrains.pycharm-community
    cmake
    gnumake
    gcc
    gdb

    # Version control
    git
    gitAndTools.gitflow
    gh

    # Office suite
    libreoffice

    # Media player
    vlc

    # Messaging
    tdesktop  # Telegram Desktop client
    slack

    # Additional useful tools
    gnome.gnome-tweaks
    gnome.dconf-editor

    # AI/ML specific tools
    cudatoolkit

    # Data visualization
    graphviz
    gnuplot

    # Terminal tools
    tmux
    screen
    terminator

    # Network tools
    wireshark
    nmap

    # File management
    ranger
    mc

    # Text processing
    pandoc
    texlive.combined.scheme-medium

    # Database tools
    postgresql
    mysql
    mongodb

    # Cloud tools
    awscli2
    google-cloud-sdk
    azure-cli

    # Container and virtualization
    docker-compose
    vagrant
  ];

  # Enable Flatpak
  services.flatpak.enable = true;

  # Enable Docker
  virtualisation.docker.enable = true;

  # Enable NVIDIA drivers if you have an NVIDIA GPU
  services.xserver.videoDrivers = [ "nvidia" ];
  hardware.opengl.driSupport32Bit = true;
  hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.stable;

  # Enable OpenCL support
  hardware.opengl.extraPackages = with pkgs; [
    rocm-opencl-icd
    rocm-opencl-runtime
    ocl-icd
    intel-compute-runtime
  ];

  # Enable CUDA
  hardware.opengl.extraPackages32 = with pkgs.pkgsi686Linux; [ libva ];
  hardware.nvidia.modesetting.enable = true;

  # Optimize system for high-performance computing
  boot.kernel.sysctl = {
    "vm.swappiness" = 10;
    "vm.dirty_ratio" = 60;
    "vm.dirty_background_ratio" = 2;
  };

  # Increase max number of file watchers
  boot.kernel.sysctl."fs.inotify.max_user_watches" = 524288;

  # Enable zsh as default shell
  programs.zsh.enable = true;
  users.users.m.shell = pkgs.zsh;

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