Nix-shell, nix-build and heredoc


#1

I have seen this snippet on the Internet

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

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=https://github.com/NixOS/nixpkgs/archive/18.09.tar.gz <<'EOF'
let
  inherit (import <nixpkgs> {}) lib buildGoPackage fetchgit;
in
buildGoPackage rec {
   blablabla
}
EOF

#2

Have you tried the following?

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

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


#3

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


#4

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).


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

fails with

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

#6

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:

~Will


#7

#8

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