16
October
2024
12:03

Why do you need a packet of XDG-DESKTOP-PORTAL, how to use it

16 October 2024 12:03

Linux has several software media on the basis of Linux Systemd and D -Bus services, third -party Flatpak and Snap.

Probably, everyone knows that Flatpak and Snap applications distribution systems use the sandbox mechanism, where applications are launched in a safe environment.

In order for the programs launched inside the “sandboxes” can have access to computer resources, such as file system, camera, sound server - a “back -based” component (Backend) between Snap/Flatpak and the operating system is used. This component is called "XDG Desktop Portal". From Flatpak documentation:

"Portals are frameworks for safe access to resources due to the limits of an isolated software environment. They provide applications with a number of general functions, including: determining the state of the network, opening the file using the file selection tool, the opening of the URI, creating screen images and performing the screen broadcasts [...]"

Portals were developed for use with applications isolated through Flatpak, but any application can use portals to provide a single access to functions independent of desktop and tools. This is usually used, for example, to resolve the overall access to the screen in Wayland via Pipewire or to use the opening and saving files in Firefox, which use the same set of tools as your current desktop environment. ”

When do you need a component of XDG-DESKTOP-PORTAL?

  • It is needed when using Flatpak and Snap

How to install XDG-DESKTOP-portal?

sudo apt update
sudo apt install xdg-desktop-portal xdg-desktop-portal-gtk

How to launch the XDG-Desktop-Portal.Service service?

This service is launched automatically, surrounded by the user when contacting interfaces interacting with the operating system. If desired, it can be launched manually to check the work (condition, status, error), but only surrounded by the current user. Team:

systemctl --user start xdg-desktop-portal.service

Checking the state of service:

systemctl --user status xdg-desktop-portal.service

Service status xdg-desktop-portal.service should be green: "active (running)". Warnings of the type "Failed to load RealtimeKit property: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.RealtimeKit1 was not provided by any .service files " and "No skeleton to export" does not affect the operation of the xdg-desktop-portal.service service; these messages should be ignored.

What is the principle of operation XDG-Desktop-portal.Service?

The component interacts with the system and Flatpak/Snap using the DBUS protocol.

How to delete XDG-DESKTOP-Portal.Service?

If the interaction with the OS from the SNAP packages, Flatpak is not required. You can remove these packages

sudo apt purge xdg-desktop-portal xdg-desktop-portal-gtk

Where

  • XDG-DESKTOP-Portal - the main package.
  • XDG-Desktop-Portal-GTK - a default expansion package for most functions associated with OS (launching applications, file selection, printing, OS settings, bunch of GNOME-KEYRING keys, etc.)

In the documentation on wiki.archlinux.org/title/xdg_desktop_portal сказано, что для каждого окружения рабочего стола есть свои пакеты. Например:

  • environment GNOME-> Package XDG-DESKTOP-Portal-GNOME
  • environment KDE-> packet XDG-DESKTOP-portal-KDE
  • Entricated LXQT-> Package XDG-DESKTOP-Portal-LXQT

How to ensure the work of XDG-Desktop-portal.Service?

The XDG-Desktop-Portal component needs variables

  • XDG_RUNTIME_DIR
  • XDG_CONFIG_HOME
  • XDG_DATA_DIRS
  • et al. See.Description XDG.

Check the current entourage of the desktop and other variables of the XDG:

env | grep XDG

XDG-Desktop-portal configuration files are in the folder /usr/share/xdg-desktop-portal/. They should not be changed.

Also, the component needs the names of the folders listed in the file:
~/.config/user-dirs.dirs

Example - viewing the user's folder list:

cat ~/.config/user-dirs.dirs

Result:
# This File is written by xdg-User-dirs-update
# IF You Want to Change Or Add Directors, Just Edit the Line You're
# intersted in. All Local Changes Will BE Retained On the Next Run.
# Format is XDG_XXX_DIR = "$ home/yyy", What yyy is a shell-uscaped
# Homedir-RELATIVE PATH, Or xDG_XXX_DIR = " /YYY", What /YYY IS
# Absolute Path. No Other Format Is Supported.
#
XDG_DESKTOP_DIR = "$home/desktop"
XDG_DOWNLOAD_DIR = "$home/loading"
XDG_TEMPLATES_DIR = "$home/templates"
XDG_PULICHIRARE_DIR = "$home/public"
XDG_DOCUMENTS_DIR = "$home/documents"
XDG_MUSIC_DIR = "$home/music"
XDG_PICTURES_DIR = "$home/images"
XDG_VIDEOS_DIR = "$home/video"

This file is created when executing a command

