Set environment variable for applications on startup How do you set an environment variable on startup so that it can be used in applications?

For example, ATOM_HOME is used by Atom.
Solution

The solution uses two files: environment.plist and environment.conf.

environment.plist should be placed in ~/Library/LaunchAgents for a per user solution (recommended), or in /Library/LaunchAgents for a global solution (not recommended – potential security loophole). 

environment.conf can be placed almost anywhere. $PATH_TO_ENVIRONMENT_CONF can be relative, but only to the 'default' environment variables, or any that are defined in a EnvironmentVariables key. 1

environment.plist:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
  &lt;key&gt;Label&lt;/key&gt;
  &lt;string&gt;local.launchd.environment&lt;/string&gt;
  &lt;key&gt;ProgramArguments&lt;/key&gt;
  &lt;array&gt;
    &lt;string&gt;sh&lt;/string&gt;
    &lt;string&gt;-c&lt;/string&gt;
    &lt;string&gt;xargs -L 1 launchctl &amp;lt; $PATH_TO_ENVIRONMENT_CONF&lt;/string&gt;
  &lt;/array&gt;
  &lt;key&gt;RunAtLoad&lt;/key&gt;
  &lt;true/&gt;
&lt;/dict&gt;
&lt;/plist&gt;


environment.conf:

setenv ATOM_HOME $HOME/.config/atom
setenv SOME_VAR "Use quotes if spaces are necessary"


How it works

When a user logs in, the LaunchAgents in ~/Library/LaunchAgents are executed. In this case, sh -c xargs -L 1 launchctl &lt; $PATH_TO_ENVIRONMENT_CONF is executed. launchctl manages daemons and agents. In this case, we are using it to set an environment variable accessible to all applications and the shell. 

xargs -L 1 launchctl &amp;lt; $PATH_TO_ENVIRONMENT_CONF

xargs                     # xargs converts stdin to command line arguments
xargs -L 1                # Tells xargs to invoke launchctl for each line
           launchctl      # Run launchctl with the arguments
                     &amp;lt; # Since a plist is an xml document, angle brackets must be escaped. 

                     &amp;lt; $PATH_TO_ENVIRONMENT_CONF
                          # This tells xargs to read input from the file at $PATH_TO_ENVIRONMENT_CONF


This solution can also be extended to other launchctl subcommands.