Nix shell, direnv and XDG_DATA_DIRS?

TL;DR When I enter a Nix shell using direnv the value of $XDG_DATA_DIRS is overwritten rather than appended to. How can I prevent this?

Details

I’m using Nix on my ArchLinux box and see a slightly strange behaviour from xdg-open when using nix-shell.

Usually xdg-open https://google.com will open Firefox, my default browser, and take me to Google’s search page.The same happens in a Nix shell I’ve entered manually using nix-shell.

However, in a Nix shell entered using direnv I instead see this:

❯ xdg-open https://google.com
gio: https://google.com: No application is registered as handling this file

I’ve realised that this is due to $XDG_DATA_DIRS being replaced, rather than appended to:

❯ nix-shell
~/tmp/nix-shell-test via ❄️  impure (nix-shell) on ☁️  (eu-central-1)
❯ echo $XDG_DATA_DIRS
/nix/store/zrh1cskgv6kc8bh7hzfii14h2nck33qv-patchelf-0.12/share:/home/magnus/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share/:/usr/share/

compared to

❯ direnv allow
direnv: loading ~/tmp/nix-shell-test/.envrc
direnv: using nix
direnv: using cached derivation
direnv: eval /home/magnus/tmp/nix-shell-test/.direnv/cache-pre278688.c0e88185200
direnv: export +AR +AS +CC +CONFIG_SHELL +CXX +HOST_PATH +IN_NIX_SHELL +LD +NIX_BINTOOLS +NIX_BINTOOLS_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_BUILD_CORES +NIX_BUILD_TOP +NIX_CC +NIX_CC_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_CFLAGS_COMPILE +NIX_ENFORCE_NO_NATIVE +NIX_HARDENING_ENABLE +NIX_INDENT_MAKE +NIX_LDFLAGS +NIX_STORE +NM +OBJCOPY +OBJDUMP +RANLIB +READELF +SIZE +SOURCE_DATE_EPOCH +STRINGS +STRIP +TEMP +TMP +TMPDIR +__ETC_PROFILE_SOURCED +buildInputs +builder +configureFlags +depsBuildBuild +depsBuildBuildPropagated +depsBuildTarget +depsBuildTargetPropagated +depsHostHost +depsHostHostPropagated +depsTargetTarget +depsTargetTargetPropagated +doCheck +doInstallCheck +name +nativeBuildInputs +nobuildPhase +out +outputs +patches +phases +propagatedBuildInputs +propagatedNativeBuildInputs +shell +shellHook +stdenv +strictDeps +system ~PATH ~XDG_DATA_DIRS
~/tmp/nix-shell-test via ❄️  impure (nix-shell) on ☁️  (eu-central-1)
❯ echo $XDG_DATA_DIRS
/nix/store/zrh1cskgv6kc8bh7hzfii14h2nck33qv-patchelf-0.12/share

Files

shell.nix:

{pkgs ? (import (import ./nix/sources.nix).nixpkgs {})
}:

with pkgs;

let
  shell-pkgs =
    [niv
    ];

in
mkShell {
  buildInputs = shell-pkgs;
}

.envrc:

use nix

It turns out this is an issue with direnv itself and there’s a a bug reported already.

The workaround is to put use nix --keep XDG_DATA_DIRS in .envrc.

2 Likes