Nix-shell, nix-build and heredoc

I have seen this snippet on the Internet

nix-shell -p git <<'HEREDOC'
  git status

This does not work for me.

Was it for an older version of Nix or merely a wish, implementing of this feature seems useful

Especially with nix-instantiate and nix-build, for writing quick bash scripts which require more flexibility than nix-shell provides

nix-build -I nixpkgs= <<'EOF'
  inherit (import <nixpkgs> {}) lib buildGoPackage fetchgit;
buildGoPackage rec {

Have you tried the following?

nix-shell -p git --run "$SHELL" <<HEREDOC
  git status

Or equivalently: echo git status | nix-shell -p git --run "$SHELL"

1 Like

No, I did not try to fix that @peel’s example, it was just a source of inspiration for feeding nix-build with sources.

1 Like

Mhm, yeah, feeding the Nix expression via stdin to nix-build would be quite helpful in some occasions, especially when writing wrappers (although one could still use a shell variable).

nix-build /dev/stdin <<'EOF'
  inherit (import <nixpkgs> {}) lib buildGoPackage;
in buildGoPackage rec {

fails with

error: getting status of '/tmp/sh-thd.d0byhc (deleted)': No such file or directory 

Might be a better way, but at least w/zsh
you can use =( to do this sort of thing:

$ nix-build =(<<EOF                                     17:22:31 on 19-02-05
cmdsubst heredoc> let
cmdsubst heredoc> foo = import <nixpkgs> {};
cmdsubst heredoc> bar = foo.bash;
cmdsubst heredoc> in
cmdsubst heredoc> bar
cmdsubst heredoc> EOF
cmdsubst> )

The same with <(<<EOF gives an error like yours.

I was looking into this for another program
and found this in the zsh man pages…

There might be a simpler way xD.

And by “might” I mean “surely, please” :smiley:


Didn’t look at the source code previously, but it turns out that at least for nix-build, this already works (see also my comment) by using nix-build -.