Baby step #2 - using tree in the ./builder script


I’m trying to take the next 3 baby steps but none worked.
Environment: ubuntu 16.4

Lets take the the one I want most 1st.
I’m trying to use tree in my build script.
I added it to my system using
nix-env -i tree
and it works.

which tree
mral@sunburst20171202:/nix/store/4c4qq58y8yyj3dyhzv3737km0r7hbxpp-atrTest-0.0.1$ whereis tree
tree: /nix/store/p0cb4i0kwvkajxmvrzwfdsh5rzjrgig7-user-environment/bin/tree /usr/share/man/man3/tree.3.gz

I can run tree from a standard bash shell using unbutu’s bash shell and
from the nix-shell.
When I try to build my package is fails and I’m sure its because of the pure
build stuff that nix does, clearing path etc.
What I dont know is how to declare tree as a depencency.

I’ve tried all kinds of incantations following hints that I’ve seen in the manual.
Various combination of import, using { tree} or {} all in various places
and none have worked and none of the error messages gave me enough information
to know what I was doing wrong.
The nix expression is:

  pkgs = import <nixpkgs> {};

pkgs.stdenv.mkDerivation rec {
		name = "atrTest-0.0.1";
		#args = [" -v "];
		builder = ./;
		src = ./makefile;
		extraStuff = "extra stuff${name} for shell";
		postInstall = ''
	mkdir ''${out}/Assets
	echo "all done now running postInstall for ${name}" > ''${out}/Assets/log.txt

The build script is:
source $stdenv/setup

echo "hi from $0, pwd=$(pwd), myName=${extraStuff}, arg=$1"
mkdir -p ${out}/atrnix
tree  > ${out}/fileList.xml
echo "hi from nix-build" > ${out}/atrnix/hello.txt
echo "${postInstall}" >> ${out}/postSetup

I’m invoking it from a nix-shell as:

 nix-build -K atrTest.nix
these derivations will be built:
building '/nix/store/9jnk7i5nr5i1ndzb3sja399xnfv4ykkp-atrTest-0.0.1.drv'...
hi from /nix/store/, pwd=/tmp/nix-build-atrTest-0.0.1.drv-5, myName=extra stuffatrTest-0.0.1 for shell, arg=
/nix/store/ line 6: tree: command not found
note: keeping build directory '/tmp/nix-build-atrTest-0.0.1.drv-5'
builder for '/nix/store/9jnk7i5nr5i1ndzb3sja399xnfv4ykkp-atrTest-0.0.1.drv' failed with exit code 127
error: build of '/nix/store/9jnk7i5nr5i1ndzb3sja399xnfv4ykkp-atrTest-0.0.1.drv' failed

I can see that it cann’t find the tree command but
how do I declare it as a dependency so it will be found?

I’ll start a seperate thread for the args problem, its been commented out for now.
I’ll start a seperate thread for the postInstall, its showing up as env variable
instead of being executed like I expected.


You need to add buildInputs = [pkgs.tree]; to the derivation. Technically you should use nativeBuildInputs because you are only using tree at build time.


Thanks that worked. I extended my test by using md5sum and to my surprise it worked without adding it to the buildInputs attribute list. Is there a list or way to find out what the set of utilities that are present by default and then I will know that if I’m using something not on the list I have to add it to buildInputs.

I’m going to think about your refinement about buildtime vs runtime. I know what that means but I’m not far enough along the learning curve to really know why thats important.


I like your style. Please continue posting these baby steps -threads, it’s interesting :slight_smile:

I’ve been using nixos for a year now and my biggest complaint is the unstructured and partial nature of documentation. Especially a proper API listing that would list all the functions and their arguments etc. This would make a fine project…


Thanks @ilikeavocadoes for the encouragement. Its nice to know I’m adding something valuable.