Portals are a standardised framework allowing desktop applications to use resources outside of their sandbox. The file chooser portal, for example, opens a native file chooser on the host system. When the user selects a file, the application is granted access to that file.
Portals provide a range of common features to applications, including:
See the Portal API reference for all supported portals.
Portals originated from the Flatpak project, but are now a common Linux desktop standard with support from GNOME, KDE and Snapcraft. They are even used outside of sandboxes to provide a standardised API to common desktop features such as screenshots and screen casts on wayland.
Use the Portal API’s in your application Unlike regular Snapcraft interfaces, portals require applications to use a new API in order to access resources. Toolkits like GTK 3 and Qt5, however, provide transparent support for portals. See Portal support in GTK 3 or Portal support in Qt5 and KDE for detailed information. If your application is not using one of those toolkits, you will need to use the Portals API directly. See the Portals API documentation for more information.
Add the desktop
interface to your snap
This interface gives your snap access to the portals.
Enable Portal support in your application.
GTK 3: turn on portal support in GTK 3 by setting the following environment variable:
GTK_USE_PORTAL=1
Qt: often defaults to using portals, but you can enable it manually by changing the platform theme:
Set QT_QPA_PLATFORMTHEME=gtk3
on GTK based desktops and
Set QT_QPA_PLATFORMTHEME=flatpak
and QT_QPA_FLATPAK_PLATFORMTHEME=kde
for Qt based desktops.
Electron: portal support is present in the Electron file chooser.
Both the gnome
extension and the kde-neon
extension automatically enable portal support for GTK 3 and Qt applications on GTK-based desktops. If your snap uses either extension, you only need to do step 1.
It is recommended to use the file chooser portal instead of the home
and removable-media
interfaces for the following reasons:
home
interface does not give your app access to hidden files and folders in the home directory for security reasons. Note that the home
interface does give access to hidden files and folders elsewhere, just not in the home directory itself.removable-media
interface, however, does not auto-connect by default.However, the file chooser portal works a bit differently than the home interface:
/run/user/<uid>/doc/<hash>/
in order to give your application access to it. So the path your application sees is different from the path a user chose, even though both are the same file.The FileChooser portal also contains a few bugs:
xdg-desktop-portal
> 1.8.0.org.freedesktop.portal.Flatpak.Spawn
only works in a Flatpak. If your application needs to run arbitrary binaries on the host system, you can use classic confinement.xdg-desktop-portal
in the host system. Older versions do not support all portals. Repology shows what version of xdg-desktop-portal
each distribution has and the portals NEWS file explains what portals each version supports.See Desktop applications for more information on how to snap a desktop application.
Last updated 11 months ago.