To call a Nix function that uses set destructuring, you need to pass it a set with exactly the keys it requires, no more and no less:
nix-repl> ({ a }: a) { a = 4; b = 5; }
error: anonymous function at (string):1:2 called with unexpected argument ‘b’, at (string):1:1
The exception to this is if the function's argument list contains an ellipsis at the end:
nix-repl> ({ a, ... }: a) { a = 4; b = 5; }
4
However, most of the packages in nixpkgs consist of a default.nix
file containing a function which is not defined with this ellipsis. Yet, somehow when you use callPackage
, it manages to call these functions and pass them only the arguments that they need. How is this implemented?
There is a reflection primop, that can deconstruct function argument:
callPackage
then iterates over those attribute names, fetches required packages and constucts the attrset of packages, that is fed later to called function.Here's a simple example:
To browse Nix primops, go to 15.5. Built-in Functions in the Nix manual (or see the docs of the unstable branch).