XDG_DATA_DIRS reinitalized to an incomplete value by zshenv
Issue description
When using zsh as the default shell, the XDG_DATA_DIRS environment variable is reinitialized to /run/opengl-driver/share:/etc/per-user-pkgs/sebastien/share:/home/sebastien/.nix-profile/share:/nix/var/nix/profiles/default/share:/run/current-system/sw/share. Normally, it should contain paths to all packages providing a gschema file.
When using bash, this variable contains all those paths.
Steps to reproduce
- make sure Gnome is installed (
services.xserver.desktopManager.gnome3.enable = truein/etc/nixos/configuration.nix) - open a terminal, default shell is Bash, execute
echo $XDG_DATA_DIRSto check than there are many paths in this variable - add the following parameters to
/etc/nixos/configuration.nixto install zsh as the default shell:programs.zsh.enable = true; users.defaultUserShell = "/run/current-system/sw/bin/zsh"; - reboot
- open a terminal, new default shell is Zsh, execute
echo $XDG_DATA_DIRS: the variable does only contain the values mentioned above
Technical details
- system:
"x86_64-linux" - host os:
Linux 4.9.72, NixOS, 17.09.git.7be4cb9 (Hummingbird) - multi-user?:
yes - sandbox:
no - version:
nix-env (Nix) 1.11.16 - channels(root):
"nixos-17.09.2476.53e6d671a96" - nixpkgs:
/nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs
Please note I'm using a clone of nixpkgs with some local commits, that's why host os is tagged with "17.09.git.7be4cb9". However, I also tested this on a virtual machine which uses https://github.com/NixOS/nixpkgs. I'm 100% sure this issue is not caused by local commits.
Source of the problem
I have tried to identify the source of the problem, here are the result of my search, however I do not have a fix to suggest.
Zsh sources /etc/zshenv, which sources /nix/store/XXX-set-environment, which redefines XDG_DATA_DIRS.
When I comment the export XDG_DATA_DIRS=[...] line in /nix/store/XXX-set-environment or when I comment . /nix/store/XXX-set-environment in /etc/zshenv, then this variable is not overriden (thus, it contains the whole list of needed paths).
I do not know if it is safe to not source /nix/store/XXX-set-environment in /etc/zshenv, I didn't take a look to other variables' values. I also do not know if it is safe to remove the export XDG_DATA_DIRS=[...] line from /nix/store/XXX-set-environment.
I've tried to narrow it down a little bit more.
I've experiences the described behaviour when using gdm or lightdm as the display manager, but not with slim. With slim+gnome3+zsh, the XDG_DATA_DIRS environment variable is correctly set.
Thus, using slim as the display manager may be a viable workaround, and the issue may be related to some configuration in slim missing in other display managers packages, or maybe related to systemd-logind, which is not supported by slim.
Indeed, it works with bash. I have no idea where does the values originate but I would consider it a bug, the data dirs should not leak into the environment.
I believe this is the same bug we've encountered in https://github.com/rycee/home-manager/issues/183. The issue I believe – as a non-zsh user who actually doesn't know what he's talking about – is caused by setting the system session variables in /etc/zshenv (which is sourced by each instance) and not /etc/zprofile (which is only sourced by login shells). Thus I think the fix is to move the
. ${config.system.build.setEnvironment}
line in nixos/modules/programs/zsh/zsh.nix a bit further down to sit above the ${cfge.loginShellInit} line.
I think the same issue happens with fish-shell.
I do not feel confident to change the shell modules without knowing why was the environment setting put there in the first place.
Yeah, it would be good with input from somebody with more insight.
But the gist of it from what I can tell is that when an graphical login is started the environment is typically set up through bash, which will source the set-environment script and then this environment will be expanded when things start up. Once inside X one can start an xterm, which will start zsh. Since no zsh has run before, there is no __ETC_ZSHENV_SOURCED variable in the environment and therefore the content of /etc/zshenv will be run, among which is sourcing the set-environment script which will effectively reset the environment.
CC semi-random people from the history of the zsh module: @oxij @ttuegel @yacinehmito
As a simple demonstration. My default shell is bash, I enabled programs.zsh and then I get
$ echo $SHELL
/run/current-system/sw/bin/bash
$ EDITOR=vim zsh -c 'echo $EDITOR'
nano
$ EDITOR=vim bash -c 'echo $EDITOR'
vim
I believe a person having zsh as their default shell will get the output "vim" in both cases.
Could you please telle me more about "when an graphical login is started the environment is typically set up through bash"? I'm not an expert of this area, so I'm not sure I understand what you are saying.
What is sure is that I have no __ETC_*_SOURCES related to Bash in my environment
Regarding the zprofile file, indeed it is not loaded when I start a terminal. However, when I start zsh with the --login option, this file is correctly loaded but there is no impact on the value of XDG_DATA_DIRS.
I confirm your last assertion (I don't understand the relation with the current issue, but if it can help, I'm happy to help):
$ echo $SHELL
/run/current-system/sw/bin/zsh
$ EDITOR=vim zsh -c 'echo $EDITOR'
vim
$ EDITOR=vim bash -c 'echo $EDITOR'
vim
By the way, when I start xterm bash directly from my graphical env (not from an already opened term, so no zsh initialization of any sort), I get a result similar to yours:
$ echo $SHELL
/run/current-system/sw/bin/bash
$ EDITOR=vim zsh -c 'echo $EDITOR'
vi
$ EDITOR=vim bash -c 'echo $EDITOR'
vim
@tiramiseb The xsession script, used by (I think) all display managers, is run by Bash and includes the line . /etc/profile. The /etc/profile file, in turn, includes . ${config.system.build.setEnvironment}. As far as I know this is where the initial environment variables originate for graphical sessions.
If you look for __ETC_PROFILE_DONE in your environment then I think you should find __ETC_PROFILE_DONE=1, if not, well then I'm not certain what's going on 🙂
Looking at the bash module code I think it also is a bit wrong in that the . ${config.system.build.setEnvironment} line is set in shellInit while I believe it should be set in loginShellInit. But both of these options end up in /etc/profile, which is only sourced by login Bash shells so the effect is not as severe as in Zsh.
[The bug ...] is caused by setting the system session variables in
/etc/zshenv(which is sourced by each instance) and not/etc/zprofile(which is only sourced by login shells). Thus I think the fix is to move ...
The problem with such a fix would be that non-interactive non-login uses of zsh (like non-interactive ssh, cron, at, etc) would break.
The crux of the problem, as I see it, is just
When using zsh as the default shell, the XDG_DATA_DIRS environment variable is reinitialized
which comes from re-sourcing set-environment script because we have different __ETC_*_SOURCED variables when all we want to set everything exactly once.
I see three solutions here:
- fight with the order things get sourced (which most comments above suggest a variant of),
- add some more environment variables to control sourcing (and push the problem for later),
- simply fix
set-environmentscript to ignore things that are already set.
Sure, the latter solution changes semantics somewhat, but, I think, its the proper way to go here.
Since set-environment gets generated from a centralized location and env vars there get values merged by lib.types
I don't think we need to introduce anything with append-and-export semantics.
Simply ignoring should be enough. This way would also uncover bugs in services that reset env vars they shouldn't touch (or should merge with nixos environment).
Note that Steam games have a similar problem with overriding LD_LIBRARY_PATH (see #33218, for instance) and I would have liked nixpkgs integration to make wrappers that fix their start scripts instead of having NIX_LD_LIBRARY_PATH hack of #31263.
rycee: you're right, __ETC_PROFILE_DONE=1.
Note that Steam games have a similar problem with overriding
LD_LIBRARY_PATH(see #33218, for instance) and I would have liked nixpkgs integration to make wrappers that fix their start scripts instead of havingNIX_LD_LIBRARY_PATHhack of #31263.
Dynamically wrapping certain executables is very error-prone, impossible in some cases (e.g. executables are checksummed) and also duplicates effort for every app that downloads such executables, that's not only for Steam.
Replied in #31263. Lets keep on topic here, I mentioned #31263 just as an example.
I'm curious if not the bash setup actually has the same problem. Could somebody who use zsh as login shell try the EDITOR=vim bash -c 'echo $EDITOR' command in a console login (i.e., not in an X session)?
@oxij Sorry for the super long delay but could you explain a bit further about what you mean by "the problem with such a fix would be that non-interactive non-login uses of zsh (like non-interactive ssh, cron, at, etc) would break"?
From my understanding I can see how it would break a script that assumes that Zsh will reset some environment variables but I'm inclined to say that such a script is erroneous and that it should make the assumption explicit by setting the variables itself. Otherwise the script is likely to break on a different platform with a different system Zsh setup. But again I'm not experienced with Zsh so I'll try to avoid making too grandiose claims.
could you explain a bit further about what you mean by "the problem with such a fix would be that non-interactive non-login uses of zsh (like non-interactive ssh, cron, at, etc) would break"?
Login shell is not just the first (parent) shell of a session. A shell needs to be invoked in a special way (see the man) to become a login shell. I doubt you ever run any login shells. For instance, tty emulators don't run login shells.
From man zshall (bash is similar):
STARTUP/SHUTDOWN FILES
Commands are first read from /etc/zshenv; this cannot be overridden. ...
Commands are then read from $ZDOTDIR/.zshenv. If the shell is a login shell, commands are read from /etc/zprofile and then $ZDOTDIR/.zprofile. Then, if the shell is interactive, commands are read from /etc/zshrc and then $ZDOTDIR/.zshrc. Finally, if the shell is a login shell, /etc/zlogin and $ZDOTDIR/.zlogin are read.
Most things don't run login shells, which means that most uses of zsh will get an almost empty env (not populated by set-environment).
/etc/zshenv bears such a name because you are supposed to set environment there.
But I still don't understand why it would break non-interactive non-login uses. Presumably in those cases you want to use the given environment, not to override it? For example, consider a script that runs $PAGER when it executes. When I run the script from cron I might prefer to have PAGER=cat but I still want the standard PAGER=less when running the script manually. So I wouldn't want the shell to override my environment much like the XDG_DATA_DIRS case in the original message.
Then again, I still want to create login shells with restored variables when I so desire, like so:
$ export FRESH_VAR=foo PAGER=more ; bash -c 'echo $FRESH_VAR - $PAGER'
foo - more
$ export FRESH_VAR=foo PAGER=more ; bash --login -c 'echo $FRESH_VAR - $PAGER'
foo - less -R
While having set-environment skip existing variables would work with the first case (honoring existing environment for non-login shells) I don't see how it could be made to work with the second (overriding existing environment for login-like shells).
Just merged #40223 which should fix this. Please reopen if it is still an issue after that PR.
Reopening because #40223 was reverted in #40759.
Was really confused for a while there, as I could not figure out where these extra entries was set in the NixOS modules. After grepping for the entries in the nix store it became clear that they're set by the particular terminal wrapper that launched the shell, and is therefor in no way intended. You can see this by running eg. tilix and gnome-terminal, comparing the resulting XDG_DATA_DIRS.