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

let
  laptop = builtins.readFile "/etc/hostname" == "syl\n";
in
{

  # nix = {
  #   package = pkgs.nixVersions.git;
  # };

  # This adds hyprland cache to cachix and relaxes sandbox
  nix.settings = {
    substituters = [ "https://hyprland.cachix.org" ];
    trusted-public-keys =
      [ "hyprland.cachix.org-1:a7pgxzMz7+chwVL3/pzj6jIBMioiJM7ypFP8PwtkuGc=" ];
    sandbox = "relaxed";
  };


  # Use the systemd-boot EFI boot loader.
  boot = {
    kernelPackages = pkgs.linuxPackages_zen;
    kernelParams = [ "mem_sleep_default=deep" "nvme.noacpi=1" ];
    # initrd.kernelModules = [ "amdgpu" ];
  };

  # The global useDHCP flag is deprecated, therefore explicitly set to false here.
  # Per-interface useDHCP will be mandatory in the future, so this generated config
  # replicates the default behaviour.
  networking.useDHCP = false;

  boot.supportedFilesystems = [ "ntfs" ];

  services = {
    desktopManager.cosmic.enable = true;
  };

  programs.nix-ld.enable = true;
  programs.ssh.startAgent = true;
  security.pam.services.kwallet = {
    name = "kwallet";
    enableKwallet = true;
  };

  systemd = {
    oomd = {
      enable = true;
      enableRootSlice = true;
      enableUserSlices = true;
      enableSystemSlice = true;
    };
    extraConfig = ''
      DefaultTimeoutStopSec=10s
      DefaultTimeoutStartSec=10s
    '';
  };
  fonts.packages = with pkgs; [
    nerd-fonts.victor-mono
    noto-fonts
    noto-fonts-cjk-sans
    noto-fonts-emoji
    liberation_ttf
    mplus-outline-fonts.githubRelease
    dina-font
    proggyfonts
  ];

  hardware = { keyboard.qmk.enable = true; };

  security.pam.services = {
    # sddm.enableKwallet = true;
    greetd.enableKwallet = true;
    swaylock = { };
  };

  services.displayManager.cosmic-greeter = {
    enable = true;
  };

  services.greetd = {
    enable = true;
    # settings = rec {
    #   initial_session = {
    #     command = "dbus-run-session Hyprland";
    #     # command = "dbus-run-session startplasma-wayland";
    #     user = "chris";
    #   };
    #   default_session = {
    #     command = "${pkgs.greetd.tuigreet}/bin/tuigreet --time -r --window-padding 2 --cmd 'dbus-run-session Hyprland' -s /etc/greetd/environments";
    #     user = "greeter";
    #   };
    #   plasma_session = {
    #     command = "dbus-run-session startplasma-wayland";
    #     user = "chris";
    #   };
    #   hyprland_session = {
    #     command = "Hyprland";
    #     user = "chris";
    #   };
    # };
  };

  programs.regreet = { enable = true; };

  environment.etc."greetd/environments".text = ''
    hyprland
    dbus-run-session startplasma-wayland
    fish
    bash
  '';

  qt = {
    platformTheme = "qt5ct";
    enable = true;
  };

  xdg.portal = {
    enable = true;
    extraPortals = [ pkgs.kdePackages.xdg-desktop-portal-kde ];
  };

  programs.hyprland = {
    enable = true;
    withUWSM = true;
    xwayland.enable = true;
  };

  services.avahi = {
    enable = true;
    nssmdns4 = true;
  };

  # Enable sound.
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
    jack.enable = true;
    wireplumber.enable = true;
  };

  # Needed for some pipewire progs and other gtk apps
  programs.dconf.enable = true;

  environment.variables = {
    EDITOR = "emacsclient -t -a";
    VISUAL = "emacsclient -c -a";
    QT_LOGGING_RULES = "js=true; kwin_*.debug=true";
    # GTK_USE_PORTAL = "1";
    # Fixing Qt apps in other environments
    # Thought I needed this but apparently they are working fine without it.
    # QT_XCB_GL_INTEGRATION = "xcb_egl";
    # QT_QPA_PLATFORM_PLUGIN_PATH = "${pkgs.qt5.qtbase.bin}/lib/qt-${pkgs.qt5.qtbase.qtCompatVersion}/plugins/platforms";
    NIXOS_OZONE_WL = "1";
    _JAVA_AWT_WM_NONEREPARENTING = "1";
    # DISABLE_QT5_COMPAT = "0";
    GDK_BACKEND = "wayland";
    ANKI_WAYLAND = "1";
    WLR_DRM_NO_ATOMIC = "1";
    QT_AUTO_SCREEN_SCALE_FACTOR = "1";
    QT_QPA_PLATFORM = "wayland";
    # QT_WAYLAND_DISABLE_WINDOWDECORATION = "1";
    # QT_QPA_PLATFORMTHEME = "qt5ct";
    MOZ_ENABLE_WAYLAND = "1";
    MOZ_DBUS_REMOTE = "1";
    WLR_BACKEND = "vulkan";
    WLR_RENDERER = "vulkan";
    XDG_SESSION_TYPE = "wayland";
    # Remember that this causes issues in some games use "unset SDL_VIDEODRIVER &&"
    SDL_VIDEODRIVER = "wayland";
    CLUTTER_BACKEND = "wayland";
    WLR_NO_HARDWARE_CURSORS = "1";
  };

  programs.partition-manager.enable = true;
  programs.kdeconnect.enable = true;

  # Turn on flatpak
  services.flatpak.enable = true;

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

  # Enable CUPS to print documents.
  services.printing.enable = true;
  services.printing.drivers =
    [ pkgs.gutenprint pkgs.gutenprintBin pkgs.cnijfilter2 ];

  services.usbmuxd.enable = true;
  services.fstrim.enable = true;

  #######################
  # VIRTUALIZATION
  #######################

  # virtualisation.waydroid.enable = true;
  virtualisation.podman.enable = true;
  # virtualisation.docker.enable = true;
  virtualisation.libvirtd = {
    enable = true;
    qemu = {
      runAsRoot = false;
      ovmf = {
        enable = true;
        # packages = [
        #   pkgs.OVMFFull.fd
        #   pkgs.pkgsCross.aarch64-multiplatform.OVMF.fd
        # ];
      };
      swtpm.enable = true;
    };
  };
  services.samba-wsdd.enable = true;
  services.samba = {
    enable = true;
    # settings = {
    #   workgroup = "WORKGROUP";
    #   "server string" = "smbnix";
    #   "netbios name" = "smbnix";
    #   security = "user";
    #   "hosts allow" = ["192.168.0." "127.0.0.1" "localhost"];
    #   "hosts deny" = "0.0.0.0/0";
    #   "guest account" = "nobody";
    #   "map to guest" = "bad user";
    # };
    shares = {
      public = {
        path = "/home/chris";
        "read only" = false;
        browsable = true;
        "guest ok" = true;
        comment = "Share";
      };
    };
  };

  # services.syncthing = {
  #   enable = true;
  #   dataDir = "/home/chris";
  #   openDefaultPorts = true;
  #   configDir = "/home/chris/.config/syncthing";
  #   user = "chris";
  #   group = "users";
  #   guiAddress = "0.0.0.0:8384";
  #   overrideFolders = true;
  #   overrideDevices = true;
  #   settings = {
  #     gui = {
  #       theme = "dark";
  #     };
  #     devices = {
  #       syl = {
  #         id = "AJMADOK-TENODAA-VSOEW2A-4RXY2XI-YNHIS7H-H3ZYAO5-3UQ64EE-O2N5BAY";
  #       };
  #       kaladin = {
  #         id = "4MQVV7D-WMZ6TZQ-2OSJCHO-ZCOCUCK-DXZLDM4-PWBHJHS-PMI45EH-QGIMAAJ";
  #       };
  #       shadow = {
  #         id = "B24VU6Z-URTMEXN-ZYKRWX7-XWSTHXD-XKX67EB-XSKMT4V-KWKGX7Q-W7DB2QV";
  #       };
  #       tablet = {
  #         id = "4HEXCNH-MCVBZQX-LQ735TG-P2VTJ7N-CZ5MK4P-ICZAPC7-YCXVEWV-7NILMA5";
  #       };
  #       kohlin = {
  #         id = "BSWKBRR-2IJBCHA-UQY7DE7-CV2U2IL-PMFQFCJ-D5ZZJMU-FPYRUPC-MCC32QQ";
  #       };
  #       dalinar = {
  #         id = "MPRMA33-XHW6SRE-D2EDBCP-TSMRNGI-ZCE2TAN-FE4BMKA-PSKGYJW-CRIEJQL";
  #       };
  #     };
  #     folders = {
  #       docs = {
  #         path = "/home/chris/docs";
  #         devices = [ (if laptop then "kaladin" else "syl") "shadow" "dalinar" ];
  #         versioning = {
  #           type = "staggered";
  #           fsPath = "~/.local/syncthing/backup";
  #           params = {
  #             cleanInterval = 3600;
  #             maxAge = 31536000;
  #           };
  #         };
  #       };
  #     };
  #   };
  # };


  # Android Tools
  programs.adb.enable = true;
  services.udev.packages = [ pkgs.android-udev-rules ];

  services.esphome = { enable = true; };
}
