Accessing home directory with home interface


#1

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.


#2

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.


#3

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 …


Classic confinement for epitopepredict snap
#4

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


#5

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.