Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos/shells: Avoid overriding the environment for other child shells #45784

Merged
merged 2 commits into from Sep 6, 2018

Conversation

oxij
Copy link
Member

@oxij oxij commented Aug 30, 2018

Motivation for this change

An edited version of #44720.

Things done
  • It boots.
  • bash and zsh in tty, pty and over ssh work.
  • Checked fish.

/cc @LnL7 @pxc @rnhmjoj from git-blame

hedning and others added 2 commits August 30, 2018 13:20
A shared exported guard `__NIXOS_SET_ENVIRONMENT_DONE` is introduced that can
be used to prevent child shells from sourcing `system.build.setEnvironment`
the second time.

This fixes e.g. `nix run derivation` when run from e.g. ZSH through the console or
ssh. Before this Bash would resource the common environment resetting the `PATH`
environment variable.

We also export `system.build.setEnvironment` to `/etc/set-environment` making it
easy to reset the common environment with `. /etc/set-environment` when
needed and to grep for environment variables in `/etc` (which was the
motivation of NixOS#30418).

This reverts changes made in b00a3fc
(the original NixOS#30418).
to comply with `doc/coding-conventions.xml`
@Mic92
Copy link
Member

Mic92 commented Aug 30, 2018

What if someone wants to . /etc/profile after upgrading?

@@ -162,15 +162,24 @@ in
/bin/sh
'';

# For resetting environment with `. /etc/set-environment` when needed
# and discoverability (see motivation of #30418).
environment.etc."set-environment".source = config.system.build.setEnvironment;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. This should be documented for sure!

@oxij
Copy link
Member Author

oxij commented Aug 30, 2018

What if someone wants to . /etc/profile after upgrading?

Everything would stay as it was before this PR as it sets __ETC_PROFILE_SOURCED=1.

@oxij
Copy link
Member Author

oxij commented Sep 4, 2018 via email

@hedning
Copy link
Contributor

hedning commented Sep 5, 2018

This does fix a rather unfortunate bug with nix run, where it will reset PATH if the parent shell isn't bash (or in a typical nixos graphical session which source /etc/profile).

@rnhmjoj
Copy link
Contributor

rnhmjoj commented Sep 5, 2018

@oxij I'll test this with fish soon. If I understand correctly this should also fix #18946.

@oxij
Copy link
Member Author

oxij commented Sep 5, 2018 via email

@rnhmjoj
Copy link
Contributor

rnhmjoj commented Sep 5, 2018

Everything seems to be working correctly, no problem so far.

@oxij
Copy link
Member Author

oxij commented Sep 5, 2018

If fish works, then LGTM.

Can somebody merge this? Please also backport to 18.09.

This fixes #18946, #33219, #46091 and unblocks #44497.

@FRidh FRidh added this to the 18.09 milestone Sep 6, 2018
@oxij
Copy link
Member Author

oxij commented Sep 6, 2018 via email

Copy link
Contributor

@xeji xeji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@xeji xeji merged commit 5fc8ebd into NixOS:master Sep 6, 2018
@xeji
Copy link
Contributor

xeji commented Sep 6, 2018

backported in e981546..23da995

@oxij
Copy link
Member Author

oxij commented Sep 6, 2018

@xeji Thanks! Can I also ask you to do the honors of closing #18946, #33219, and #46091?

@xeji
Copy link
Contributor

xeji commented Sep 6, 2018

done

@oxij
Copy link
Member Author

oxij commented Sep 6, 2018

@xeji Huge thanks!

Thanks to @hedning for the persistence on the issue and to everyone else here for testing.

Cheers!

hedning added a commit to hedning/nixpkgs that referenced this pull request Oct 16, 2018
When GDM launches a new session it will inherit the user's systemd
environment (but only unset variables). If `__NIXOS_SET_ENVIRONMENT_DONE` is set
in the user's systemd environment it will prevent the environment to be set
properly or updated (eg. after having done a system rebuild).

Gnome sessions exports their environment to systemd at startup. If something
is keeping the user's systemd process alive (eg. ssh) launching a new gnome
session after logging out will result in a broken PATH. Specifically the PATH
will be inherited from GDM and never reset.

We patch GDM to never inherit `__NIXOS_SET_ENVIRONMENT_DONE` so new sessions
will always reset their base environment.

fixes NixOS#48255

For more info about the environment setup:
NixOS#45784
@oxij oxij deleted the pull/44720-shell-env-edited branch November 18, 2018 08:57
jian-lin added a commit to linj-fork/home-manager that referenced this pull request Feb 8, 2022
This patch moves both home.sessionVariables and
programs.zsh.sessionVariables from .zshrc to .zshenv. Additionally,
these two kinds of session variables will not be sourced more than
once to allow user-customized ones to take effect.

Before, session variables are in .zshrc, which causes non-interactive
shells to not be able to get those variables. For example, running a
command through SSH is in a non-interactive and non-login shell, which
suffers from this. With this patch, all kinds of shells can get
session variables.

The reason why these session variables are not moved to .zprofile is
that programs started by systemd user instances are not able to get
variables defined in that file. For example, GNOME
Terminal (gnome-terminal-server.service) is one of these programs and
doesn't get variables defined in .zprofile. As a result, the shells it
starts, which are interactive and non-login, do not get those
variables.

Fixes nix-community#2445

Related NixOS/nixpkgs#33219
Related NixOS/nixpkgs#45784
jian-lin added a commit to linj-fork/home-manager that referenced this pull request Feb 8, 2022
This patch moves both home.sessionVariables and
programs.zsh.sessionVariables from .zshrc to .zshenv. Additionally,
these two kinds of session variables will not be sourced more than
once to allow user-customized ones to take effect.

Before, session variables are in .zshrc, which causes non-interactive
shells to not be able to get those variables. For example, running a
command through SSH is in a non-interactive and non-login shell, which
suffers from this. With this patch, all kinds of shells can get
session variables.

The reason why these session variables are not moved to .zprofile is
that programs started by systemd user instances are not able to get
variables defined in that file. For example, GNOME
Terminal (gnome-terminal-server.service) is one of these programs and
doesn't get variables defined in .zprofile. As a result, the shells it
starts, which are interactive and non-login, do not get those
variables.

Fixes nix-community#2445

Related NixOS/nixpkgs#33219
Related NixOS/nixpkgs#45784
jian-lin added a commit to linj-fork/home-manager that referenced this pull request Feb 8, 2022
This patch moves both home.sessionVariables and
programs.zsh.sessionVariables from .zshrc to .zshenv. Additionally,
these two kinds of session variables will not be sourced more than
once to allow user-customized ones to take effect.

Before, session variables are in .zshrc, which causes non-interactive
shells to not be able to get those variables. For example, running a
command through SSH is in a non-interactive and non-login shell, which
suffers from this. With this patch, all kinds of shells can get
session variables.

The reason why these session variables are not moved to .zprofile is
that programs started by systemd user instances are not able to get
variables defined in that file. For example, GNOME
Terminal (gnome-terminal-server.service) is one of these programs and
doesn't get variables defined in .zprofile. As a result, the shells it
starts, which are interactive and non-login, do not get those
variables.

Fixes nix-community#2445

Related NixOS/nixpkgs#33219
Related NixOS/nixpkgs#45784
teto pushed a commit to teto/home-manager that referenced this pull request Feb 17, 2022
This patch moves both home.sessionVariables and
programs.zsh.sessionVariables from .zshrc to .zshenv. Additionally,
these two kinds of session variables will not be sourced more than
once to allow user-customized ones to take effect.

Before, session variables are in .zshrc, which causes non-interactive
shells to not be able to get those variables. For example, running a
command through SSH is in a non-interactive and non-login shell, which
suffers from this. With this patch, all kinds of shells can get
session variables.

The reason why these session variables are not moved to .zprofile is
that programs started by systemd user instances are not able to get
variables defined in that file. For example, GNOME
Terminal (gnome-terminal-server.service) is one of these programs and
doesn't get variables defined in .zprofile. As a result, the shells it
starts, which are interactive and non-login, do not get those
variables.

Fixes nix-community#2445

Related NixOS/nixpkgs#33219
Related NixOS/nixpkgs#45784
teto pushed a commit to nix-community/home-manager that referenced this pull request Feb 17, 2022
This patch moves both home.sessionVariables and
programs.zsh.sessionVariables from .zshrc to .zshenv. Additionally,
these two kinds of session variables will not be sourced more than
once to allow user-customized ones to take effect.

Before, session variables are in .zshrc, which causes non-interactive
shells to not be able to get those variables. For example, running a
command through SSH is in a non-interactive and non-login shell, which
suffers from this. With this patch, all kinds of shells can get
session variables.

The reason why these session variables are not moved to .zprofile is
that programs started by systemd user instances are not able to get
variables defined in that file. For example, GNOME
Terminal (gnome-terminal-server.service) is one of these programs and
doesn't get variables defined in .zprofile. As a result, the shells it
starts, which are interactive and non-login, do not get those
variables.

Fixes #2445

Related NixOS/nixpkgs#33219
Related NixOS/nixpkgs#45784

This file is not formatted before and is excluded by ./format, so I don't format it.
jian-lin added a commit to linj-fork/home-manager that referenced this pull request Feb 21, 2022
This patch moves both home.sessionVariables and
programs.zsh.sessionVariables from .zshrc to .zshenv. Additionally,
these two kinds of session variables will not be sourced more than
once to allow user-customized ones to take effect.

Before, session variables are in .zshrc, which causes non-interactive
shells to not be able to get those variables. For example, running a
command through SSH is in a non-interactive and non-login shell, which
suffers from this. With this patch, all kinds of shells can get
session variables.

The reason why these session variables are not moved to .zprofile is
that programs started by systemd user instances are not able to get
variables defined in that file. For example, GNOME
Terminal (gnome-terminal-server.service) is one of these programs and
doesn't get variables defined in .zprofile. As a result, the shells it
starts, which are interactive and non-login, do not get those
variables.

Fixes nix-community#2445

Related NixOS/nixpkgs#33219
Related NixOS/nixpkgs#45784
teto pushed a commit to teto/home-manager that referenced this pull request Aug 22, 2022
This patch moves both home.sessionVariables and
programs.zsh.sessionVariables from .zshrc to .zshenv. Additionally,
these two kinds of session variables will not be sourced more than
once to allow user-customized ones to take effect.

Before, session variables are in .zshrc, which causes non-interactive
shells to not be able to get those variables. For example, running a
command through SSH is in a non-interactive and non-login shell, which
suffers from this. With this patch, all kinds of shells can get
session variables.

The reason why these session variables are not moved to .zprofile is
that programs started by systemd user instances are not able to get
variables defined in that file. For example, GNOME
Terminal (gnome-terminal-server.service) is one of these programs and
doesn't get variables defined in .zprofile. As a result, the shells it
starts, which are interactive and non-login, do not get those
variables.

Fixes nix-community#2445

Related NixOS/nixpkgs#33219
Related NixOS/nixpkgs#45784

This file is not formatted before and is excluded by ./format, so I don't format it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants