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

Requires GNU grep #3

Closed
tviti opened this issue Mar 10, 2020 · 12 comments
Closed

Requires GNU grep #3

tviti opened this issue Mar 10, 2020 · 12 comments

Comments

@tviti
Copy link

tviti commented Mar 10, 2020

Issue

This isn't really an issue with nix-direnv, but more of a caveate I encountered when trying to use nix-direnv on macOS. I first encountered this using direnv-mode in emacs w/ nix-direnv on macOS, but it looks like it's also a problem just using direnv from bash on macOS.

From a directory that DOESN'T have a cached environment (i.e. .direnv either doesn't exist or is empty), the following error is shown when making a first attempt at loading the env:

usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
	[-e pattern] [-f file] [--binary-files=value] [--color=when]
	[--context[=num]] [--directories=action] [--label] [--line-buffered]
	[--null] [pattern] [file ...]

which results in the creation of an empty cache.

Setup

Directory structure:

nix-direnv-test taylor$ ls -la
total 16
drwxr-xr-x   5 taylor  staff   160 Mar 10 11:26 .
drwxr-xr-x  55 taylor  staff  1760 Mar 10 11:24 ..
drwxr-xr-x   3 taylor  staff    96 Mar 10 11:26 .direnv
-rw-r--r--   1 taylor  staff     8 Mar 10 11:24 .envrc
-rw-r--r--   1 taylor  staff    87 Mar 10 11:25 shell.nix

.direnv:

use nix

shell.nix:

let pkgs = import <nixpkgs> { };
in pkgs.mkShell {
    buildInputs = [ pkgs.hello ];
}

Steps to reproduce

nix-direnv-test taylor$ direnv allow
direnv: loading ~/Source/nix-direnv-test/.envrc
direnv: using nix
usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
	[-e pattern] [-f file] [--binary-files=value] [--color=when]
	[--context[=num]] [--directories=action] [--label] [--line-buffered]
	[--null] [pattern] [file ...]

Fix

The issue seems to be that nix-direnv is trying to invoke the macOS builtin BSD-grep at /usr/bin/grep, which for one reason or another doesn't like (in direnvrc):

    tmp=$(nix-shell --show-trace --pure "$@" --run "$dump_cmd" \
              | grep -oP '(?<=_____direnv_____).*')

I haven't actually spent any time trying to figure out WHY that line is problematic with the builtin grep, but I managed to sort this out by installing nix-direnv with the following derivation, which explicitely uses nixpkgs.gnugrep

{ bash, fetchFromGitHub, gnugrep, stdenv }:

stdenv.mkDerivation {
  name = "nix-direnv";

  src = fetchFromGitHub {
    owner = "nix-community";
    repo = "nix-direnv";
    rev = "81d3f5083ae68aec5d9f6fc9eebac57c82a9d280";
    sha256 = "067493hbsij59bvaqi38iybacqbzwx876dvdm651b5mn3zs3h42c";
  };

  phases = [ "unpackPhase" "patchPhase" "installPhase" ];

  prePatch = ''
    substituteInPlace direnvrc --replace "/usr/bin/env bash" "${bash}/bin/bash"
    substituteInPlace direnvrc --replace "grep" "${gnugrep}/bin/grep"
  '';
  
  installPhase = ''
    mkdir -p $out/share/nix-direnv
    cp -rv ./* $out/share/nix-direnv
  '';
}
@Mic92
Copy link
Member

Mic92 commented Mar 11, 2020

Interesting. I might be able to replace this invocation with an awk call that should be portable. Do you now if nix show-derivation $out | grep -E -o -m1 '/nix/store/.*.drv' is problematic with bsdgrep?

@Mic92
Copy link
Member

Mic92 commented Mar 11, 2020

@zimbatm does direnv on macOS uses the builtin bash btw? Because that might also cause problems.

@Mic92
Copy link
Member

Mic92 commented Mar 11, 2020

Replacing /usr/bin/env bash should not have any effect because direnv executes the bash version itself.

@zimbatm
Copy link
Member

zimbatm commented Mar 11, 2020

yes... direnv uses whatever bash is in the environment

@Mic92
Copy link
Member

Mic92 commented Mar 11, 2020

In that case I should suggest people to install direnv via nixpkgs or get a newer version via $packagemanager. I have not tested bash compatibility though.

@zimbatm
Copy link
Member

zimbatm commented Mar 11, 2020

The one that comes from nixpkgs is bound to the nixpkgs bash at compile time.

@zimbatm
Copy link
Member

zimbatm commented Mar 11, 2020

Anyone using Homebrew should also brew install bash

@Mic92
Copy link
Member

Mic92 commented Mar 11, 2020

It would be great if direnv would have something like a .direnv.d or even environment variables to load third party plugins. I think it is time to package nix-direnv in nixpkgs itself.

@Mic92
Copy link
Member

Mic92 commented Mar 11, 2020

Can we convince the homebrew maintainers to also hard code bash? https://github.com/Homebrew/homebrew-core/blob/master/Formula/direnv.rb#L18

@zimbatm
Copy link
Member

zimbatm commented Mar 11, 2020

@Mic92 direnv looks in ~/.config/direnv/lib/*.sh for additional libs. (since v2.21)

@Mic92
Copy link
Member

Mic92 commented Mar 11, 2020

Ok. This would not play well with vanilla nix or most other system package manager in that regard, but a home-manager module could work.

tviti added a commit to tviti/nix-cfg that referenced this issue Mar 25, 2020
We need to make sure that nix-direnv is explicitly using utils from
nixpkgs, since the operating system versions of the utils may not play
nicely (e.g. BSD-grep on macOS won't work w/ nix-direnv).

See also: nix-community/nix-direnv#3
tviti added a commit to tviti/nix-cfg that referenced this issue Mar 25, 2020
We need to make sure that nix-direnv is explicitly using utils from
nixpkgs, since the operating system versions of the utils may not play
nicely (e.g. BSD-grep on macOS won't work w/ nix-direnv).

See also: nix-community/nix-direnv#3
@Mic92
Copy link
Member

Mic92 commented May 11, 2020

Nix-direnv can now be installed via nix. This version will hard code nix as well as gnugrep.

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

4 participants
@zimbatm @Mic92 @tviti and others