Repository navigation
Description
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.
Activity
tiramiseb commentedon Dec 31, 2017
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.
jtojnar commentedon Jan 2, 2018
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.rycee commentedon Jan 21, 2018
I believe this is the same bug we've encountered in nix-community/home-manager#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 theline in
nixos/modules/programs/zsh/zsh.nixa bit further down to sit above the${cfge.loginShellInit}line.jtojnar commentedon Jan 21, 2018
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.
rycee commentedon Jan 21, 2018
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-environmentscript 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_SOURCEDvariable in the environment and therefore the content of/etc/zshenvwill be run, among which is sourcing theset-environmentscript which will effectively reset the environment.CC semi-random people from the history of the zsh module: @oxij @ttuegel @yacinehmito
rycee commentedon Jan 21, 2018
As a simple demonstration. My default shell is bash, I enabled
programs.zshand then I getI believe a person having zsh as their default shell will get the output "vim" in both cases.
tiramiseb commentedon Jan 21, 2018
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_*_SOURCESrelated to Bash in my environmentRegarding the zprofile file, indeed it is not loaded when I start a terminal. However, when I start zsh with the
--loginoption, 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):
tiramiseb commentedon Jan 21, 2018
By the way, when I start
xterm bashdirectly from my graphical env (not from an already opened term, so no zsh initialization of any sort), I get a result similar to yours:rycee commentedon Jan 21, 2018
@tiramiseb The
xsessionscript, used by (I think) all display managers, is run by Bash and includes the line. /etc/profile. The/etc/profilefile, 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_DONEin 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 inshellInitwhile I believe it should be set inloginShellInit. 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.oxij commentedon Jan 21, 2018
tiramiseb commentedon Jan 21, 2018
rycee: you're right, __ETC_PROFILE_DONE=1.
lukateras commentedon Jan 22, 2018
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.
oxij commentedon Jan 22, 2018
8 remaining items
hedning commentedon Aug 5, 2018
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.hedning commentedon Aug 6, 2018
Edit: this won't work :(
I'm guessing we should use a commonly named guard for the stuff all the shells share, ie.
system.build.setEnvironment. So something like this should probably do the trickrycee commentedon Aug 6, 2018
@hedning Would this work for both the cases outlined in #33219 (comment)? I suspect it wouldn't work for the second use-case.
hedning commentedon Aug 6, 2018
@rycee Hmm, yeah you're right, the
exportwould break the second case. Without the export it won't serve its purpose at all, as a new shell won't pick it up.hedning commentedon Aug 7, 2018
OK, took a second look at this, and I think I've come up with something of a solution, though perhaps a little bit ugly: hedning@07fe4f3
We need a shell agnostic, and therefor, exported guard to prevent environment clobbering between different shells. But we can't simply share eg. bash's
__ETC_PROFILE_DONEguard, since each shell might need some specific setup.So building on the idea behind
__ETC_PROFILE_DONEwe create__NIXOS_SET_ENVIRONMENT_DONEwhich only guards against sourcingsystem.build.setEnvironment. For instance in/etc/bashrcwe first check__NIXOS_SET_ENVIRONMENT_DONEsourcing the common environment if it's not set. We then check for__ETC_PROFILE_DONEsourcing the shell specificprofileif that's not done.I'm not sure if it's actually wanted behavior to source effectively the whole
/etc/profilefrom/etc/bashrc, I suspect the purpose was to source the basic environment andshellInit, notloginShellInit, so we should probably break that out.Login shells will not check
__NIX_SET_ENVIRONMENT_DONE, but rather sourcesetEnvironmentunconditionally.One ZSH specific problem with this approach is that variables set in
shellInitmight be clobbered by new login shells due to/etc/zprofilebeing sourced after/etc/zshenv. Not quite sure how to fix that.oxij commentedon Aug 30, 2018
Follow up: #45784.
xeji commentedon Sep 6, 2018
fixed in #45784
zsh: move sessionVariables from .zshrc to .zshenv
zsh: move sessionVariables from .zshrc to .zshenv
zsh: move sessionVariables from .zshrc to .zshenv
zsh: move sessionVariables from .zshrc to .zshenv
zsh: move sessionVariables from .zshrc to .zshenv (#2708)
zsh: move sessionVariables from .zshrc to .zshenv
zsh: move sessionVariables from .zshrc to .zshenv (nix-community#2708)