1{
2 config,
3 pkgs,
4 lib,
5 ...
6}:
7
8let
9 inherit (lib)
10 mkEnableOption
11 mkIf
12 mkOption
13 mkRenamedOptionModule
14 mkRemovedOptionModule
15 teams
16 types
17 ;
18
19 associationOptions =
20 with types;
21 attrsOf (coercedTo (either (listOf str) str) (x: lib.concatStringsSep ";" (lib.toList x)) str);
22in
23
24{
25 imports = [
26 (mkRenamedOptionModule [ "services" "flatpak" "extraPortals" ] [ "xdg" "portal" "extraPortals" ])
27 (mkRemovedOptionModule [
28 "xdg"
29 "portal"
30 "gtkUsePortal"
31 ] "This option has been removed due to being unsupported and discouraged by the GTK developers.")
32 ];
33
34 meta = {
35 maintainers = teams.freedesktop.members;
36 };
37
38 options.xdg.portal = {
39 enable =
40 mkEnableOption ''[xdg desktop integration](https://github.com/flatpak/xdg-desktop-portal)''
41 // {
42 default = false;
43 };
44
45 extraPortals = mkOption {
46 type = types.listOf types.package;
47 default = [ ];
48 description = ''
49 List of additional portals to add to path. Portals allow interaction
50 with system, like choosing files or taking screenshots. At minimum,
51 a desktop portal implementation should be listed. GNOME and KDE already
52 adds `xdg-desktop-portal-gtk`; and
53 `xdg-desktop-portal-kde` respectively. On other desktop
54 environments you probably want to add them yourself.
55 '';
56 };
57
58 xdgOpenUsePortal = mkOption {
59 type = types.bool;
60 default = false;
61 description = ''
62 Sets environment variable `NIXOS_XDG_OPEN_USE_PORTAL` to `1`
63 This will make `xdg-open` use the portal to open programs, which resolves bugs involving
64 programs opening inside FHS envs or with unexpected env vars set from wrappers.
65 See [#160923](https://github.com/NixOS/nixpkgs/issues/160923) for more info.
66 '';
67 };
68
69 config = mkOption {
70 type = types.attrsOf associationOptions;
71 default = { };
72 example = {
73 x-cinnamon = {
74 default = [
75 "xapp"
76 "gtk"
77 ];
78 };
79 pantheon = {
80 default = [
81 "pantheon"
82 "gtk"
83 ];
84 "org.freedesktop.impl.portal.Secret" = [ "gnome-keyring" ];
85 };
86 common = {
87 default = [ "gtk" ];
88 };
89 };
90 description = ''
91 Sets which portal backend should be used to provide the implementation
92 for the requested interface. For details check {manpage}`portals.conf(5)`.
93
94 Configs will be linked to `/etc/xdg/xdg-desktop-portal/` with the name `$desktop-portals.conf`
95 for `xdg.portal.config.$desktop` and `portals.conf` for `xdg.portal.config.common`
96 as an exception.
97 '';
98 };
99
100 configPackages = mkOption {
101 type = types.listOf types.package;
102 default = [ ];
103 example = lib.literalExpression "[ pkgs.gnome-session ]";
104 description = ''
105 List of packages that provide XDG desktop portal configuration, usually in
106 the form of `share/xdg-desktop-portal/$desktop-portals.conf`.
107
108 Note that configs in `xdg.portal.config` will be preferred if set.
109 '';
110 };
111 };
112
113 config =
114 let
115 cfg = config.xdg.portal;
116 packages = [ pkgs.xdg-desktop-portal ] ++ cfg.extraPortals;
117 in
118 mkIf cfg.enable {
119 warnings = lib.optional (cfg.configPackages == [ ] && cfg.config == { }) ''
120 xdg-desktop-portal 1.17 reworked how portal implementations are loaded, you
121 should either set `xdg.portal.config` or `xdg.portal.configPackages`
122 to specify which portal backend to use for the requested interface.
123
124 https://github.com/flatpak/xdg-desktop-portal/blob/1.18.1/doc/portals.conf.rst.in
125
126 If you simply want to keep the behaviour in < 1.17, which uses the first
127 portal implementation found in lexicographical order, use the following:
128
129 xdg.portal.config.common.default = "*";
130 '';
131
132 assertions = [
133 {
134 assertion = cfg.extraPortals != [ ];
135 message = "Setting xdg.portal.enable to true requires a portal implementation in xdg.portal.extraPortals such as xdg-desktop-portal-gtk or xdg-desktop-portal-kde.";
136 }
137 ];
138
139 services.dbus.packages = packages;
140 systemd.packages = packages;
141
142 environment = {
143 systemPackages = packages ++ cfg.configPackages;
144 pathsToLink = [
145 # Portal definitions and upstream desktop environment portal configurations.
146 "/share/xdg-desktop-portal"
147 # .desktop files to register fallback icon and app name.
148 "/share/applications"
149 ];
150
151 sessionVariables = {
152 NIXOS_XDG_OPEN_USE_PORTAL = mkIf cfg.xdgOpenUsePortal "1";
153 NIX_XDG_DESKTOP_PORTAL_DIR = "/run/current-system/sw/share/xdg-desktop-portal/portals";
154 };
155
156 etc = lib.concatMapAttrs (
157 desktop: conf:
158 lib.optionalAttrs (conf != { }) {
159 "xdg/xdg-desktop-portal/${
160 lib.optionalString (desktop != "common") "${desktop}-"
161 }portals.conf".text =
162 lib.generators.toINI { } { preferred = conf; };
163 }
164 ) cfg.config;
165 };
166 };
167}