{
  lib,
  pkgs,
  ...
}: let
  # Patch termfilechooser .portal to add Niri to UseIn
  termfilechooser-niri = pkgs.xdg-desktop-portal-termfilechooser.overrideAttrs (old: {
    postFixup =
      (old.postFixup or "")
      + ''
        substituteInPlace $out/share/xdg-desktop-portal/portals/termfilechooser.portal \
          --replace-fail "wlroots;" "wlroots;Niri;"
      '';
  });
in {
  xdg.portal = {
    enable = true;
    config = {
      common = {
        default = ["wlr" "gtk"];
        "org.freedesktop.impl.portal.ScreenCast" = "wlr";
        "org.freedesktop.impl.portal.Screenshot" = "wlr";
        "org.freedesktop.impl.portal.RemoteDesktop" = "wlr";
        "org.freedesktop.impl.portal.Secret" = ["gnome-keyring"];
        "org.freedesktop.impl.portal.OpenURI" = "gtk";
        # FileChooser covers both OpenFile and SaveFile use-cases
        "org.freedesktop.impl.portal.FileChooser" = "termfilechooser";
      };
    };
    extraPortals = [
      pkgs.xdg-desktop-portal-gtk
      pkgs.xdg-desktop-portal-wlr
      termfilechooser-niri
    ];
  };

  environment.sessionVariables = {
    NIX_XDG_DESKTOP_PORTAL_DIR = lib.mkForce null;
  };
}
