summary refs log tree commit diff
path: root/nixos/modules/config/xdg/portal.nix
blob: bdbbfda2bb424be5fe53e5f2d0f50e6731eae253 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{ config, pkgs ,lib ,... }:

with lib;

{
  options.xdg.portal = {
    enable =
      mkEnableOption "<link xlink:href='https://github.com/flatpak/xdg-desktop-portal'>xdg desktop integration</link>"//{
        default = false;
      };

    extraPortals = mkOption {
      type = types.listOf types.package;
      default = [];
      description = ''
        List of additional portals to add to path. Portals allow interaction
        with system, like choosing files or taking screenshots. At minimum,
        a desktop portal implementation should be listed. GNOME and KDE already
        adds <package>xdg-desktop-portal-gtk</package>; and
        <package>xdg-desktop-portal-kde</package> respectively. On other desktop
        environments you probably want to add them yourself.
      '';
    };

    gtkUsePortal = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Sets environment variable <literal>GTK_USE_PORTAL</literal> to <literal>1</literal>.
        This is needed for packages ran outside Flatpak to respect and use XDG Desktop Portals.
        For example, you'd need to set this for non-flatpak Firefox to use native filechoosers.
        Defaults to <literal>false</literal> to respect its opt-in nature.
      '';
    };
  };

  config =
    let
      cfg = config.xdg.portal;
      packages = [ pkgs.xdg-desktop-portal ] ++ cfg.extraPortals;

    in mkIf cfg.enable {

      assertions = [
        { assertion = (cfg.gtkUsePortal -> cfg.extraPortals != []);
          message = "Setting xdg.portal.gtkUsePortal to true requires a portal implementation in xdg.portal.extraPortals such as xdg-desktop-portal-gtk or xdg-desktop-portal-kde.";
        }
      ];

      services.dbus.packages  = packages;
      systemd.packages = packages;

      environment.variables = {
        GTK_USE_PORTAL = mkIf cfg.gtkUsePortal "1";
        XDG_DESKTOP_PORTAL_PATH = map (p: "${p}/share/xdg-desktop-portal/portals") cfg.extraPortals;
      };
    };
}