Trouble setting environment variables in home manager

I think this happens because home.sessionVariables writes variables to ~/.profile, but shells don’t source this by default. However, you can source it with:

This is only necessary for bash as zsh re-sources ~/.zshenv for new instances (see the zsh docs)

  programs.bash = {
    enable = true;

    initExtra = ''
      # include .profile if it exists
      [[ -f ~/.profile ]] && . ~/.profile
    '';
  };

Alternatively, you can set the variables in the shell config. With this, you don’t have to re-login for changes to take effect. You just have to re-source your shell config or open a new terminal:

  programs.zsh = {
    enable = true;
    sessionVariables = {
      EDITOR = "vim";
    };
  };

On another note, some environment variables can be set by home-manager. For example, the defaultEditor option in the following example will automatically set the EDITOR env variable to vim:

  programs.vim = {
    enable = true;
    defaultEditor = true;
  };
1 Like