How to Always Run a Flatpak Application with Filesystem Permission

flatpak

I installed the Byte (com.github.alainm23.byte) app from the flathub remote through flatpak. That is a music player app. My music library is stored in another HD than my system, which is correctly being mounted at startup at /media/myuser/D2.

Since flatpak apps have limited access to the host environment I have to provide a filesystem permission to the app so that it can access the desired path.

According to the docs I can set that permission with the run and override commands.

When I launch the app from the terminal using flatpak run --filesystem=/media com.github.alainm23.byte it works as intended and the app can access the desired path.

However when I launch the app from the system menu, the app can't access the path. I thought that by running the override command with the filesystem permission (sudo flatpak override --filesystem=/media com.github.alainm23.byte) would solve the issue and allow the app to have access to the path even when launched from the system menu or even when running from the terminal without the permission (flatpak run com.github.alainm23.byte), but it didn't, I even restarted my machine to test it.

When I run flatpak override --show com.github.alainm23.byte it outputs:

[Context]
filesystems=/media;

That makes me think the permission was correctly set.

Having the override set, if I run it from the terminal without the permission (flatpak run com.github.alainm23.byte), when trying to access the file system in the app, it outputs:

(com.github.alainm23.byte:2): Gtk-WARNING **: 11:40:55.105: Failed to measure available space: Erro ao obter informações do sistema de arquivos para /media/myuser/D2: No such file or directory

So, how can the app be launched from the system menu with permission to access /media?

Here are some system info that might be helpful

myuser@pop-os:~$ inxi --system
System:    Host: pop-os Kernel: 5.4.0-7634-generic x86_64 bits: 64 Desktop: Gnome 3.36.4 
           Distro: Pop!_OS 20.04 LTS 
myuser@pop-os:~$ flatpak --version
Flatpak 1.6.5

Best Answer

TL;DR

If the app is installed "user-wide" add the --user option to the override command:

flatpak override --user --filesystem=/media com.github.alainm23.byte

Detailed

So, after thinking a lot, and reading a github issue about flatpak desktop entries (at this point I was thinking to workaround by manually tweaking it), I understood what was happening.

Basically, flatpak install installs apps "system-wide" by default. However I installed it from the Pop!_Shop (distro store). I realized the app wasn't in the system folder (/var/lib/flatpak/app), but it was in the user folder (~/.local/share/flatpak/app). Because of that I can conclude that Pop!_Shop installed the app "user-wide".

When I ran flatpak override --filesystem=/media com.github.alainm23.byte back then, it would complain and I just prepended sudo to it and it worked. At that time I didn't realize doing that would just set the override to a "system-wide installation" and the app was installed "user-wide".

So, adding the option --user to the override command solves the issue, and it doesn't require sudo.

flatpak override --user --filesystem=/media com.github.alainm23.byte

Now the app has access to the aforementioned file system when launched from the system menu and even from shell without specifying the permission (flatpak run com.github.alainm23.byte).

Related Question