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

zsh: set environment variables in zshenv instead of zprofile #217205

Merged
merged 1 commit into from
Mar 23, 2023

Conversation

jian-lin
Copy link
Contributor

This patch fixes two issues:

  1. The file in which environment variables are set is inconsistent.
  • This file sets them in zprofile since 20111. This is the case when programs.zsh.enable is not set.
  • Zsh module sets them in zshenv since the very beginning in 20122. This is the case when programs.zsh.enable is set.
  1. Setting environment variables in zprofile overrides what users set in .zshenv. See these3 home-manager4 issues5.
Description of changes
Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.05 Release Notes (or backporting 22.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@jian-lin
Copy link
Contributor Author

An explanation for home-manager issues: nix-community/home-manager#3681 (comment).

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/home-manager-doesnt-seem-to-recognize-sessionvariables/8488/25

@jian-lin
Copy link
Contributor Author

jian-lin commented Feb 22, 2023

In short, when system-level programs.zsh enable is not set, this patch makes environment variables set in users' .zshenv not be overridden by the system-level ones.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-ready-for-review/3032/1868

Copy link
Member

@samuela samuela left a comment

Choose a reason for hiding this comment

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

LGTM although I don't have any familiarity with this corner of nixpkgs

@jian-lin
Copy link
Contributor Author

Can we merge this? Or is there anything else I need to do?

@NickCao
Copy link
Member

NickCao commented Feb 28, 2023

I know nothing about zsh and is not confident enough about this. Could any real zsh user provide some opinions?

@samuela
Copy link
Member

samuela commented Feb 28, 2023

cc maintainers @pSub and @Artturin

@jian-lin
Copy link
Contributor Author

jian-lin commented Mar 5, 2023

A release note is added as requested by @Mic92. IMHO, it's ready to be merged.

@jian-lin jian-lin requested a review from chvp March 7, 2023 08:26
Copy link
Member

@chvp chvp left a comment

Choose a reason for hiding this comment

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

I added this to my nixos-config and can confirm that it fixed the issue I reported originally in nix-community/home-manager@2116fe6#r67182173

if test -r /etc/zprofile; then
. /etc/zprofile
if test -r /etc/zshenv; then
. /etc/zshenv
else
emulate bash
alias shopt=false
. /etc/profile
Copy link
Contributor Author

Choose a reason for hiding this comment

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

My motivation of this pr is to solve the issue that env vars set in .zshenv are overridden by those in /etc/set-environment ($out/etc/zprofile sources /etc/profile which sources /etc/set-environment) on NixOS system with programs.zsh.enable = false. This issue comes from users of home-manager.

I can think of three proposals to deal with this issue:

Proposal 0: souce /etc/profile in $out/etc/zprofile, i.e., the old behaviour

  • Pros
    • config.environment.loginShellInit is sourced in login zsh
  • Cons
    • env vars set in .zshenv are overridden by those in /etc/set-environment
    • config.environment.shellInit is not sourced in non-login zsh
    • config.programs.bash.shellInit is also sourced in login zsh
    • config.programs.bash.loginShellInit is also sourced in login zsh

Proposal 1: souce /etc/profile in $out/etc/zshenv, i.e., what this patch currently dose

  • Pros
    • env vars set in .zshenv are not overridden by those in /etc/set-environment
    • config.environment.shellInit is sourced in all zsh
  • Cons
    • config.environment.loginShellInit is also sourced in non-login zsh
    • config.programs.bash.shellInit is also sourced in all zsh
    • config.programs.bash.loginShellInit is also sourced in all zsh

Proposal 2: souce /etc/set-environment in $out/etc/zshenv

  • Pros
    • env vars set in .zshenv are not overridden by those in /etc/set-environment
    • config.programs.bash.shellInit is not sourced in any zsh
    • config.programs.bash.loginShellInit is not sourced in any zsh
  • Cons
    • config.environment.shellInit is not sourced in any zsh
    • config.environment.loginShellInit is not sourced in any zsh

I prefer proposal 2 because it fixes the issue without any wrong behaviour, i.e., sourcing config of bash. If you want those shell configs in config.environment, just set programs.zsh.enable.

WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

I also think proposal 2 sounds more reasonable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have changed to proposal 2.

pkgs/shells/zsh/default.nix Outdated Show resolved Hide resolved
This patch fixes two issues:

1. The file in which environment variables are set is inconsistent.
  - This file sets them in zprofile when programs.zsh.enable is not
  set.
  - Zsh module sets them in zshenv when programs.zsh.enable is set.

2. Setting environment variables in zprofile overrides what users set
in .zshenv.  See these[1] home-manager[2] issues[3].

/etc/profile is also changed to /etc/set-environment. Here is a
comparison:

Using /etc/profile:
- Pros
  - config.environment.shellInit is sourced in all zsh
- Cons
  - config.environment.loginShellInit is also sourced in non-login zsh
  - config.programs.bash.shellInit is also sourced in all zsh
  - config.programs.bash.loginShellInit is also sourced in all zsh

Using /etc/set-environment:
- Pros
  - config.programs.bash.shellInit is not sourced in any zsh
  - config.programs.bash.loginShellInit is not sourced in any zsh
- Cons
  - config.environment.shellInit is not sourced in any zsh
  - config.environment.loginShellInit is not sourced in any zsh

[1]: nix-community/home-manager#2751 (comment)
[2]: nix-community/home-manager#2991
[3]: nix-community/home-manager#3681 (comment)
Copy link
Member

@chvp chvp left a comment

Choose a reason for hiding this comment

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

Works again.

@jian-lin
Copy link
Contributor Author

A friendly ping @Mic92

@K900 K900 merged commit 382c756 into NixOS:master Mar 23, 2023
@RaitoBezarius
Copy link
Member

This will probably break the existing zsh configurations of some people, right?

@jian-lin
Copy link
Contributor Author

jian-lin commented Mar 23, 2023

This will probably break the existing zsh configurations of some people, right?

For users who set their shell to zsh and do not enable the system-level zsh module, yes, there are changed behaviours listed in the proposal 2. I have added a release note for that. That should be enough, right?

@K900
Copy link
Contributor

K900 commented Mar 23, 2023

We should really remove the generic shellInit options, especially with more and more people wanting to run stuff like xonsh/nu/whatever other not-even-remotely-posix shell.

@jian-lin jian-lin deleted the fix-zsh-set-env branch March 23, 2023 16:11
@expipiplus1
Copy link
Contributor

FWIW this totally broke zsh for me, every new shell was sourcing /etc/set-environment unconditionally, so resetting PATH (and removing anything inserted by adding nix-shell etc...).

Adding programs.zsh.enable = true in my system config didn't fix things.

(I haven't investigated this deeply at all, but the if [ -z "" ]; looks very suspect to me...)

❯ cat /nix/store/z2cnzz34sqrk8zwqq2g7sq0pfj8kk6j8-zsh-5.9/etc/zshenv_zwc_is_used
if test -e /etc/NIXOS; then
  if test -r /etc/zshenv; then
    . /etc/zshenv
  else
    emulate bash
    alias shopt=false
    if [ -z "" ]; then
      . /etc/set-environment
    fi
    unalias shopt
    emulate zsh
  fi
  if test -r /etc/zshenv.local; then
    . /etc/zshenv.local
  fi
else
  # on non-nixos we just source the global /etc/zshenv as if we did
  # not use the configure flag
  if test -r /etc/zshenv; then
    . /etc/zshenv
  fi
fi

@jian-lin
Copy link
Contributor Author

@expipiplus1 Thanks! Fixed in #223696.

@expipiplus1
Copy link
Contributor

Thanks!

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/home-manager-not-setting-sessionvariables-or-shellaliases/33947/2

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

10 participants