Stuck on exercise in Nix Pills chapter 8

Hey!

I am running on NixOS 21.05.

I am stuck on completing the excercise in Nix Pills Chapter 8.3.

Exercise: Complete the new builder.sh by adding $baseInputs in the for loop together with $buildInputs . As you noticed, we passed that new variable in the derivation. Instead of merging buildInputs with the base ones, we prefer to preserve buildInputs as seen by the caller, so we keep them separated. Just a matter of choice.

I have limited bash experience, so I get stuck here. I suppose some bash knowledge is good to have, but I would rather get on with learning nix :grinning_face_with_smiling_eyes: No idea how to do this, and the following Nix Pill seems to depend on it, where I get this message running the nix-store -r command:

❯ nix-store -r /nix/store/7rmv83d9lwhd4z37479irw54dlsg0qgg-hello.drv
this derivation will be built:
  /nix/store/7rmv83d9lwhd4z37479irw54dlsg0qgg-hello.drv
building '/nix/store/7rmv83d9lwhd4z37479irw54dlsg0qgg-hello.drv'...
/nix/store/6k3q6g0w8pinmmmqy576xv434y3fsqjz-builder.sh: line 7: tar: No such file or director

Is it possible this could be related? Struggling to see if I have made any typos, but tried to just copy paste and I am getting the same results.

Here are the relevant files:

builder.sh (as mentioned, I didn’t know excactly what to do, so I left it as is):

set -e
unset PATH
for p in $buildInputs; do
  export PATH=$p/bin${PATH:+:}$PATH
done

tar -xf $src

for d in *; do
  if [ -d "$d" ]; then
    cd "$d"
    break
  fi
done

./configure --prefix=$out
make
make install

autotools.nix:

pkgs: attrs:
with pkgs;
let defaultAttrs = {
  builder = "${bash}/bin/bash";
  args = [ ./builder.sh ];
  baseInputs = [ gnutar gzip gnumake gcc binutils-unwrapped coreutils gawk gnused gnugrep ];
  buildInputs = [ ];
  system = builtins.currentSystem;
};
in
derivation (defaultAttrs // attrs)

hello.nix:

let
  pkgs = import <nixpkgs> { };
  mkDerivation = import ./autotools.nix pkgs;
in
mkDerivation {
  name = "hello";
  src = ./hello-2.10.tar.gz;
}

And as mentioned, I run this command per 9.3

nix-instantiate hello.nix

with the following output

warning: you did not specify '--add-root'; the result might be removed by the garbage collector
/nix/store/7rmv83d9lwhd4z37479irw54dlsg0qgg-hello.drv

So far so good.

But when I run

nix-store -r /nix/store/7rmv83d9lwhd4z37479irw54dlsg0qgg-hello.drv

I get this:

this derivation will be built:
  /nix/store/7rmv83d9lwhd4z37479irw54dlsg0qgg-hello.drv
building '/nix/store/7rmv83d9lwhd4z37479irw54dlsg0qgg-hello.drv'...
/nix/store/6k3q6g0w8pinmmmqy576xv434y3fsqjz-builder.sh: line 7: tar: No such file or directory
error: builder for '/nix/store/7rmv83d9lwhd4z37479irw54dlsg0qgg-hello.drv' failed with exit code 127;
       last 1 log lines:
       > /nix/store/6k3q6g0w8pinmmmqy576xv434y3fsqjz-builder.sh: line 7: tar: No such file or directory
       For full logs, run 'nix log /nix/store/7rmv83d9lwhd4z37479irw54dlsg0qgg-hello.drv'.

I do have the hello-2.10.tar.gz file in the same folder, downloaded from http://ftp.gnu.org/gnu/hello/hello-2.10.tar.gz. In earlier parts of the tutorial, I was successful in unpacking it.

Oh, I looked into chapter 10, and it gives away the solution there:

for p in $baseInputs $buildInputs; do
  export PATH=$p/bin${PATH:+:}$PATH
done

Simple enough if you know a bit of bash I guess :slight_smile: In my opinion as a beginner, this solution should be given right away instead of making it a bash exercise that can confuse a beginner :blush:

It took you just 36 minutes to cheat the exercise :wink: . More seriously, the pills are mostly a journey on building software with Nix that clearly expects the user to have some background on getting around on a unix system and builds upon that to explain Nix’s peculiarities… It’s an approach to Nix. By the way, it makes you exercise with bash because the stdenv builder script is written in Bash, so if you’re gonna use it, it may be good to know it a bit. As you have learned, if you feel stranded, just ask!

2 Likes

Thanks! That is noted :slight_smile: I have learned lots from it so far regardless.

You are probably right in that I need deeper UNIX and bash knowledge to grasp it all fully.