Nix pill 7.2 unexpected behavior


In Nix pill 7.2, the text suggests that building the script

declare -xp
echo foo > $out

with the derivation

d = derivation { name = "foo"; builder = "${bash}/bin/bash"; args = [ ./ ]; system = builtins.currentSystem; }

will produce an output :

these derivations will be built: /nix/store/i76pr1cz0za3i9r6xq518bqqvd2raspw-foo.drv building '/nix/store/i76pr1cz0za3i9r6xq518bqqvd2raspw-foo.drv'... declare -x HOME="/homeless-shelter" declare -x NIX_BUILD_CORES="4" declare -x NIX_BUILD_TOP="/tmp/nix-build-foo.drv-0" declare -x NIX_LOG_FD="2" declare -x NIX_STORE="/nix/store" declare -x OLDPWD declare -x PATH="/path-not-set" declare -x PWD="/tmp/nix-build-foo.drv-0" declare -x SHLVL="1" declare -x TEMP="/tmp/nix-build-foo.drv-0" declare -x TEMPDIR="/tmp/nix-build-foo.drv-0" declare -x TMP="/tmp/nix-build-foo.drv-0" declare -x TMPDIR="/tmp/nix-build-foo.drv-0" declare -x builder="/nix/store/q1g0rl8zfmz7r371fp5p42p4acmv297d-bash-4.4-p19/bin/bash" declare -x name="foo" declare -x out="/nix/store/gczb4qrag22harvv693wwnflqy7lx5pb-foo" declare -x system="x86_64-linux" warning: you did not specify '--add-root'; the result might be removed by the garbage collector /nix/store/gczb4qrag22harvv693wwnflqy7lx5pb-foo this derivation produced the following outputs: out -> /nix/store/gczb4qrag22harvv693wwnflqy7lx5pb-foo

When I execute the steps in the instructions, I just get a message

this derivation produced the following results
   out -> /nix/store/<hash>-foo

Where the derivation just contains foo.

I’m curious why

  1. The output I get differs from what the instructions suggest it should be, and
  2. Why declare -xp is used at all in the pill. It doesn’t do anything with the declared items, and although I’m a total bash pleb, they don’t seem to be accessible in the builder script. Replacing echo foo > $out with echo $XDG_SEAT > $out or some other variable in the output of declare -xp gives me a blank output after building.

Thanks for any help.

A few things are happening:

  • Your version of Nix is newer than what is presented in the Nix Pill. The new version hides some of the output, but the same thing is happening behind the scenes. Then when you run it multiple times, it no longer need to build anything, and just uses cache.

  • The point of the “declare -xp” was to print out all the environment variables. But the new version of Nix hides build outputs by default. If you want to view those outputs, you can run nix log /nix/store/<hash>-foo

  • The exact hash you see will be different because you are probably using a different version of Nixpkgs than the Nix Pills author did for the example. This means you’ll be using a different bash and stdenv version. For the tutorial this is fine, but in production many people “pin” the Nixpkgs version to get reproducibility.

1 Like

On my machine, the build log output is:

declare -x HOME="/homeless-shelter"
declare -x NIX_BUILD_CORES="0"
declare -x NIX_BUILD_TOP="/build"
declare -x NIX_LOG_FD="2"
declare -x NIX_STORE="/nix/store"
declare -x OLDPWD
declare -x PATH="/path-not-set"
declare -x PWD="/build"
declare -x SHLVL="1"
declare -x TEMP="/build"
declare -x TEMPDIR="/build"
declare -x TERM="xterm-256color"
declare -x TMP="/build"
declare -x TMPDIR="/build"
declare -x builder="/nix/store/81wybawvkr95c7j8gj5ab3y740mq1fli-bash-4.4-p23/bin/bash"
declare -x name="foo"
declare -x out="/nix/store/nzag21m3rdq6i92gfav762fsf318sr5c-foo"
declare -x system="x86_64-linux"

Note that XDG_SEAT is not defined. So your experiment makes sense.

1 Like

@tomberek, I got some more time to go over this today and wanted to properly thank you for taking the time to help me out. After going over it again with the extra information you provided I was able to understand it much better; I filed a PR on the pills repo to get that info included.