From 2116fe6b50a5118d56f1f443cccf024abee9de40 Mon Sep 17 00:00:00 2001 From: Jian Lin <75130626+jian-lin@users.noreply.github.com> Date: Thu, 17 Feb 2022 17:20:56 +0800 Subject: [PATCH] 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. --- modules/programs/zsh.nix | 17 +++++++++++++---- .../modules/programs/zsh/session-variables.nix | 6 +++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/modules/programs/zsh.nix b/modules/programs/zsh.nix index 6cd88030..2d81698b 100644 --- a/modules/programs/zsh.nix +++ b/modules/programs/zsh.nix @@ -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 ] ++ optional cfg.enableCompletion nix-zsh-completions @@ -491,10 +504,6 @@ in "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 '' # oh-my-zsh extra settings for plugins ${cfg.oh-my-zsh.extraConfig} diff --git a/tests/modules/programs/zsh/session-variables.nix b/tests/modules/programs/zsh/session-variables.nix index 638b41c9..f6f795f7 100644 --- a/tests/modules/programs/zsh/session-variables.nix +++ b/tests/modules/programs/zsh/session-variables.nix @@ -16,9 +16,9 @@ with lib; test.stubs.zsh = { }; nmt.script = '' - assertFileExists home-files/.zshrc - assertFileRegex home-files/.zshrc 'export V1="v1"' - assertFileRegex home-files/.zshrc 'export V2="v2-v1"' + assertFileExists home-files/.zshenv + assertFileRegex home-files/.zshenv 'export V1="v1"' + assertFileRegex home-files/.zshenv 'export V2="v2-v1"' ''; }; }