NixOS / nix-pills

Creative Commons Attribution Share Alike 4.0 International
392 stars 117 forks source link

Nix Pills - error in example code - Chapter 8 - Generic Builders #225

Open D3vil0p3r opened 10 months ago

D3vil0p3r commented 10 months ago

Introduce your stance In Nix Pills Chapter 8 - Generic Builders there is an error inside the example code.

Describe the issue builder.sh is shown to be:

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

and buildInputs is empty and defined in `autotools.nix shown as:

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

When nix-build hello.nix is run, it produces the error:

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

It occurs because PATH variable defined in autotools.nix stores paths contained in baseInputs instead of buildInputs so it cannot find the tar executable coming from gnutar.

To fix this either in the for loop of builder.sh ALSO baseInputs along with buildInputs or define gnutar inside buildInputs = []; of autotools.nix file. Be free to choose the most elegant approach.

Page links https://nixos.org/guides/nix-pills/generic-builders

Additional context Add any other context about the problem here.

henrik-ch commented 9 months ago

I think that the problem with the code above is that the suggested exercise hasn't been completed yet - please see this section of the page:

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.

please see here below a builder.sh modified accordingly.

set -e
unset PATH
for p in $baseInputs $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

and hello.nix

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

and autotools.nix - I think that this is exactly as you have it but just to make sure:

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

running nix-build hello.nix with the files as above, I think that the example should work.

I think that it could be helpful to many people not to leave this exercise as an open point with no listed solution. I am thinking of adding some type of accordion/tip section to make sure that everyone converges on the solution, but I don't know how that can be achieved yet.

nixos-discourse commented 9 months ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2023-12-11-documentation-team-meeting-notes-99/36866/1

YievCkim commented 6 months ago

So it's not:

Just a matter of choice.