Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

direnv with powershell fails to export Nix flake variables #1254

Open
BlackHoleFox opened this issue Mar 12, 2024 · 1 comment · May be fixed by #1255
Open

direnv with powershell fails to export Nix flake variables #1254

BlackHoleFox opened this issue Mar 12, 2024 · 1 comment · May be fixed by #1255
Labels

Comments

@BlackHoleFox
Copy link

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 cding 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

  1. Setup a terminal on macOS (Linux might work? I haven't tried) with PowerShell (pwsh), direnv, nix and nix-direnv.
  2. Create a new folder with a test flake inside of it.
  3. Run echo "use flake" >> .envrc
  4. Run direnv allow inside the folder
  5. Run cd ../ && cd your-folder-name
  6. 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.

@BlackHoleFox
Copy link
Author

BlackHoleFox commented Mar 12, 2024

#1255 is my attempt at fixing this in the simple way. Feedback would be appreciated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

1 participant