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
Conversation
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`
What if someone wants to |
@@ -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; |
There was a problem hiding this comment.
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!
Everything would stay as it was before this PR as it sets |
ping?
I agree that the new `/etc` thing should be documented somewhere, but I don't see a good place in the manual where to put it, and I'm not feeling like making one right now.
|
This does fix a rather unfortunate bug with |
Yes, you're right, it should.
|
Everything seems to be working correctly, no problem so far. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
backported in e981546..23da995 |
done |
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
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 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 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 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 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.
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 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.
Motivation for this change
An edited version of #44720.
Things done
/cc @LnL7 @pxc @rnhmjoj from git-blame