direnv with powershell fails to export Nix flake variables
BlackHoleFox opened this issue · comments
Describe the bug
When using direnv
, Powershell, and Nix flakes on macOS (maybe the first attempt to do so?), a bad syntax Powershell error stops everything from working.
I'm new to this all I don't know if the fault lies with direnv
or nix-direnv
for how environment variable names are being produced (and quoted) but here I am since it looks trivially fixable inside direnv
.
In a project of mine I have a Nix flake that exports a few packages via pkgs.mkShell
and nothing else. When using just nix develop
it all works fine. I then attempted to add direnv
and nix-direnv
to it and hit a problem.
.envrc
:
use flake
After configuring my shell hook, I found a large error appearing when cd
ing into the project's folder:
direnv: loading ~/x/project/.envrc
direnv: using flake
warning: Git tree '/Users/fox/x/project' is dirty
direnv: export +AR +AS +CC +CONFIG_SHELL +CXX +HOST_PATH +IN_NIX_SHELL +LD +LD_DYLD_PATH +MACOSX_DEPLOYMENT_TARGET +NIX_BINTOOLS +NIX_BINTOOLS_WRAPPER_TARGET_HOST_aarch64_apple_darwin +NIX_BUILD_CORES +NIX_BUILD_TOP +NIX_CC +NIX_CC_WRAPPER_TARGET_HOST_aarch64_apple_darwin +NIX_CFLAGS_COMPILE +NIX_DONT_SET_RPATH +NIX_DONT_SET_RPATH_FOR_BUILD +NIX_ENFORCE_NO_NATIVE +NIX_HARDENING_ENABLE +NIX_IGNORE_LD_THROUGH_GCC +NIX_LDFLAGS +NIX_NO_SELF_RPATH +NIX_STORE +NM +PATH_LOCALE +RANLIB +SIZE +SOURCE_DATE_EPOCH +STRINGS +STRIP +TEMP +TEMPDIR +TMP +ZERO_AR_DATE +__darwinAllowLocalNetworking +__impureHostDeps +__propagatedImpureHostDeps +__propagatedSandboxProfile +__sandboxProfile +__structuredAttrs +buildInputs +buildPhase +builder +cmakeFlags +configureFlags +depsBuildBuild +depsBuildBuildPropagated +depsBuildTarget +depsBuildTargetPropagated +depsHostHost +depsHostHostPropagated +depsTargetTarget +depsTargetTargetPropagated +doCheck +doInstallCheck +dontAddDisableDepTrack +mesonFlags +name +nativeBuildInputs +out +outputs +patches +phases +preferLocalBuild +propagatedBuildInputs +propagatedNativeBuildInputs +shell +shellHook +stdenv +strictDeps +system ~PATH ~TMPDIR ~XDG_DATA_DIRS
Invoke-Expression: Variable reference is not valid. ':' was not followed by a valid variable name
character. Consider using ${} to delimit the name.
This error stopped the rest of the invocation and my expected packages weren't available. After dumping the contents of direnv export pwsh
, I ran it through PowerShell to look for syntax errors. They quickly showed up after. Note that this is shortened for brevity, the actual environment update expression is like 2KB of PowerShell but this error shows up hundreds of times:
`$env:'depsHostHost'='';` -> `Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name.`
To Reproduce
- Setup a terminal on macOS (Linux might work? I haven't tried) with PowerShell (
pwsh
),direnv
,nix
andnix-direnv
. - Create a new folder with a test flake inside of it.
- Run
echo "use flake" >> .envrc
- Run
direnv allow
inside the folder - Run
cd ../ && cd your-folder-name
- See the variable reference error
Expected behavior
I would have expected direnv
to successfully modify the variables it was passed and let me use my Nix flake packages.
Environment
- OS: macOS 14.3.1
- Shell: Powershell (pwsh) 7.3.4
- direnv 2.34.0
Additional context
This is happening because PowerShell doesn't know how to handle $env:<quote>key<quote>
. iiuc its not able to determine what is part of the key name and what is not.
#1255 is my attempt at fixing this in the simple way. Feedback would be appreciated :)