Setting an environment variable for all your apps

In the New World Order, Linux apps should all store their user-specific data according to the FreeDesktop Base Directory specification, which in practice means that config details for myapp end up in $HOME/.config/myapp. All well and good. However, I don’t like having configuration stored in dotfiles; I like to be able to get at it more easily, so I want it in $HOME/Settings. The XDG spec provides for this: you set an environment variable XDG_CONFIG_HOME (which defaults to $HOME/.config) and then everything uses it. Great! But…where do I set this variable so that all the apps get it?

Some suggestions:

  • $HOME/.bashrc, $HOME/.bash_profile, $HOME/.profile — as far as I can tell, these aren’t run as part of the login process, so they’re no good. They get run when you start bash, which means when you first fire up a terminal.
  • $HOME/.gnomerc — gets run by gdm. Might be a Debianism, and doesn’t work if I change away from gdm a few months from now
  • $HOME/.xinitrc, $HOME/.xsession — get run if you’re in X but not if you’re running over SSH, and .xsession is a Debianism
  • /etc/xdg/user-dirs.conf — this will change it for all users on the machine, not just me
  • Something in PAM. Perhaps. It’s not clear what, though.
  • A file of my choice, which I then source from all of the above places. This is doable but seems stupid to me, and I’m bound to miss something.
  • Something else. This is where you come in; where am I meant to set the environment so that everything gets access to it?

Answers on a postcard…

17 Responses to “Setting an environment variable for all your apps”

  1. whatever

    Frank O'De
  2. Whoops - hit the button too quick there.

    Why don’t you just make a symbolic link - ln -s $HOME/.config $HOME/Settings

    Frank O'Dwyer
  3. Frank: that’s what I currently am doing. However, I ought to be able to set it correctly rather than working around the problem :)

    sil
  4. There ~/.xprofile, which is read by gdm.

    pclouds
  5. /etc/profile is the usual UNIX way to do this kind of thing - think it works on Linux too. It usually contains system wide defaults and (I think) should work regardless of whether you use X or ssh, as long as you’re not using some weird shell that ignores it. Worth a try anyhow.

    Frank O'Dwyer
  6. Frank: /etc/profile will set it for all users on the system, not just me; same problem as /etc/xdg.

    pclouds: I didn’t know about that, but it has the same problem as .xsession; it won’t work if I ssh in (or possibly if I switch to a different login manager).

    sil
  7. Well you could put some logic in there, e.g. within /etc/profile test for the existence of a file such as $HOME/.xdg_profile and include it if it exists.

    Or test if $HOME/Settings exists and set the variable if it does.

    Otherwise you’re possibly stuck with doing it in two places, once for ssh and once for X.

    Still think a symlink is the easiest way though :-)

    Frank O'Dwyer
  8. Put it in .config/FreeDesktop, of course! ;)

    Roberto
  9. Roberto: you’re all comedy all the time, you are :-)

    sil
  10. I think .bashrc is loaded whenever bash is started.. And the gdm startup is a bash script (or maybe you use a crappy distro that uses another shell for gdm? Maybe this shell has an equivalent file?) On a unix system, I’ll bet there’s a shell somewhere in starting mostly anything.

    Tester
  11. Tester: .bashrc isn't sourced as part of startup. I know this because I wrote
    
    echo "I was called" >> $HOME/bash-startup
    
    in .bashrc and reboted the machine; no bash-startup file was created.
    sil
  12. You will find that ~/.profile is parsed when you login both via gdm (with almost all sessions managers too) and when you ssh in.

    Go ahead and try it:
    echo ”.profile was called” >> $HOME/bash-startup

    karl
  13. I can confirm that ~/.profile is called when you log in - I use it to load the colour profile for my monitor, and so just after I log in first the screen does a noticeable colour shift.

    Michael Maclean
  14. Michael Maclean: aha. Yes. It does get run. *After* your autostart applications are autostarted. Useful, I don’t think. :)

    sil
  15. Well it doesn’t sound like there is any single clean way of accomplishing it at the mo then.

    You can get anything run before starting your session using ~/.dmrc ~/.xsession and ~/.profile.

    in ~/.dmrc
    [Desktop]
    Session=default

    in ~/.xsession (assuming your still using gnome)
    export XDG_CONFIG_HOME=$HOME/Settings
    gnome-session

    in ~/.profile add your XDG_CONFIG_HOME line to the end (or beginning) too. .profile wont be read till after things have autostarted, but that dont matter as the var is already exported in ~/.xsession. It is only there for when you open terminals/bash/dash.

    karl
  16. Oh, I didn’t realise it was after the autostart apps. But then, I don’t have any :)

    Michael Maclean
  17. CDE sources your profile at login if you set DTSOURCEPROFILE=true in ~/.dtprofile. It sounds like this is the behaviour you’re after.

    You could get this by creating a .xsession that starts
    #!/bin/bash –login

    Peter Oliver

Leave a Reply

OpenID is a decentralised authentication system. If you use LiveJournal or Vox you already have an OpenID; just use the URL of your homepage there. See also how to get yourself an OpenID.