zsh: move sessionVariables from .zshrc to .zshenv (#2708)

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 commit is contained in:
Jian Lin 2022-02-17 17:20:56 +08:00 committed by GitHub
parent 810e5f3613
commit 2116fe6b50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 7 deletions

View file

@ -442,6 +442,19 @@ in
''; '';
}) })
{
home.file."${relToDotDir ".zshenv"}".text = ''
# Environment variables
. "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh"
# Only source this once
if [[ -z "$__HM_ZSH_SESS_VARS_SOURCED" ]]; then
export __HM_ZSH_SESS_VARS_SOURCED=1
${envVarsStr}
fi
'';
}
{ {
home.packages = with pkgs; [ zsh ] home.packages = with pkgs; [ zsh ]
++ optional cfg.enableCompletion nix-zsh-completions ++ optional cfg.enableCompletion nix-zsh-completions
@ -491,10 +504,6 @@ in
"source ${pkgs.zsh-syntax-highlighting}/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" "source ${pkgs.zsh-syntax-highlighting}/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
} }
# Environment variables
. "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh"
${envVarsStr}
${optionalString cfg.oh-my-zsh.enable '' ${optionalString cfg.oh-my-zsh.enable ''
# oh-my-zsh extra settings for plugins # oh-my-zsh extra settings for plugins
${cfg.oh-my-zsh.extraConfig} ${cfg.oh-my-zsh.extraConfig}

View file

@ -16,9 +16,9 @@ with lib;
test.stubs.zsh = { }; test.stubs.zsh = { };
nmt.script = '' nmt.script = ''
assertFileExists home-files/.zshrc assertFileExists home-files/.zshenv
assertFileRegex home-files/.zshrc 'export V1="v1"' assertFileRegex home-files/.zshenv 'export V1="v1"'
assertFileRegex home-files/.zshrc 'export V2="v2-v1"' assertFileRegex home-files/.zshenv 'export V2="v2-v1"'
''; '';
}; };
} }