Detailed setup of screen sharing in Wayland (Sway)

Getting screen sharing to work on Wayland seems to be surprisingly hard. Maybe it is compared to X11 that doesn't require any additional setup at all.

To have working screen sharing on Sway you really need three components installed and set up with correct environment variables.

These three components are:

These three components has to have systemd user services. You should be able to see them in the list if you run systemctl --user, just look for the different programs name ending in .service.

You can also check on each service individually by running:

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

Required environment variables

  • XDG_SESSION_TYPE=wayland
  • XDG_CURRENT_DESKTOP=sway

How you set this may vary depending on your distribution, in NixOS I did set these in a global way. But this, surprisingly to me wasn't enough to make them apply all the way through to systemd user services.

So I had to add the following to command to run before I log into sway:

systemctl --user import-environment

It may be fine to start it after sway is launched as well, it probably just has to be executed before you attempt to do any screen sharing. This command also accepts specific environment variables to be imported.

Don't worry if they don't run at this point. They should be started by socket activation when you start sharing your screen.

Testing screen sharing

Testing screen sharing can easily be done with a patched1 Firefox started with MOZ_ENABLE_WAYLAND=1 set or with a Blink based browser with the pipewire flag enabled in about://flags.

To actually test screen sharing I'd recommend using Mozilla's gUM Test Page, then just start your screen share on that page.

What should happen?

Starting the screen share should trigger the socket activation for the user services for pipewire.service, xdg-desktop-portal.service and xdg-desktop-portal-wlr.service. In other words, these three services should be started automatically when you start sharing your screen.

There's no real use to start them manually if they aren't running at this point since they require the environment variables to work regardless if they run or not.

What if the services doesn't start?

Then it doesn't work. You can just stop the services that may have started, then try to adjust your environment variables and try again.

You can check on each service with the commands above. If you want to stop all or specific services you can run the following to do so:

systemctl --user stop pipewire.service
systemctl --user stop xdg-desktop-portal.service
systemctl --user stop xdg-desktop-portal-wlr.service

Footnotes:

1

NixOS has pulled in a patch for Firefox (older than 83) to support Pipewire. This should mean that it doesn't need patching if it's newer than 83. Link to source.

Date: 2021-02-19 Fri 23:00

Author: Elis Hirwing <elis@hirwing.se>

Created: 2021-02-20 Sat 08:59