Home-manager doesn't seem to recognize sessionVariables

I’m hitting this, too. I can tell that hm-session-vars has been sourced (somewhere), but it seems like maybe /etc/set-environment was sourced after. (thus my EDITOR gets replaced with the nixos env default).

Further, there’s an “include guard” at the top of hm-session-vars that would prevent it from fixing EDITOR if it were sourced again later.

I’m probably going to remove the “include guard” and see what happens. I’m wondering if maybe it will just be fixed.

1 Like

Well, I removed the include guard, forgot about it and then later noticed git was using Helix, so I’d say that might be worth pursuing. I’ll try to send a PR for HM to start a discussion soon.

Shell/Bash Digression

I’m also interested in this peculiarity. Note that home.sessionVariables is about “environment variables to always set at login.” In particular, these variables are not set in a Shell session that is not a login session. You can try it out with eg. bash vs. bash --login. If we keep the example of Bash, when using home.sessionVariables and programs.bash.enable = true, HM does the following:

  • write the session variables in ~/.nix-profile/etc/profile.d/hm-session-vars.sh
  • source this file from ~/.profile
  • source ~/.profile (and ~/.bashrc) from ~/.bash_profile

During its startup, Bash will look for ~/.bash_profile when started as a login Shell and for ~/.bashrc when starting as an interactive non-login Shell (cf Bash Startup Files in the Bash Reference Manual), giving the behaviour that we all observe.

Back to this Problem

I don’t know what happens, but I suppose those variables are set correctly at login, but overridden afterwards. This is also what @colemickens’s experiment to remove the “include guard” suggests. The cleanest way to deal with this would be to understand what precisely happens and act there. I’m not good enough with NixOS/HM to figure that out at this point. I can think of two less clean ways to deal with this:

  1. Go for the aforementioned trick to source the ~/.nix-profile/etc/profile.d/hm-session-vars.sh in ~/.bashrc, and this whether HM manages it or not. (If HM manages it, then it should be done through HM’s configuration of course.) The risk of this is that this might pick up on some variables that HM puts in this file for the login, thinking they will be overridden by user-chosen ones afterwards.

  2. For this particular case, put the few variables bindings that are of interest to us directly in ~/.bashrc either through HM or manually. I will go for this one, and probably wait for a cleaner option later on. (There is already an environment.interactiveShellInit in NixOS, why not an interactiveShellVariables or something?)

1 Like

. “/nix/var/nix/profiles/system/etc/profiles/per-user/${USER}/etc/profile.d/hm-session-vars.sh”

to init…

failed
Ventura ERROR

I also just hit this problem. Home Manager now has a pull request that should solve this issue, at least for zsh.