What’s the right way to add directories to XDG_DATA_DIRS?

I’ve installed some app under /opt/myapp, which has a /opt/myapp/share directory. As I finish installing, it tells me:

Note that '/opt/myapp/share' is not in the search path
set by the XDG_DATA_HOME and XDG_DATA_DIRS
environment variables, so applications may not
be able to find it until you set them. The
directories currently searched are:

- /usr/share/gnome
- /home/joeuser/.local/share/flatpak/exports/share
- /var/lib/flatpak/exports/share
- /usr/local/share
- /usr/share

What’s the right way to add directories to that list – system-wide and as a single user?

Answer

The German ubuntuusers wiki has a nice list of files and directories that can be used for that purpose.

Setting it globally

From my research, appending to that environment variable globally is not trivial, but here are some pointers:

  • If you want to overide the existing value, /etc/environment is the easiest way
  • Depending on the system configuration /etc/profile might also be a good way, because you it is executed by the shell
  • Other files to try might be /etc/X11/Xsession.d/* and /etc/security/pam_env.conf

Setting it per-user

  • $HOME/.profile (or $HOME/.zprofile for zsh users) is suggested in multiple places, however adding the line XDG_DATA_DIRS="$HOME/.local/xdg:$XDG_DATA_DIRS" in there rendered my desktop completely non-functional upon login
  • The way that worked for me was to create $HOME/.xsessionrc and put the line export XDG_DATA_DIRS="$HOME/.local/xdg:$XDG_DATA_DIRS" in there. Of course, you have to replace $HOME/.local/xdg by the directory you want to add. Please also note that this will only set the variable for graphical applications, not for the shell (so your value won’t be mentioned in echo $XDG_DATA_DIRS), but that should not be a problem.

Recommendation

Just execute this line and log in again, and it should work:

echo export 'XDG_DATA_DIRS="/opt/myapp/share:$XDG_DATA_DIRS"' >> ~/.xsessionrc

If for whatever reason your system is nonfunctional after that, enter Recovery Mode, go into the root shell and type rm /home/<username>/.xsessionrc and then reboot to get back into your system.

Attribution
Source : Link , Question Author : einpoklum , Answer Author : xeruf

Leave a Comment