Home-manager: sessionVariables not persisted after log out

Hi all, I am using standalone flake home-manager to manage everything outside of my NixOS system settings. I am using ZSH as my default shell, which I did not configure before installing home-manager.
For context, I am currently using Gnome Wayland (with autologin, if relevant), with my home-manager and NixOS configs flakes following the unstable branch.
When I set the following in my home.nix file:

  programs.neovim = {
    enable = true;
    defaultEditor = true;
  };

  # ...
  
  home.sessionVariables = {
    EDITOR = "nvim";
  };

  # Let Home Manager manage bash
  programs.bash.enable = true;
  # Let Home Manager manage zsh
  programs.zsh = {
    enable = true;
    oh-my-zsh = {
      enable = true;
      # ... set theme
    };
  };

The correct EDITOR is set on initial boot (nvim), but if I log out and back in again it is reset to nano.
When I add the following it works as expected:

programs.zsh = {
  # ...
  # Have to add as home.sessionVariables and programs.neovim.defaultEditor do not work on logout/login? Maybe due to guard?
 sessionVariables = {
    EDITOR = "nvim";
    VISUAL = "nvim";
  };
};

I have also set programs.zsh.enable = true and set pkgs.zsh as the default shell for my user in my NixOS configuration.nix, and do not make other changes to zsh or the environment variables there. I have checked that the shell is not a login shell - when I run zsh --login it initializes the session vars as expected.

Is setting the program.zsh.sessionVariables the correct way to do this, or am I missing something as to why home.sessionVariables is not working as expected? Thanks a lot!

1 Like

This is a known issue. See:

I’m working around this bug like that: nixcfg/modules/home-manager/basic/fish.nix at 0e544e140e7ca2dd0f7191bc59d19f2a304947d4 · dawidd6/nixcfg · GitHub (you must be sure it won’t break anything on your system though, i.e. sourcing hm-session-vars.sh file can be repeated, so no PATH modification allowed for example)

I have the same issue as OP, and after i went through the linked Github issues i’m still not 100% sure whats going on. In some of the linked issues, using home.sessionVariables does not work at all since some Desktop Environments did/do not read ~/.profile.

However using GNOME/GDM the home.sessionVariables are actually applied correctly on first login after a boot. For example my EDITOR is also correctly said to nvim. Just after a logout/login, some session variables get overridden (EDITOR is set to nano all of a sudden).

I assume the /etc/profile might get sourced again. But if i logout+login again, why would /etc/profile get sourced, but this time ~/.profile doesn’t? Whats the difference between my initial login and any subsequent login?

Maybe you have an explaination for this. Would be great to get this working in a consistent way.