Accessing home directory with home interface

Is there a canonical way for programs that use the “home” interface to refer to the actual home directory? Standard methods like Qt’s QDir::homePath() merely return $HOME which is always set to $SNAP_USER_DATA. Consequently, applications that plug into home in order to access the user’s files will typically initialize their file dialogs at $SNAP_USER_DATA, from which the user must awkwardly go up several levels in order to access their documents. Keepassxc does this for example. Users should not need to know the relation between $SNAP_USER_DATA and their home directory in order to access their files.

The desktop-launch launcher provided by ubuntu/snapcraft-desktop-helpers project should link all XDG standard user directories (Documents, Music, Pictures…etc.) to the user’s real ones so the snapped application can easily access these files.

You definitely still have $USER set in the environment so using /home/$USER might work … there are other ways like using getpwnam() to get the pw_dir from /etc/passwd (that is likely not confined).

Since this question doesnt come up for the first time, i wonder if we should not have something like “$ORIG_HOME” or some such in the environment exported by snapd. That would make it easier to handle and also cover exotic locations for homedirs (/var/home/$USER comes to mind) that would only live in the passwd file but not be known inside the snap environment. Filing a whishlist bug against snapd might make sense here …

I still face this issue now. I am not sure is there any recommended to solve this?

I am using Rust’s dirs::home_dir() function and it returns /home/<user>/snap/<package> path instead of /home/<user>. This is happening only when I install install via snap.

I did some digging and seems like dirs::home_dir() function depends on $HOME.

As @Lin-Buo-Ren mentioned, snapcraft-dektop-helpers handles the Documents, Videos, etc directories.
but not HOME directory.

What’s the recommended way do solve this issue?
Should I add additional conditions or use this method?

SNAP VERSION
snap 2.39.3
snapd 2.39.3
series 16
ubuntu 18.04
kernel 4.15.0-55-generic

You are free to create a wrapper script for your command and reset HOME. You can determine the user’s home directory via shell getent passwd $(id -u) | cut -d ':' -f 6 or use the getpwent() equivalent in your language of choice.

I have experimented the same “problem” in my application. I have solved it by “re-written” the environment variable HOME of my snap application to point to the “real home”. My app is working as expected but I do not know if this may have some negative repercussion later on. According to the documentation, each snap application “re-writes” the HOME variable that each snap appears to have a dedicated home directory that is a subdirectory of the real home directory. Which is the main goal of this? Is there any problem to prevent it by forcing the HOME variable in the snapcraft.yaml file?

Thank you in advance

My .yaml:

apps:
myApp:
environment:
HOME: /home/$USER

Environment variables doc: