132

Is there a way to set the enviroment variable in .desktop file? I'm trying to run application (eclipse) with custom gtk style, so basically I want to get the following result by runing a .desktop file:

GTK2_RC_FILES=gtkrc.custom /path/to/eclipse

I've tryied to put it in a bash script and run it from the .desktop file, but then it does not integrate well with the Unity launcher.

dotintegral
  • 1,323

3 Answers3

193

You can add an environment variable to an application by editing its .desktop file. For example, to run "digiKam" with the environment variable APPMENU_DISPLAY_BOTH=1, find the corresponding digikam.desktop file and add the setting of the variable, via the env command, to the entry "Exec":

Exec=env APPMENU_DISPLAY_BOTH=1 digikam -caption "%c" %i

In your case:

Exec=env GTK2_RC_FILES=gtkrc.custom /path/to/eclipse
jasmines
  • 11,011
  • 2
    how can I set env variable containing user's $HOME?. Neither Exec=env MYVAR="$HOME/foo" nor Exec=env MYVAR="~/foo" gets expanded. Instead, they are passed literally. – Martin Vegter Dec 31 '16 at 09:25
  • 1
    it seems you can do it as described in https://stackoverflow.com/a/8980518/1446479 – peedee Dec 13 '17 at 13:58
  • for other looking for a slight variation on this, you can also unset env vars with the -u flag, like env -u DONT_WANT_THIS_SET some-command – Tom Saleeba Jun 07 '23 at 04:42
13

An alternative to modify the .desktop file is to put a wrapper script in e.g. ~/bin.

$ cat ~/bin/eclipse
#!/bin/sh
export GTK2_RC_FILES=gtkrc.custom
exec /usr/bin/eclipse "$@"

This way the customization won't be overwritten next time the application package is updated.

Edit:

A hint about why this works can you see by checking out what the PATH variable contains. In my case:

$ echo $PATH
/home/gunnar/bin:/home/gunnar/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

~/bin is the first folder in the list, and thus is looked at before /usr/bin.

Gunnar Hjalmarsson
  • 33,540
  • 3
  • 64
  • 94
  • If you just put your .desktop file in your local settings, it won't get overridden, anyway. – Auspex May 31 '19 at 09:24
  • @Auspex: True. But at the same time, if you use a local copy, you won't benefit from possible updates of the package owned .desktop file. – Gunnar Hjalmarsson May 31 '19 at 10:27
  • 1
    I'm not seeing how putting a wrapper in ~/bin is going to help with that. The package-owned .desktop file is never going to see that wrapper! You'd need to also use update-alternatives – Auspex Jun 11 '19 at 13:11
  • @Auspex: The .desktop file does not need to "see" the wrapper. It just executes eclipse, and due to PATH the wrapper is picked instead of /usr/bin/eclipse. I edited the answer to clarify. – Gunnar Hjalmarsson Jun 11 '19 at 16:06
  • Yes, the .desktop file does need to see the wrapper. I was thinking that the average .desktop file specifies a full path, but on further inspection I think most of the system ones do just specify a command name—and so will see your wrapper. However, the only Eclipse desktop files I have are in the format specified in the question—giving a full path to the Eclipse binary. – Auspex Jun 12 '19 at 15:42
  • 1
    @Auspex: Well, the one which is provided in the Ubuntu archive (up to 18.10) has Exec=eclipse. But I agree, if .desktop specifies a full path, my answer does not apply. – Gunnar Hjalmarsson Jun 13 '19 at 01:31
0

This seems to be a problem with kmenuedit. It never saves the "Environment Variable" field in the .desktop file. Hope this helps. I opened a bug with KDE. "FOO=my_var_1" never gets saved, and never gets sent to the app.

EMR
  • 11
  • 1