Skip to content

XDG_DATA_DIRS reinitalized to an incomplete value by zshenv #33219

Closed

Description

@tiramiseb

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

  1. make sure Gnome is installed (services.xserver.desktopManager.gnome3.enable = true in /etc/nixos/configuration.nix)
  2. open a terminal, default shell is Bash, execute echo $XDG_DATA_DIRS to check than there are many paths in this variable
  3. add the following parameters to /etc/nixos/configuration.nix to install zsh as the default shell: programs.zsh.enable = true; users.defaultUserShell = "/run/current-system/sw/bin/zsh";
  4. reboot
  5. 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

  1. added
    0.kind: bug
    6.topic: nixos
    on Dec 30, 2017
  2. tiramiseb commented on Dec 31, 2017

    @tiramiseb
    ContributorAuthor

    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.

  3. jtojnar commented on Jan 2, 2018

    @jtojnar
    Member

    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.

  4. rycee commented on Jan 21, 2018

    @rycee
    Member

    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 the

    . ${config.system.build.setEnvironment}

    line in nixos/modules/programs/zsh/zsh.nix a bit further down to sit above the ${cfge.loginShellInit} line.

  5. jtojnar commented on Jan 21, 2018

    @jtojnar
    Member

    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.

  6. rycee commented on Jan 21, 2018

    @rycee
    Member

    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

  7. rycee commented on Jan 21, 2018

    @rycee
    Member

    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.

  8. tiramiseb commented on Jan 21, 2018

    @tiramiseb
    ContributorAuthor

    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
    
  9. tiramiseb commented on Jan 21, 2018

    @tiramiseb
    ContributorAuthor

    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
    
  10. rycee commented on Jan 21, 2018

    @rycee
    Member

    @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.

  11. oxij commented on Jan 21, 2018

    @oxij
    Member
  12. tiramiseb commented on Jan 21, 2018

    @tiramiseb
    ContributorAuthor

    rycee: you're right, __ETC_PROFILE_DONE=1.

  13. lukateras commented on Jan 22, 2018

    @lukateras
    Member

    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.

    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.

  14. oxij commented on Jan 22, 2018

    @oxij
    Member
  15. 8 remaining items

  16. hedning commented on Aug 5, 2018

    @hedning
    Contributor

    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.

  17. hedning commented on Aug 6, 2018

    @hedning
    Contributor

    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 trick

    modified   nixos/modules/config/shells-environment.nix
    @@ -164,12 +164,16 @@ in
     
         system.build.setEnvironment = pkgs.writeText "set-environment"
            ''
    -         ${exportedEnvVars}
    +         if [ -z "$__NIX_SET_ENVIRONMENT_SOURCED" ]; then
    +           export $__NIX_SET_ENVIRONMENT_SOURCED=1
     
    -         ${cfg.extraInit}
    +           ${exportedEnvVars}
     
    -         # ~/bin if it exists overrides other bin directories.
    -         export PATH="$HOME/bin:$PATH"
    +           ${cfg.extraInit}
    +
    +           # ~/bin if it exists overrides other bin directories.
    +           export PATH="$HOME/bin:$PATH"
    +         fi
            '';
     
         system.activationScripts.binsh = stringAfter [ "stdio" ]
  18. rycee commented on Aug 6, 2018

    @rycee
    Member

    @hedning Would this work for both the cases outlined in #33219 (comment)? I suspect it wouldn't work for the second use-case.

  19. hedning commented on Aug 6, 2018

    @hedning
    Contributor

    @rycee Hmm, yeah you're right, the export would break the second case. Without the export it won't serve its purpose at all, as a new shell won't pick it up.

  20. hedning commented on Aug 7, 2018

    @hedning
    Contributor

    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_DONE guard, since each shell might need some specific setup.

    So building on the idea behind __ETC_PROFILE_DONE we create __NIXOS_SET_ENVIRONMENT_DONE which only guards against sourcing system.build.setEnvironment. For instance in /etc/bashrc we first check __NIXOS_SET_ENVIRONMENT_DONE sourcing the common environment if it's not set. We then check for __ETC_PROFILE_DONE sourcing the shell specific profile if that's not done.

    I'm not sure if it's actually wanted behavior to source effectively the whole/etc/profile from /etc/bashrc, I suspect the purpose was to source the basic environment and shellInit, not loginShellInit, so we should probably break that out.

    Login shells will not check __NIX_SET_ENVIRONMENT_DONE, but rather source setEnvironment unconditionally.

    One ZSH specific problem with this approach is that variables set in shellInit might be clobbered by new login shells due to /etc/zprofile being sourced after /etc/zshenv. Not quite sure how to fix that.

  21. oxij commented on Aug 30, 2018

    @oxij
    Member

    Follow up: #45784.

  22. xeji commented on Sep 6, 2018

    @xeji
    Contributor

    fixed in #45784

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: bug6.topic: nixos

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions