{ config, pkgs, lib, ... }:

let
  # Define isUEFI as a separate variable
  isUEFI = lib.pathExists "/sys/firmware/efi/efivars";
in
{
  imports = [
    ./hardware-configuration.nix
    <musnix>  # Добавьте эту строку для импорта musnix
  ];

  # 1. Bootloader Configuration
  boot.loader = if isUEFI then {
    # If your system uses UEFI, use systemd-boot
    systemd-boot.enable = true;
    systemd-boot.efiInstallAsRemovable = false;
    efi.canTouchEfiVariables = true;
  } else {
    # If your system uses BIOS, use GRUB
    grub = {
      enable = true;
      device = "/dev/sda"; # Ensure /dev/sda is correct
      useOSProber = true;
    };
  };

  # 2. Networking
  networking = {
    hostName = "nixos";
    networkmanager.enable = true;
    nameservers = [ "8.8.8.8" "1.1.1.1" ]; # Removed "127.0.0.1" unless running a local resolver
  };

  # 3. Localization
  i18n = {
    defaultLocale = "en_ZA.UTF-8";
    supportedLocales = [
      "en_ZA.UTF-8/UTF-8"
      "zu_ZA.UTF-8/UTF-8"
      "zu_ZA/ISO-8859-1"
    ];
    extraLocaleSettings = {
      LC_ADDRESS = "zu_ZA.UTF-8";
      LC_IDENTIFICATION = "zu_ZA.UTF-8";
      LC_MEASUREMENT = "zu_ZA.UTF-8";
      LC_MONETARY = "zu_ZA.UTF-8";
      LC_NAME = "zu_ZA.UTF-8";
      LC_NUMERIC = "zu_ZA.UTF-8";
      LC_PAPER = "zu_ZA.UTF-8";
      LC_TELEPHONE = "zu_ZA.UTF-8";
      LC_TIME = "zu_ZA.UTF-8";
    };
  };

  # 4. X11 and Desktop Environment
  services.xserver = {
    enable = true;
    displayManager.lightdm.enable = true;
    desktopManager.mate.enable = true;
    xkb = {
      layout = "us,ru";
      variant = "";
      options = "grp:alt_shift_toggle"; # Switch between US and RU with Alt+Shift
    };
  };

  # 5. Sound Configuration with PipeWire
  sound.enable = true;
  hardware.pulseaudio.enable = false; # Disable PulseAudio
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true; # Enable PulseAudio emulation via PipeWire
  };

  # 6. Bluetooth
  hardware.bluetooth.enable = true; # Ensure your system has the necessary Bluetooth hardware
  services.blueman.enable = true;   # Manage Bluetooth devices with Blueman (GTK-based)

  # 7. User Configuration
  users.users.m = {
    isNormalUser = true;
    description = "m";
    extraGroups = [ "networkmanager" "wheel" "bluetooth" "docker" "audio" ]; # Added "docker" and "audio" group
    hashedPassword = "sha512$..."; # Replace with your actual hashed password
    packages = with pkgs; [ firefox telegram-desktop ];
    openssh.authorizedKeys.keys = [ "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ..." ]; # Replace with your SSH key
  };

  # 8. Auto Login and TTY Configuration
  services.displayManager.autoLogin = {
    enable = true;
    user = "m";
  };

  # Decide whether to disable TTYs
  # If you disable getty@tty1 and autovt@tty1, you cannot access TTY1
  # It is recommended to keep at least one TTY enabled for troubleshooting
  systemd.services."getty@tty1".enable = false;
  systemd.services."autovt@tty1".enable = false;

  # 9. Allow Unfree Packages
  nixpkgs.config.allowUnfree = true;

  # 10. System Packages
  environment.systemPackages = with pkgs; [
    vim
    vscode
    wget
    docker
    htop
    git
    curl
  ];

  # 11. System State Version
  system.stateVersion = "24.05"; # Ensure this matches your NixOS version

  # 12. Environment Variables
  environment.variables = {
    EDITOR = "pluma"; # Set to MATE's default text editor
  };

  # 13. Experimental Features
  nix.settings.experimental-features = [ "nix-command" "flakes" ];
  # Ensure you have a Nix version that supports these features

  # 14. Docker Configuration
  virtualisation.docker.enable = true;

  # 15. Firewall
  networking.firewall = {
    enable = true;
    allowedTCPPorts = [ 22 80 443 ];
    allowedUDPPorts = []; # Add UDP ports if necessary
    # You can also specify allowed services, e.g., SSH, HTTP, HTTPS
  };

  # 16. Swap Space
  swapDevices = [
    {
      device = "/swapfile";
      size = 4096;
    }
  ];

  # Additional swap-related settings
  boot.kernel.sysctl."vm.swappiness" = lib.mkForce 60;  # Используем lib.mkForce для принудительного использования значения
  boot.tmp.useTmpfs = true;  # Исправляем опцию `boot.tmpOnTmpfs` на `boot.tmp.useTmpfs`

  # 17. Fonts
  fonts.packages = with pkgs; [
    noto-fonts
    noto-fonts-cjk
    noto-fonts-emoji
  ];

  # 18. Time Synchronization
  services.timesyncd.enable = true;

  # 19. Security Enhancements
  system.autoUpgrade = {
    enable = true;      # Enable automatic updates
    allowReboot = true; # Allow automatic reboots after updates
  };

  # 20. musnix Real-Time Audio Configuration
  musnix = {
    enable = true;
    kernel.realtime = true;
    das_watchdog.enable = true;
    alsaSeq.enable = true;
    ffado.enable = false; # Set to `true` if you need FireWire support
    rtcqs.enable = false;
    soundcardPciId = ""; # Replace with your sound card PCI ID if needed
  };


  # 21. Optional: Home Manager Integration
  # Consider integrating Home Manager for user-specific configurations
  # Uncomment and configure if desired
  # imports = [
  #   ./hardware-configuration.nix
  #   <home-manager/nixos>
  # ];
  # home-manager.users.m = { ... };

  # 22. Final Thoughts
  # Ensure all commented sections are configured as per your needs.
  # Regularly update your system and configuration to incorporate latest changes.
}
оптимизируй настройки для аудиофила и обьясни