So far you have built everything by hand. Time to stop doing that.
Nix comes with a small toolbox of builtin functions,
reachable through builtins with nothing to import and no nixpkgs in sight:
builtins.typeOf hands you the type of a value, as a
string.
Make the inspector return "list".
Sounds pointless until an unfamiliar expression hands you something that is not at all what you expected, which happens more than anyone admits.
builtins.length counts what is in a list. No surprises here.
Add one tool so the result becomes "tools:3".
builtins.head gives you the first item. builtins.tail gives you everything
that isn’t the first item.
head is the front of the queue and tail is everybody still waiting in it.
One catch, and it will bite you eventually: both need at least one item, so
builtins.head [] and builtins.tail [] simply fail. Check items != [] first
whenever a list might be empty.
builtins.filter runs a function over every item and keeps the ones that answer
true.
Keep only money values greater than 50.
builtins.map also runs a function over every item, but it keeps every result.
Double every amount of money.
So filter decides which items survive, and map changes the items themselves.
You’ll reach for these two constantly.
Lists are only half of it. builtins.attrNames gives you the
attribute names of a set, always in alphabetical
order.
Add the missing place so the result contains castle, gate, and tower.
builtins.attrValues gives you the values instead.
The names get sorted first, so the values arrive in that same alphabetical order. Handy, as long as you never mistake it for the order you wrote them in.
Interpolation only accepts strings, and sooner or later you will want to drop a
number into one. builtins.toString, which you can also just write as toString,
converts simple values into text.
Produce the string "balance=200".
Time to put it together. Everything below is builtins, functions, lists and sets from the lessons you’ve already done, and there is no secret answer to guess at, so break it however you like.
- Builtins belong to the language, not nixpkgs.
typeOfinspects a value’s kind.length,head,tail,filter, andmapwork with lists.attrNamesandattrValuesinspect attribute sets.toStringprepares simple values for interpolation.
Nixpkgs brings a far bigger lib toolbox along later. These builtins are the
small, reliable set that is available absolutely everywhere.


Share your thoughts