xdg-user-dirs-update

The user folders in the OS are called taking into account the current regional language settings in the user profile. The localization of language parameters also determines the variables of the environment that can be viewed using the command locale:

locale

To install on Russian:

sudo update-locale LANG=ru_RU.UTF-8

If the Russian language is selected when installing the OS, the configuration is not required.

Addition from 2026:
If FlatPak is installed, edit your ~/.bashrc file.
For example, using the nano text editor:

nano ~/.bashrc

Add the following line to the end:
export XDG_DATA_DIRS="/usr/local/share/:/usr/share/:/var/lib/flatpak/exports/share:/home/vladimir/.local/share/flatpak/exports/share"

Save the file and exit nano:
Ctrl+S, Ctrl+X.

How to ensure the operation of the XDG-Desktop-Portal portal if it does not start?

Using the import of a system variable surrounded by the user:

Most of the OS have enough this:

systemctl --user import-environment XDG_CURRENT_DESKTOP

In Wayland:

systemctl --user import-environment WAYLAND_DISPLAY  XDG_CURRENT_DESKTOP

Re -launch and check that the portal works:

systemctl --user start xdg-desktop-portal.service
systemctl --user status xdg-desktop-portal.service

How to check the work of the XDG-Desktop-portal.Service service?

1) You need to install the Flatpak application, install another application in it, for example, the Chromium browser as written In a previous article.

2) First, check that the browser is present in the list of installed Flatpak applications.

flatpak list

3) Launch

flatpak run org.chromium.Chromium

4) Select the item "Open the file" - by pressing the key ctrl+o . A system dialogue should appear, which displays the contents of the home folder. This means that the portal works.

Debugging

dbus-monitor --session

and launching an application in another window

flatpak run org.chromium.Chromium

And pressing ctrl+o :

In the window dbus-monitor будет выведено много текста:
string "Org.freedesktop.impl.portal.request"
Error Time = 1729069549.930735 Sender =: 1.49 -> Destination =: 1.46 error_name = org.freedesktop.dbus.rror.unknownmethod reply_serial = 163
string "Объект по пути «/org/freedesktop/portal/desktop/request/1_88/handle_0» не существует"
Method Call Time = 1729069549.931375 Sender =: 1.46 -> Destination =: 1.49 Serial = 164 Path =/Org/Freedesktop/Portal/Desktop; interface = org.freedesktop.impl.portal.filechooser; Member = OpenFile
object path "/org/freedesktop/portal/desktop/request/1_88/handle_0"
string "org.chromium.Chromium"
string "x11:0x3a00019"
string "Открытие файла"
array [
dict entry(
string "modal"
variant boolean true
)
dict entry(
string "filters"
variant array [
]
)
]

Launching the viewing of the URL or the document using XDG-OPEN

Instead of launching an application and opening a document from it, you can use a simple command xdg-open "имя файла" или URL.

For example:

xdg-open https://bfm.ru

or

xdg-open 388643rus.pdf

PDF file for testing - "Message of the CEO of UNESCO*Odre Azule по случаю Всемирного дня радио 13 февраля 2024 года": 388643rus.pdf

  • UNESCO - United Nations for Education, Sciences, and Culture.

The XDG-OPEN utility is included in the XDG-Utils package, installed in Linux by default. The processes launched by her work within the Flatpak container. There is also a package flatpak-xdg-utils, в котором есть особая версия xdg-open, и приложение flatpak-spawn, которое позволяет действия в системе за пределами контейнера. Эта специальная версия xdg-open устанавливается по другому пути: /usr/libexec/flatpak-xdg-utils/ вместо /usr/bin/xdg-open. Установка пакета flatpak-xdg-utils, as a rule, not required.

To manage the rights (permits) of container applications Flatpak, there is a graphic application Flatseal (Com.github.tchx84.Flatseal).

sudo flatpak install com.github.tchx84.Flatseal
flatpak run com.github.tchx84.Flatseal

Flatseal allows you to limit the application permits, like the Android settings "Applications and notifications" - "Applications permission":

For example, resolutions dl browser Chromium from Flatpak:

  • Network - yes
  • Interprocessing communications - yes
  • Window system X11 - Yes.
  • Sound server Pulseaudio - yes
  • Printing system - yes
  • GPU Acceleration (Device = DRI) - by default -> can be changed to yes
  • All devices (webcam) - yes
  • All user files (Filesystem = Home) - yes
  • Background (can work in the background) - no
  • Notifications (may send notifications) - no

Links to documentation:

Flatpak Desktop Integration
XDG Desktop Portal
*Flatpak-Xdg-Utils



Related publications