{ config, pkgs, ... }:


{
  imports = [
    # Include the results of the hardware scan.
    ./hardware-configuration.nix
    ../../shared/common.nix
    ../../shared/sound.nix
    ../../shared/window-manager.nix
    ../../shared/fonts.nix
    ../../shared/gnupg.nix
    ../../shared/garbage-collector.nix
    ../../shared/networking.secret.nix
    ../../shared/ipfs.nix
  ];

  # Configure kernel
  boot = {
    # I cannot use rtl88x2bu driver in stable
    kernelPackages = pkgs.unstable.linuxPackages_6_1;
    extraModulePackages = with config.boot.kernelPackages; [
      rtl88x2bu
    ];
  };

  # Use the GRUB 2 boot loader.
  boot.loader.grub = {
    enable = true;
    device = "/dev/sdb";
    # memtest86.enable = true;
  };

  networking = {
    hostName = "home"; # Define your hostname.

    useDHCP = false;
    interfaces = {
      wlp3s0.useDHCP = true;
      # wlp11s0f3u2.useDHCP = true;
    };

    networkmanager.enable = true;
    firewall.allowedTCPPortRanges = [
      { from = 1300; to = 1400; }
    ];
  };

  # enable bluetooth
  hardware.bluetooth.enable = true;

  nixpkgs.config.allowUnfree = true;
  local.nix.allowUnfreePackages = [
    "cnijfilter2"
    "memtest86"
    "android-sdk-cmdline-tools"
  ];

  services.openssh.enable = true;

  services = {
    avahi = {
      enable = true;
      nssmdns = true;
    };
    printing = {
      enable = true;
      drivers = with pkgs; [ gutenprint cnijfilter2 ];
    };
  };

  services.xserver = {
    # All monitors in the right order
    # Source: https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/x11/xserver.nix#L83
    xrandrHeads = [
      {
        output = "HDMI-1";
        monitorConfig = ''
          Option "PreferredMode" "1920x1080"
          Option "Rotate" "right"
        '';
      }
      {
        output = "DP-1";
        primary = true;
        monitorConfig = ''
          Option "PreferredMode" "2560x1440"
        '';
      }
    ];
  };

  services.logind.extraConfig = ''
    # don’t shutdown when power button is short-pressed
    HandlePowerKey=ignore
  '';

  # Enable the Docker
  virtualisation.docker.enable = true;

  # Additional nix configs
  local.nix.enableMyRegistry = true;

  # Wireguard client
  age.secrets.wireguard-home-private = {
    file = ../../../secrets/wireguard-home-private.age;
    mode = "0400";
  };
  local.wireguard = {
    enable = true;
    ip = "10.20.30.3/24";
    privateKeyFile = config.age.secrets.wireguard-home-private.path;
  };

  # Invisible internet project
  services.i2pd = {
    enable = true;
    proto.httpProxy.enable = true;
    proto.http.enable = true;
  };

  # Torrent client
  services.transmission.enable = true;

  # 3D printing
  local.octoprint.enable = true;

  # Android
  programs.adb.enable = true;
  programs.java = {
    enable = true;
    package = pkgs.jdk17;
  };

  nixpkgs.config.android_sdk.accept_license = true;

  environment.variables =
    let
      buildToolsVersion = "33.0.2";
      androidComposition = pkgs.unstable.androidenv.composeAndroidPackages {
        platformToolsVersion = "34.0.5";
        buildToolsVersions = [ buildToolsVersion ];
        includeEmulator = false;
        emulatorVersion = "34.1.9";
        platformVersions = [ "29" "30" "33" ];
        includeSources = false;
        includeSystemImages = false;
        systemImageTypes = [ "google_apis_playstore" ];
        abiVersions = [ "armeabi-v7a" "arm64-v8a" ];
        cmakeVersions = [ "3.10.2" ];
        includeNDK = true;
        ndkVersions = [ "23.2.8568313" ];
        useGoogleAPIs = false;
        useGoogleTVAddOns = false;
        includeExtras = [
          "extras;google;gcm"
        ];
      };
    in
    rec {
      ANDROID_SDK_ROOT = "${androidComposition.androidsdk}/libexec/android-sdk";
      ANDROID_NDK_ROOT = "${ANDROID_SDK_ROOT}/ndk-bundle";

      # Use the same buildToolsVersion here
      # GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${ANDROID_SDK_ROOT}/build-tools/${buildToolsVersion}/aapt2";
      # JAVA_HOME = pkgs.jdk17.home;
    };

  services.udev.extraRules = ''
    SUBSYSTEM=="usb", ATTR{idVendor}=="12d1", MODE="0666", GROUP="plugdev"
  '';
}
