Home-manager doesn't seem to recognize sessionVariables

I have a home.nix file that includes

  home.sessionVariables = {
    EDITOR = "code";
  };

but whenever I home-manager switch and re-login the EDITOR variable is still unset…

$ echo $EDITOR

$

Am I doing something wrong here? I’m running home-manager on Ubuntu 20.04.

3 Likes

Are you sure home-manager is installed properly? Like do any settings in home.nix take effect?

Yes, all the other settings in home.nix are recognized and take effect.

Interesting: if I’m in zsh the variable gets picked up, but not when I’m in bash…

1 Like

Maybe try searching the issues on GitHub for sessionVariables, for example: `home.sessionVariables` has no effect when using a graphical desktop environment · Issue #1011 · nix-community/home-manager · GitHub

you need to let home-manager manage your bash config files.

this is where the injection of the home-manager session-variables should get injected.

1 Like

Just for references, if you do:

  home = {
    sessionPath = [ "~/.emacs.d/bin" ];
    sessionVariables = {
      EDITOR = "emacs";
    };
  };

  # SessionPath and sessionVariables creates a hm-session file that must be sourced:
  # Beware, it puts it in .profile, not in the .bashrc!
  programs.bash = {
    enable = true;
  };

Then the hm-session-vars file is included in .profile (which is just run once when your graphical interface first starts). So either you can source .profile every bash, or you ask bash to source that file for you. I just do that by replacing the above bash configuration with:

  programs.bash = {
    enable = true;
    initExtra = ''
      . "$HOME/.nix-profile/etc/profile.d/hm-session-vars.sh"
    '';
  };
7 Likes

In case anyone tries this…

First, I had this.

  programs.bash = {
    enable = true;
    sessionVariables = {
      EDITOR = "vim";
    };
    initExtra = ''
      . "$HOME/.nix-profile/etc/profile.d/hm-session-vars.sh"
    '';
  };

But that didn’t work for me.

Then I changed it to this:

  home = {
    sessionVariables = {
      EDITOR = "vim";
    };
  };

Next I closed and reopened the terminal. Then it worked.

Are there times when you want bash.sessionVariables instead of home.sessionVariables? Or should you almost always just use home.sessionVariables?

I tried this (except I used “kak” instead of “vim”), but it isn’t working for me. After home-manager switch, restarting the terminal and even a reboot, I still get this:

echo $EDITOR
nano

I don’t even know where nano is being set as my editor. I can’t find a statement in home.nix nor configuration.nix.

EDITOR environment variable is defined as nano by default on NixOS:

Also, did you read this entry on HM FAQ? If your current shell is not provided by HM, you need to explicitly source a file for HM session variables to take effect.

That link seems to contradict itself and say the opposite of what you are saying.

First it says:
Home Manager is only able to set session variables automatically if it manages your Bash, Z shell, or fish shell configuration.

Then it says:
If you don’t want to let Home Manager manage your shell then you will have to manually source the ~/.nix-profile/etc/profile.d/hm-session-vars.sh file in an appropriate way.

I DO want to let Home Manager manage my shell, so therefore I don’t have to manually source that file.

But it doesn’t say that I must do something different to let HM manage my shell. That would seem to imply that I don’t need to do anything, that HM automatically manages my shell.

Is that a mistake in the Home Manager Manual?

home.sessionVariables are defined in a file named hm-session-vars.sh. If you are in a shell provided by a HM module, this file is already sourced (They are also explicitly sourcing it but it is hidden to the user.). If you are not using a shell provided by a HM module (e.g. shell provided by NixOS) or writing your own HM module for a shell, then you need to source that file yourself to have those sessions variables defined.

So do you have programs.bash.enable or the appropriate optiön for whatever shell yöu use?

Yes. I even have set programs.bash.sessionVariables = {EDITOR = "kak";};, but EDITOR is still defined as nano.

But do you have programs.bash.enable = true as well?

Yes.
#padding to 20 chars

Is it possible to set the EDITOR session variable for bash from Home Manager?

Yes, programs.bash.sessionVariables, though as far as I understand you tried that already…

Are other changes you do in your HM configuration for bash applied correctly?

Can you share more of your config to get a more complete view to it? It feels as if there is something not exactly as it needs to be…

One more person here with similar problems. I’ve set variables in home.sessionVariables, programs.bash.sessionVariables, and systemd.user.sessionVariables and nothing is set once I log in. Using SDDM → i3.

The strange thing is that things like GDK_SCALE etc seem to be having an effect on the UI, so they are being loaded at some point, but then they disappear. bemenu doesn’t recognize PATH updates, and terminals don’t see any of the variables that have been set.

UPDATE: It’s not clear that GDK_SCALE is being used.

UPDATE2: So it looks like the .profile file written by home-manager’s bash module is not executable, and is also owned by root. It’s put into $HOME with a chain of a couple symbolic links which obfuscates this, but this is clearly the reason it’s not working for me at least. Not sure if I’m doing something strange of if this is broken in the unstable channel.

Yes, I have the following in my HM config, and “pfetch” properly runs when opening a terminal, but $EDITOR is not getting set, it is still “nano”.

programs.bash = {
    enable = true;
    bashrcExtra = "pfetch";
    sessionVariables = {
        EDITOR = "kak";
    };
};