Set environment variable for applications on startup
-
13-04-2021 - |
Pregunta
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.
Solución
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
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>local.launchd.environment</string>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>-c</string>
<string>xargs -L 1 launchctl < $PATH_TO_ENVIRONMENT_CONF</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
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 < $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 < $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
< # Since a plist is an xml document, angle brackets must be escaped.
< $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.