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

{
  nixpkgs.config.allowUnfree = true;

  imports =
    [
      ./hardware-configuration.nix
      ./keyboard
      (import ./services pkgs)
    ];

  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;
  boot.loader.timeout = 0;

  boot.kernelPackages = pkgs.linuxPackages_latest;

  networking.hostName = "nixos";
  networking.networkmanager.enable = true;

  time.timeZone = "Asia/Tokyo";

  i18n.defaultLocale = "en_US.UTF-8";
  console = {
    font = "ter-v32n";
    packages = with pkgs; [ terminus_font ];
    keyMap = "us";
  };

  fonts.packages = with pkgs; [ plemoljp-nf ];

  users.users.${username} = {
    isNormalUser = true;
    shell = pkgs.zsh;
    extraGroups = [ "dialout" "video" "wheel" ];
    hashedPassword = "$y$j9T$srZCpiaeafhJn6f4ZWKvV1$uhpACgzDkWf8I66j6fdfNFL3fdA0XmbWzv5/izWnjA1";
  };

  users.mutableUsers = false;

  environment.systemPackages = with pkgs; [
    bash
    vim
    vulkan-loader
    vulkan-tools
    wl-clipboard
    wget
    xwayland-satellite
  ];

  programs.gnupg.agent = {
    enable = true;
    enableSSHSupport = true;
  };
  programs.mtr.enable = true;
  programs.niri.enable = true;
  programs.nix-ld.enable = true;
  programs.zsh = {
    enable = true;
    enableCompletion = true;
    autosuggestions.enable = true;
    syntaxHighlighting.enable = true;
  };

  services.getty.autologinUser = username;

  services.kubo.enable = true;

  services.openssh.enable = true;

  services.tlp = {
    enable = true;
    settings = {
      AMDGPU_ABM_LEVEL_ON_BAT = 3;
      CPU_BOOST_ON_BAT = 0;
      CPU_ENERGY_PERF_POLICY_ON_BAT = "power";
      CPU_HWP_DYN_BOOST_ON_BAT = 0;
      NMI_WATCHDOG = 0;
      PLATFORM_PROFILE_ON_BAT = "low-power";
      RADEON_DPM_PERF_LEVEL_ON_BAT = "auto";
      RADEON_DPM_STATE_ON_BAT = "battery";
      RADEON_POWER_PROFILE_ON_BAT = "low";
      RESTORE_THRESHOLDS_ON_BAT = 0;
      TLP_DEFAULT_MODE = "BAT";
    };
  };

  services.udev.extraRules = let
    p = "${pkgs.coreutils}/bin";
  in ''
    ACTION=="add", SUBSYSTEM=="backlight", RUN+="${p}/chgrp video $sys$devpath/brightness", RUN+="${p}/chmod g+w $sys$devpath/brightness"
  '';

  # This option defines the first version of NixOS you have installed on this particular machine,
  # and is used to maintain compatibility with application data (e.g. databases) created on older NixOS versions.
  #
  # Most users should NEVER change this value after the initial install, for any reason,
  # even if you've upgraded your system to a new NixOS release.
  #
  # This value does NOT affect the Nixpkgs version your packages and OS are pulled from,
  # so changing it will NOT upgrade your system - see https://nixos.org/manual/nixos/stable/#sec-upgrading for how
  # to actually do that.
  #
  # This value being lower than the current NixOS release does NOT mean your system is
  # out of date, out of support, or vulnerable.
  #
  # Do NOT change this value unless you have manually inspected all the changes it would make to your configuration,
  # and migrated your data accordingly.
  #
  # For more information, see `man configuration.nix` or https://nixos.org/manual/nixos/stable/options#opt-system.stateVersion .
  system.stateVersion = "25.05"; # Did you read the comment?

  systemd.user.services.niri.wantedBy = [ "default.target" ];

  virtualisation.virtualbox.host.enable = true;
  boot.blacklistedKernelModules = [ "kvm" "kvm_amd" "kvm_intel" ];

  nix.settings.experimental-features = [ "nix-command" "flakes" ];
}