linuxsandboxdbusflatpak

How to set a Flatpak application to autostart?


I have created an application and packaged/released it as a Flatpak (among others). The app has a setting the user can enable so the app will start automatically when the user signs into their system.

This setting works fine as a regular Linux application, however Flatpak packaged apps appear to require using a dbus method to set this up and this doesn't appear to be working for me.

Per the Flatpak docs, I've tried using the RequestBackground dbus method to start the app automatically at login, but it isn't working - even though the response was success, with the return reporting both background & autostart now being true.

If I pass a normal app as the commandline argument like gedit it works as expected and starts automatically when I sign out/in. However passing it the command to run a flatpak like flatpak run <id>, or even /usr/bin/flatpak run <id> doesn't seem to work. How can my flatpak app set itself to run automatically when the user signs into the system?


Output

Using dbus-monitor I can capture the interaction.

Method call

method call time=1690393997.911834 sender=:1.74 -> destination=:1.115 serial=2703 path=/org/freedesktop/portal/desktop; interface=org.freedesktop.impl.portal.Background; member=EnableAutostart
   string ""
   boolean true
   array [
      string "/usr/bin/flatpak"
      string "run"
      string "codes.merritt.Nyrna"
   ]
   uint32 0

method return time=1690393997.912268 sender=:1.115 -> destination=:1.74 serial=1674 reply_serial=2703
   boolean true

Response

signal time=1690393997.912469 sender=:1.74 -> destination=:1.751 serial=2704 path=/org/freedesktop/portal/desktop/request/1_751/dart1647111112; interface=org.freedesktop.portal.Request; member=Response
   uint32 0
   array [
      dict entry(
         string "background"
         variant             boolean true
      )
      dict entry(
         string "autostart"
         variant             boolean true
      )
   ]

Startup log

Checking logs after signing in for information on attempted launch.

journalctl -xe | grep Nyrna
Jul 24 13:38:03 shodan systemd[17012]: Started app-flatpak-codes.merritt.Nyrna-250100.scope.
Jul 24 13:38:03 shodan systemd[17012]: Started app-gnome-codes.merritt.Nyrna-250100.scope - Application launched by gnome-session-binary.
Jul 24 13:38:03 shodan codes.merritt.Nyrna.desktop[250440]: bwrap: execvp /usr/bin/flatpak: No such file or directory

This error here (bwrap: execvp /usr/bin/flatpak: No such file or directory) seems pertinent, however I am not sure what to do with the information.


Solution

  • The portal has to be accessed from within the Flatpak package. Testing the implementation cannot be done from a debug session.

    Set the commandline argument to the executable name of the app, and the portal will automatically add the flatpak run part to the command.

    Because it is running from within the Flatpak package, that environment and the portal know where to find the app's executable, even if it isn't normally on the PATH.

    The portal will create a desktop file on the host filesystem for the app in ~/.config/autostart (or $XDG_CONFIG_HOME) that can be inspected:

    ~/.config/autostart/codes.merritt.Nyrna.desktop

    [Desktop Entry]
    Type=Application
    Name=codes.merritt.Nyrna
    Exec=flatpak run --command=nyrna codes.merritt.Nyrna
    X-Flatpak=codes.merritt.Nyrna