Home-manager: `home.sessionVariables` has no effect when using a graphical desktop environment

Created on 27 Jan 2020  路  7Comments  路  Source: nix-community/home-manager

At the time of writing, the contents home.sessionVariables are placed in ~/.profile.

As I use KDE Plasma, these variables never get loaded for me because:

  • /etc/profile on NixOS 19.09 _does not_ source ~/.profile
  • sddm _does not_ source ~/.profile
  • Konsole and other terminal emulators start _non-login_ interactive shells by default (and therefore do not load ~/.bash_profile or ~/.profile, but ~/.bashrc instead)

For example, if I set:

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

I get:

[joe@mycomputer:~] $ echo $EDITOR
nano

My ~/.profile, ~/.bash_profile, and ~/.bashrc files are all managed by home-manager and their contents are as follows:

# ~/.profile
# -*- mode: sh -*-

. "/home/joe/.nix-profile/etc/profile.d/hm-session-vars.sh"

# ~/.bash_profile
# -*- mode: sh -*-

# include .profile if it exists
[[ -f ~/.profile ]] && . ~/.profile

# include .bashrc if it exists
[[ -f ~/.bashrc ]] && . ~/.bashrc

# ~/.bashrc
# -*- mode: sh -*-

# Commands that should be applied only for interactive shells.
if [[ $- == *i* ]]; then
  HISTFILE="$HOME/.bash_history"
HISTFILESIZE=100000
HISTSIZE=10000

  shopt -s histappend
shopt -s checkwinsize
shopt -s extglob
shopt -s globstar
shopt -s checkjobs


fi

To make matters worse, it seems that NixOS doesn't make an authoritative decision about whether ~/.profile (or actually, whether any other file) will be reliably sourced at login when using a graphical environment (https://github.com/NixOS/nixpkgs/issues/5200)

If I log in via the console or via SSH however, this _does_ count as a bash login shell and my environment variables are set correctly.

For now my current workaround is to put the following in my system's /etc/nixos/configuration.nix which applies a workaround at the system level:

environment.loginShellInit = ''
    if [ -e $HOME/.profile ]
    then
        . $HOME/.profile
    fi
'';

but I could just as easily put the following in my home.nix:

home.file.".xprofile".text = ''
    if [ -e $HOME/.profile ]
    then
        . $HOME/.profile
    fi
'';

I also think I could get around this by putting xsession.enable = true; in my home.nix which would add the necessary if [ -e $HOME/.profile ] ... code to my ~/.xprofile.

Any comments or suggested solutions to this quirk between home-manager and NixOS, or anything in the documentation I have missed?

Most helpful comment

Completely untested, but you might try systemd.user.sessionVariables instead

All 7 comments

I have the same problem.

Adding the

home.file.".xprofile".text = ''
    if [ -e $HOME/.profile ]
    then
        . $HOME/.profile
    fi
'';

snippet to my home manager config however does not work with gnome. When i do source ~/.xprofile, that works, but simply by logging in into gnome and looking up my env in the shell, i don't see this was sourced.

Completely untested, but you might try systemd.user.sessionVariables instead

I'm using this to address the issue, works like a charm:

    # Source home-manager environment
    environment.extraInit = concatMapStringsSep "\n" (user: let
      homedir = config.users.users.${user}.home;
    in ''
      if [ "$(id -un)" = "${user}" ]; then
        . "${homedir}/.nix-profile/etc/profile.d/hm-session-vars.sh"
      fi
    '') ["user1" "user2"]);

Completely untested, but you might try systemd.user.sessionVariables instead

cool this worked for me. But I have to note that I autostart my window manager Sway also with this command:

systemctl --user import-environment

Completely untested, but you might try systemd.user.sessionVariables instead

@mammothbane unfortunately does seem to be ignored by gnome.

I am currently setting env vars that shell also be available in gnome shells like this:

  programs.bash.bashrcExtra = ''
    SSH_AUTH_SOCK="/run/user/$UID/gnupg/S.gpg-agent.ssh"
  '';

i don't like it, but i also don't want to change anything in /etc/nixos/configuration.nix to make this work, as this would break my session vars for all the systems i have no root access to.

Could home-manager set systemd.sessionVariables with home.sessionVariables by default?

The right way to fix this is to wrap apps which need the socket, since giving an env var to an application that shoundnt use it is both wasteful and incorrect. This is why gnome stopped sourcing profile's, and is the way forward. Li's plan is to use systemd units to launch all apps. Gnome devs have been discussing this also.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PierreR picture PierreR  路  5Comments

mightybyte picture mightybyte  路  5Comments

rschulman picture rschulman  路  5Comments

JonathanReeve picture JonathanReeve  路  8Comments

stites picture stites  路  3Comments