Home-manager XDG_XXX env vars are NOT getting created

Using Win11 WSL-Ubuntu 22.04 inside powershell, I installed home-manager v24.05 using curl and used Nixpkgs v24.05 channel. Then edited ~/.config/home-manager/home.nix and added:
xdg.enable = true;

Afterwards, on the command line I run home-manager switch; I get XDG_RUNTIME_DIR and non-standard value of XDG_DATA_DIRS but I don’t get to have other XDG_XXX environment variables created and assigned with proper values.

If, instead, I do
{
home.sessionVariables = {
XDG_CACHE_HOME = “$HOME/.cache”;
XDG_CONFIG_HOME = “$HOME/.config”;

};
}

then I get all kinds of conflicts. Any simple fix?

Home manager has an option to set all these.

Set xdg.enable = true; in your home.nix.

I mentioned that I’ve done xdg.enable = true; and also reconnected to my WSL shell but I still don’t see XDG_XXX variables

Please post the exact command you ran and the exact error you received; that’s a given if you want troubleshooting help.

Also keep in mind you may need to reboot for envvars to update depending on the context.

Thanks for the discussion. I rebooted my windows machine and nothing new happened.
After setting xdg env vars thru home.sessionVariables in my home.nix (which defeats the purpose of xdg.enable = true;) I ran the following command:
$ home-manager switch
and I got the following error:

error:
       … while calling the 'derivationStrict' builtin
         at <nix/derivation-internal.nix>:9:12:
            8|
            9|   strict = derivationStrict drvAttrs;
             |            ^
           10|

       … while evaluating derivation 'home-manager-generation'
         whose name attribute is located at /nix/store/js49clna89jkq2rlz9qfya70hbrxraiq-nixpkgs-24.05/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:333:7

       … while evaluating attribute 'buildCommand' of derivation 'home-manager-generation'
         at /nix/store/js49clna89jkq2rlz9qfya70hbrxraiq-nixpkgs-24.05/nixpkgs/pkgs/build-support/trivial-builders/default.nix:68:17:
           67|         enableParallelBuilding = true;
           68|         inherit buildCommand name;
             |                 ^
           69|         passAsFile = [ "buildCommand" ]

       … while evaluating the option `home.activation.installPackages.data':

       … while evaluating definitions from `/home/username/.nix-defexpr/channels/home-manager/modules/home-environment.nix':

       (stack trace truncated; use '--show-trace' to show the full, detailed trace)

       error: The option `home.sessionVariables.XDG_CACHE_HOME' has conflicting definition values:
       - In `/home/username/.nix-defexpr/channels/home-manager/modules/misc/xdg.nix': "/home/username/.cache"
       - In `/home/username/.config/home-manager/home.nix': "$HOME/.cache"
       Use `lib.mkForce value` or `lib.mkDefault value` to change the priority on any of these definitions.

The last line of the error tells you how to address this. If you want a definition to take priority you can use lib.mkForce.

More info in NixOS Manual

Both the definitions are the same. I am happy with the first definition and I don’t want to use home.sessionVariables block. The problem is that the first definition, which is the outcome of xdg.enable = true; is NOT adding any xdg variables in my bash terminal even after reboots. I want to continue with the default xdg definitions but I can’t get it to work.

I think we need to see your home.nix

Thanks: here’s my ~/.config/home-manager/home.nix file:

{ config, pkgs, ... }:

{
  # Home Manager needs a bit of information about you and the paths it should
  # manage.
  home.username = "username";
  home.homeDirectory = "/home/username";
  # Enable XDG
  xdg.enable = true;

  home.stateVersion = "24.05"; # Please read the comment before changing.
  
  programs.gpg.enable = true;

  programs.ssh = {
    enable = true;

    matchBlocks = {
      bserv = {
        port = 22;
        hostname =  "106.403.111.118";
        user = "username-c";
        identityFile = "${config.home.homeDirectory}/.ssh/id_ssh";
      };
    };
  };

  programs.git = {
    enable = true;
    includes = [
      { path = "~/.git/.config-global"; }
      {
        path = "~/.git/.config-pri";
        condition = "gitdir:wa/pri/";
      }
    ];
    aliases = {
      ap = "add -p";
      aa = "add -A";
    };
    
    extraConfig = {
      core = {
        whitespace = "trailing-space,space-before-tab";
      };
    };
  };

  # The home.packages option allows you to install Nix packages into your
  # environment.
  home.packages = [
    # # Adds the 'hello' command to your environment. It prints a friendly
    # # "Hello, world!" when run.
    # pkgs.hello
    pkgs.openssh
    pkgs.curl
    pkgs.git
    pkgs.rsync
    pkgs.neovim
    pkgs.gpg-tui
    pkgs.xplr
    pkgs.wl-clipboard

    pkgs.cryptsetup
    pkgs.lvm2

    pkgs.difftastic
    pkgs.ripgrep-all
    pkgs.eza
    pkgs.fd
    pkgs.sd
    pkgs.bat
    pkgs.fastfetch

    pkgs.just
    pkgs.nushell
    
    pkgs.ansible

  ];
  
  home.file = {};

  home.sessionVariables = {
    # EDITOR = "emacs";
    #XDG_CACHE_HOME = "$HOME/.cache";
    #XDG_CONFIG_DIRS = "/etc/xdg";
    #XDG_CONFIG_HOME = "$HOME/.config";
    #XDG_DATA_DIRS = "/usr/local/share/:/usr/share/";
    #XDG_DATA_HOME = "$HOME/.local/share";
    #XDG_STATE_HOME = "$HOME/.local/state";
    ##XDG_DATA_DIRS="/usr/local/share:/usr/share:$HOME/.nix-profile/share:/nix/var/nix/profiles/default/share:/var/lib/snapd/desktop"
  };

  # Let Home Manager install and manage itself.
  programs.home-manager.enable = true;
}

you need to tell bash to read what nix has set for the variables.

So either programs.bash.enable = true;
or

See point 4 in Standalone installation in Home Manager manual

If you do not plan on having Home Manager manage your shell configuration then you must source the

$HOME/.nix-profile/etc/profile.d/hm-session-vars.sh
2 Likes

What exactly is the purpose of this line?

Sets various XDG_ environment variables in the shell and xdg.* variables in nix - see

Nix configuration of other applications can test for xdc being enabled led and thus change where they read and write files to/from

1 Like

It works, thanks.
The solution was to write in home.nix:

programs.bash.enable = true;

It wasn’t mentioned anywhere in Home-Manager Manual or Appendix. Perhaps it can be added to this document:
Home Manager Manual
and
(Home Manager Manual)

It can easily save precious hours/days of busy developers, whom are making use of Nix home-manager to maintain their work machines.