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

NIX_BIN_PREFIX: unbound variable #70

Closed
aaschmid opened this issue Feb 24, 2021 · 8 comments
Closed

NIX_BIN_PREFIX: unbound variable #70

aaschmid opened this issue Feb 24, 2021 · 8 comments

Comments

@aaschmid
Copy link

Since version 1.2.3 I get the following error

~/work/some-dir
direnv: loading ~/work/some-dir/.envrc
direnv: using nix
/nix/store/4aifrh1mksd52aam92a4zc6hzjvargrk-nix-direnv-1.2.3/share/nix-direnv/direnvrc:145: NIX_BIN_PREFIX: unbound variable

It works with version 1.2.1.

My system info is

 - system: `"x86_64-darwin"`
 - host os: `Darwin 19.6.0, macOS 10.15.7`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.3.10`
 - channels(user): `"darwin, home-manager"`
 - channels(root): `"nixpkgs-21.05pre272202.7fd33568097, darwin, home-manager"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixpkgs`

Which other information are required?

I haven't investigated further yet, but if you have a tip for me how to fix, I can also try to provide a PR

@Mic92
Copy link
Member

Mic92 commented Feb 24, 2021

This is set here:

NIX_BIN_PREFIX=$(command -v nix-shell)

Do you have by chance anything in your .envrc that unsets NIX_BIN_PREFIX?

@aaschmid
Copy link
Author

I am not sure about your question. I use use_nix() and there NIX_BIN_PREFEX is set:

path=$("${NIX_BIN_PREFIX}nix-instantiate" --find-file nixpkgs)
.

Most of my .envrc files only have use nix. I tried with various shell.nix files using nodejs, java, python with virtual envs.

Does that help?

@Mic92
Copy link
Member

Mic92 commented Feb 25, 2021

I am not sure about your question. I use use_nix() and there NIX_BIN_PREFEX is set:

path=$("${NIX_BIN_PREFIX}nix-instantiate" --find-file nixpkgs)

.

Most of my .envrc files only have use nix. I tried with various shell.nix files using nodejs, java, python with virtual envs.

Does that help?

Can you put set -x in your ~/.direnvrc before nix-direnv is sourced and
give me the output of an example .envrc.
I currently cannot reproduce what is going on in your case.

@aaschmid
Copy link
Author

Here is the result of my first and in depth investigation:

  1. My direnvrc installed via home-manager has following line to check for $NIX_BIN_PREFIX: if [[ -z ${NIX_BIN_PREFIX:-/nix/store/q0z2kvkgrpvaipa87jl98qh7g5pym5fj-nix-2.3.10/bin/} ]]; then
  2. Changing this to if [[ -z ${NIX_BIN_PREFIX:-} ]]; then and it works as expected
  3. This change comes from pkgs/tools/misc/nix-direnv/default.nix line 18: --replace "\''${NIX_BIN_PREFIX:-}" "\''${NIX_BIN_PREFIX:-${nix}/bin/}" \
  4. Creating an overlay with following fixes it:
   nixpkgs.overlays = [
    (self: super: {
      nix-direnv = super.nix-direnv.overrideAttrs (oldAttrs: rec {
        # Substitute instead of wrapping because the resulting file is getting sourced, not executed:
        postPatch = ''
          substituteInPlace direnvrc \
            --replace "grep" "${super.gnugrep}/bin/grep"
        '';
      });
    })
  ];

Sorry for not doing deep investigation earlier. If you aggree with that change I can create a PR for it on nixpkgs repo.

@aaschmid
Copy link
Author

aaschmid commented Feb 27, 2021

The other alternative might be changing

--replace "\''${NIX_BIN_PREFIX:-}" "\''${NIX_BIN_PREFIX:-${nix}/bin/}" \

to

--replace "\''${NIX_BIN_PREFIX}" "\''${NIX_BIN_PREFIX:-${nix}/bin/}" \

(Note the missing :- in order to set a proper default for $NIX_BIN_PREFIX if there is a problem with setting it at the beginning ...

@Mic92
Copy link
Member

Mic92 commented Feb 27, 2021

The other alternative might be changing

--replace "\''${NIX_BIN_PREFIX:-}" "\''${NIX_BIN_PREFIX:-${nix}/bin/}" \

to

--replace "\''${NIX_BIN_PREFIX}" "\''${NIX_BIN_PREFIX:-${nix}/bin/}" \

(Note the missing :- in order to set a proper default for $NIX_BIN_PREFIX if there is a problem with setting it at the beginning ...

yes please.

@aaschmid
Copy link
Author

aaschmid commented Mar 1, 2021

@Mic92: Should I also change the nixFlakes usage in the

sed -i "2iNIX_BIN_PREFIX=${nixFlakes}/bin/" direnvrc
as we did in nixpkgs?

@Mic92
Copy link
Member

Mic92 commented Mar 1, 2021

@Mic92: Should I also change the nixFlakes usage in the

sed -i "2iNIX_BIN_PREFIX=${nixFlakes}/bin/" direnvrc

as we did in nixpkgs?

See https://github.com/NixOS/nixpkgs/pull/114622/files#r584597612

aaschmid added a commit to aaschmid/nixpkgs that referenced this issue Mar 3, 2021
* Reason: statically set `${NIX_BIN_PREFIX}` within generated
  `direnvrc`.
* Before it only replaced the `-z ${NIX_BIN_PREFIX:-}` check
  which therefore never was `true`. So, also `NIX_BIN_PREFIX` never got
  set such that its usage later always failed execution with
  `NIX_BIN_PREFIX: unbound variable`. With the fix, the line containing
  the check will no longer be replaced.
* Solves nix-community/nix-direnv#70
* See also discussion on NixOS#114622

Signed-off-by: Andreas Schmid <service@aaschmid.de>
@aaschmid aaschmid closed this as completed Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants