[OS X Emacs] Re: Environment variables and Aquamacs 2.0

Norman Gray norman at astro.gla.ac.uk
Thu May 27 05:37:36 EDT 2010


Jan, hello.

On 2010 May 27, at 00:11, Jan Dubois wrote:

> On Wed, 26 May 2010, Norman Gray wrote:
>> On 2010 May 26, at 22:01, Jan Dubois wrote:
>> 
>>> There is actually at least one more scenario: Aquamacs being started
>>> from Spotlight. In which case you get neither environment.plist nor
>>> shell environment variables because the process starts out as a
>>> child of Spotlight, which is not running under the current user.
>> 
>> 
>> I don't believe this would be a separate scenario.
> 
> No need for "believe", you can actually try it.

I did try it, and a Terminal started from Spotlight inherits launchd's environment, and launchd's initial environment is (usually? always?) governed by environment.plist.

For example, try

% launchctl setenv LAUNCHD_ENVIRONMENT_TEST foo

Then quit Terminal, restart it using Spotlight, and 

% echo $LAUNCHD_ENVIRONMENT_TEST
foo
%

I've done this with shell startup files (ie, ~/.zshrc and /etc/zshenv) moved aside, to rule out any shell-based cleverness as much as possible, and it works the same with Aquamacs (for example) started from Spotlight (though this thread suggests these two things may not be as independent as I'd rather prefer).

What I can't really pin down is the relationship between launchd and loginwindow.  Note [1] says that "For each of [the keys in environment.plist], loginwindow registers an environment variable of the same name and assigns it the value of the key."  I think, but can't find explicit confirmation, that it does so by setting the variable in the per-user /sbin/launchd instance, using a mechanism analogous to the launchctl call above.

Note [2] looks like it should be helpful here, but isn't.  See 'ps axj' for the process tree; note that loginwindow and the per-user launchd are not children of each other.


[1] http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPRuntimeConfig/Articles/EnvironmentVars.html
[2] http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/CustomLogin.html

> For me applications
> started via Spotlight don't get the settings from environment.plist,
> but application started via the Finder do.

Do they get the settings reported by 'launchctl getenv'?  If they don't, then I am confused.  If they do, then there appears to be a dislocation, on your system, between environment.plist and launchd.

>> Spotlight (and other application launchers) will launch applications
>> by getting launchd to launch them, which is the same as the Finder
>> does. They therefore inherit launchd's environment, which is the
>> environment which is set by environment.plist. (I may be wrong in a
>> couple of fine details here, but that's the basic notion, which is
>> unfortunately not documented as obviously as one would like).
> 
> I don't think environment.plist is a launchd thingie; it is used
> by WindowServer/Finder only.  But whatever the details, the *fact*
> is that at least on *some* systems GUI processes started via Spotlight
> get a different set of environment variables than processes started
> via Finder.

Then the question is: which systems are working correctly?  I suspect that getting different environments in the two cases might be anomalous (and it would be enlightening to be proved wrong).

To drift back towards the original topic of this thread, there still seems to be only two ways in which Aquamacs' initial set of environment variables is controlled: from launchd's environment (which at least generally appears to be linked to environment.plist) and a Terminal shell's environment, which David has a way of capturing (there's possibly also the /etc/paths route, which I still think is a sub-optimal recommendation).

Another possibility would be to do

% env| <some trimming required!> | sed 's/\(.*\)=\(.*\)/launchctl setenv \1 \2/' | sh

which would export the shell's environment to launchd, and thus to launched applications such as Aquamacs.  But I imagine this is what Aquamacs is doing (in at least some circumstances) at present.

Hmm: there's a side-thought that rather ran away with itself....

Best wishes,

Norman


-- 
Norman Gray  :  http://nxg.me.uk




More information about the MacOSX-Emacs mailing list