flatpak icon indicating copy to clipboard operation
flatpak copied to clipboard

Autostart support

Open pulb opened this issue 10 years ago • 27 comments

Some daemon-like applications like Mailnag store .desktop files in ~/.config/autostart so they’re executed on session start. Does flatpak have support for this?

pulb avatar Jun 15 '16 15:06 pulb

We can't expose random files into autostart, because these will be run on the host side and would completely escape the sandbox. Might be nice to have some built-in support that starts the sandboxed version though.

alexlarsson avatar Jan 27 '17 09:01 alexlarsson

There also needs to be some clear way to toggle it, either by a portal so the app can expose a UI, a command, or relying on desktops adding this.

TingPing avatar Jan 27 '17 10:01 TingPing

Geary could use something like this for its "Always watch for new mail" feature - enabling/disabling that pref installs/deletes a desktop file in ~/.config/autostartthat launches Geary using --hidden at startup, so it starts running in the background when the user logs in.

I can work around this using --filesystem=xdg-config/autostart:create for now though.

mjog avatar Oct 31 '17 05:10 mjog

You need to have 2 moving parts here, whether the application is allowed to run in the background, and whether the app requested to be run in the background. The first one needs to be provided by a Flatpak portal, and be designed such that it allows the creation of a configuration panel to control the toggle, and a store for the application to ask to be run in the background. Again, this cannot be solely under the application's control as you'd likely want to be able to enable the functionality by default, but still control whether the app gets its wish in the configuration dialogue.

hadess avatar Oct 31 '17 09:10 hadess

This was discussed a bit at GUADEC, some random thoughts:

  • Portal sounds reasonable
  • Should be a UI to allow background apps in g-c-c
    • Eventually track what is foreground/background
  • Want a way to set custom exec args
    • Maybe put it in the .desktop file at export time, handles escaping/flatpak details

TingPing avatar Jul 10 '18 09:07 TingPing

I've started to work on this. Rough plan is:

  • add a --background option to flatpak run that makes it fail unless the app has a 'run in background' permission
  • export autostart file, rewrite Exec to use 'flatpak run --background'
  • Add a portal method to let apps request 'run in background' privileges
  • Maybe let that portal create an autostart file if one does not exist
  • Have something monitor apps that are running in the background and kill them if they are not allowed to

matthiasclasen avatar Feb 02 '19 19:02 matthiasclasen

This sounds great! Would the autostart file be removed when the 'run in background' permission is revoked? Also, would be it possible to expose some API in GLib for enabling/disabling autostart for an app that works with the portal when running under Flatpak?

In Geary, the "start in the background" pref actually does double work: It creates an autostart file, but does so with an extra "--hidden" command line arg to the Exec line. This means while the app is launched on login, the GUI only shows up when the icon is first clicked. Does what you have in mind handle that sort of use case? [edit: i.e. the same thing I outlined in a comment above facepalm]

Cheers!

mjog avatar Feb 03 '19 00:02 mjog

The way this will look from the application side is:

  • you install an autostart file with an Exec line that does what you want (can add --hidden there)
  • flatpak will rewrite the Exec line to prepend "flatpak run --background"
  • flatpak run --background will only launch the app if the 'run in background' permission is set

I envision that the control center will show a 'run in background' switch in the applications panel.

No library api for this at the moment. Will see about this later.

matthiasclasen avatar Feb 03 '19 00:02 matthiasclasen

  • Add a portal method to let apps request 'run in background' privileges

You will also need a way for the application to query whether running in the background is allowed, so the app's interface can be adjusted after the initial request has been made and rejected, for example.

hadess avatar Feb 04 '19 00:02 hadess

Good point. The portal can easily return that

matthiasclasen avatar Feb 04 '19 06:02 matthiasclasen

This was handled on the portal side

matthiasclasen avatar May 16 '19 17:05 matthiasclasen

@matthiasclasen Could you post a link to the documentation regarding this feature?

Skycoder42 avatar May 17 '19 11:05 Skycoder42

https://github.com/flatpak/xdg-desktop-portal/blob/master/data/org.freedesktop.impl.portal.Background.xml

matthiasclasen avatar May 17 '19 11:05 matthiasclasen

I'm sorry if I'm asking something stupid but I can't really find any documentation on how to use this new portal feature. I opened all the link of this issue and went through all the issues linked here but I haven't found a single Pull Request or explanation on how to use it.

I understand the general idea:

  • add a --background option to flatpak run that makes it fail unless the app has a 'run in background' permission
  • export autostart file, rewrite Exec to use 'flatpak run --background'
  • Add a portal method to let apps request 'run in background' privileges
  • Maybe let that portal create an autostart file if one does not exist
  • Have something monitor apps that are running in the background and kill them if they are not allowed to
  1. But what is the exact permission that I need to add?
  2. Where is documented?
  3. Is the rewrite of the Exec done in a completely transparent way?
  4. Is there already an application that is taking advantage of it so that I can check how a working implementation looks?

@matthiasclasen I was told on freenode that you might help me :)

leinardi avatar Jul 26 '19 19:07 leinardi

We ended up not adding the --background option to flatpak

I'll try to write up some docs for this

matthiasclasen avatar Jul 27 '19 18:07 matthiasclasen

@matthiasclasen @TingPing Any news about the missing documentation? Can we re-open this issue until there is a documented way to allow a flatpak application to autostart itself on login?

leinardi avatar Sep 01 '19 15:09 leinardi

EDIT: The interface is documented here: https://github.com/flatpak/xdg-desktop-portal/blob/master/data/org.freedesktop.portal.Background.xml#L34

TingPing avatar Sep 02 '19 17:09 TingPing

This doesn't seem to be in a release yet though. (EDIT: It should be but I didn't see it locally)

I definitely have the relevant XML files locally on my Fedora 30 system.

davidstrauss avatar Sep 04 '19 23:09 davidstrauss

I'm sorry but I still don't understand how to use this xml to autostart a flatpak application on login. Can someone provide a working example of some app already using it or some more "for dummies" documentation?

leinardi avatar Sep 26 '19 13:09 leinardi

@leinardi Here is a WIP for an easy api to use this: https://github.com/matthiasclasen/libportal/pull/1

TingPing avatar Sep 27 '19 21:09 TingPing