How do I set an environmental variable for a GNOME session?

15

4

On my system, for apps that use SDL, I need to set an environmental variable:

export SDL_AUDIODRIVER=alsa

to get the sound to work properly. How do I set up my GNOME session so that this variable is present for all applications (so if I run an application through Nautilus, or use DBGL, the apps work properly)?

EDIT:

Configuration files I have set (and reset my machine after each) to no avail:

  • .xsession
  • .xsessionrc
  • .gnomerc
  • .bash_profile
  • .profile<-- This works in the general case, but not when your distro is setting the variable elsewhere.
  • /etc/profile

Bernard

Posted 2009-08-06T14:14:26.353

Reputation: 575

Are you sure this needs to be an environment variable? Seems like it might be some other kind of setting. Have you dug around in the system sound settings applet? – codeLes – 2009-08-06T15:29:36.697

It does indeed. This is how SDL figures out what backend to use. I wish it just used a configuration file, but SDL is fairly consistent in its use of environmental variables to dictate behaviour. – Bernard – 2009-08-06T15:31:15.850

have you seen this: http://modarchive.org/forums/index.php?topic=1879.0

– codeLes – 2009-08-06T15:35:45.200

1Well yeah, it's a choice between recompiling SDL or setting an environmental variable. I think I'm making the right choice. :p This works (i.e., in a shell, it works) it's just the thorny issue of getting GNOME to propagate the variable for apps not started from a shell that's the issue. Thank you for your continuing help. – Bernard – 2009-08-06T15:45:12.670

What distro is this in?! (I agree that the config for gdm is really hard to follow, so my first thought, "just look in /etc/gdm/ and read the scripts" is a lot of pain.) Here, .profile seems to work just fine. (debian/testing) – Anders Eurenius – 2009-08-07T00:10:28.790

Also, are you sure that it's not really there (and SDL is ignoring it)? Can you echo it in an xterm? (set, then echo again?) – Anders Eurenius – 2009-08-07T00:11:50.847

It's really not there. Run emacs in an xterm, it echos 'alsa' for M-x getenv. Run GTK+ version of emacs, it echos 'pulse'. I'm guessing the SUSE guys (OpenSUSE 11.1) are setting it somewhere else. – Bernard – 2009-08-07T02:34:44.743

Answers

6

It could be that the SDL_AUDIODRIVER variable is being unset somewhere. One strategy I use for problems with things related to startup scripts is to put in debug echo statements that is simple to enable/disable by creating/deleting a file. You could start by adding

debug_msg() {
        test -f $HOME/.debug && echo "$@" 1>&2
}

debug_msg "running ~/.bashrc ($0), SDL_AUDIODRIVER = '$SDL_AUDIODRIVER'"

to .bashrc, .bash_profile, .profile and /etc/profile to see what value it has and if/where it is changing.

hlovdal

Posted 2009-08-06T14:14:26.353

Reputation: 2 760

Mmm, that's my current thoughts as well. It's set to 'pulse', and that must be being set somewhere by OpenSUSE. I'll do some detective work when I get home. – Bernard – 2009-08-14T03:58:01.433

6

https://help.ubuntu.com/community/EnvironmentVariables talks about four places where we can set the environment variables:

  • ~/.pam_environment - […] It is not a script file, but rather consists of assignment expressions, one per line. […] requires a re-login in order to initialize the variables. […]
  • ~/.profile - This is probably the best file for placing environment variable assignments, since it gets executed automatically by the DisplayManager during the start-up process desktop session as well as by the login shell when one logs-in from the textual console.
  • ~/.bash_profile or ~/.bash_login - If one of these file exist, bash executes it rather then "~/.profile" when it is started as a login shell. (Bash will prefer "~/.bash_profile" to "~/.bash_login"). However, these files won't influence a graphical session by default.
  • ~/.bashrc - Because of the way Ubuntu currently sets up the various script files by default, this may be the easiest place to set variables in. The default configuration nearly guarantees that this file will be executed in each and every invocation of bash as well as while logging in to the graphical environment. However, performance-wise this may not be the best thing to do since it will cause values to be unnecessarily set many times.

http://userbase.kde.org/Session_Environment_Variables/en suggests this place for KDE:

KDE will execute any script it finds in $HOME/.kde/env whose filename ends in .sh, and it will maintain all the environment variables set by them. It is important that any variable you want to set must be also exported.

https://help.ubuntu.com/community/ComposeKey#XIM suggests modifying ~/.gnomerc or ~/.Xsession.

Denilson Sá Maia

Posted 2009-08-06T14:14:26.353

Reputation: 9 603

1

Create a .gnomerc file in your $HOME which is a shell script (like .bashrc) which sets all the variables you want.

Mike McQuaid

Posted 2009-08-06T14:14:26.353

Reputation: 3 639

This isn't working for me, unfortunately. – Bernard – 2009-08-06T14:53:46.707

Have you tried opening gnome-terminal and confirmed it doesn't show up in env? – Mike McQuaid – 2009-08-06T15:24:59.543

1@Mike: it's in my .bashrc so it would show up, but it would be inaccurate. Emacs and M-x getenv confirms that it is not being overridden for GUI apps. – Bernard – 2009-08-06T15:27:08.650

0

Have you tried creating the environment variable int .profile or /etc/profile?

That should work and should already exist. Unlike .bashrc which is per terminal session, these are initiated at login to your session.

codeLes

Posted 2009-08-06T14:14:26.353

Reputation: 1 772

Alas, no effect. – Bernard – 2009-08-06T15:05:53.810