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

with lib;
let
  cfg = config.modules.desktop;
in {
  options.modules.desktop = {
    envProto = mkOption {
      type = types.nullOr (types.enum ["x11" "wayland"]);
      description = "What display protocol to use";
    };
  };

  config = mkMerge [
    {
      modules.desktop.fonts.enable = true;
      modules.desktop.fonts.baseFonts = true;

      # mounting and trash functionality, the recommended solution for most file managers
      services.gvfs.enable = true;
    }
    (mkIf (cfg.envProto == "wayland") {
      environment.sessionVariables = {
        # magic environment variables that improve wayland compat
        NIXOS_OZONE_WL = "1";
        _JAVA_AWT_WM_NONEREPARENTING = "1";
        GDK_BACKEND = "wayland,x11";
        ANKI_WAYLAND = "1";
        MOZ_ENABLE_WAYLAND = "1";
        ELECTRON_OZONE_PLATFORM_HINT = "wayland";
        XDG_SESSION_TYPE = "wayland";
        SDL_VIDEODRIVER = "wayland";
        CLUTTER_BACKEND = "wayland";
      };
    })
    (mkIf (cfg.envProto == "x11") {
      services.xserver.excludePackages = [ pkgs.xterm ];
    })
  ];
}
