{
  pkgs,
  config,
  lib,
  ...
}:
let
  inherit (config.grimmShared)
    enable
    portals
    sound
    screens
    ;
  inherit (lib)
    mkIf
    mkEnableOption
    mapAttrs'
    foldl'
    min
    getExe
    isInt
    nameValuePair
    ;
in
{
  config = mkIf (enable && portals) {
    xdg.icons.enable = true;
    xdg.sounds.enable = lib.mkIf sound.enable true;

    xdg.portal = {
      enable = true;
      xdgOpenUsePortal = true;
      extraPortals = with pkgs; [
        xdg-desktop-portal-wlr
        xdg-desktop-portal-kde
        xdg-desktop-portal-gtk
        lxqt.xdg-desktop-portal-lxqt
      ];

      wlr.enable = true;
      wlr.settings = mapAttrs' (
        name: value:
        nameValuePair ("screencast_" + name) {
          output_name = value.id;
          max_fps = if isInt value.fps then value.fps else (foldl' min 2147483647 value.fps);
          chooser_type = "simple";
          chooser_cmd = "${getExe pkgs.slurp} -f %o -or";
        }
      ) screens;
    };

    environment.sessionVariables = {
      XDG_CONFIG_HOME = "$HOME/.config";
      XDG_DESKTOP_DIR = "$HOME/Desktop";
      XDG_DOCUMENTS_DIR = "$HOME/Documents";
      XDG_DOWNLOAD_DIR = "$HOME/Downloads";
      XDG_MUSIC_DIR = "$HOME/Music";
      XDG_PICTURES_DIR = "$HOME/Pictures";
      XDG_PUBLICSHARE_DIR = "$HOME/Public";
      XDG_TEMPLATES_DIR = "$HOME/Templates";
      XDG_VIDEOS_DIR = "$HOME/Videos";
    };

    environment.systemPackages = with pkgs; [
      xwaylandvideobridge
      xdg-user-dirs
      confwhich
    ];
  };

  options.grimmShared.portals = mkEnableOption "Enables portals for wlr, gtk and kde as well as fixes fonts";
}