# Nix functions

All functions in Nix are anonymous, and can be specified in any of the following ways.

## Single identifier

A function written this way will match any argument.

x: !x


This function negates of the boolean type.

x: y: x + y


This function takes a number or a string and produces a function which takes a value of the same type and either adds or concatenates them, respectively.

Functions written this way can be partially applied.

let
concat = x: y: x + y
in
map (concat "foo") [ "bar" "baz" "quux" ]


This evaluates to ["foobar" "foobaz" "fooquux"]. We’ve named our function by binding it to an attribute called concat.

## Set pattern

A function written this way will match only a set containing the named attributes.

{ x, y }: x + y


Additional arguments can be allowed with ....

{ x, y, ... }: x + y


This is equivalent to the first definition, but can take any set containing x and y attributes.

Default arguments can be set using ?.

{ x ? "hello", y, ... }: x + y


Thus, only a set containing y is required, but we can accept a set with any other attributes. If x is unset, it will default to "hello".

## As-pattern

An as-pattern uses a name to refer to the entire set passed to the function.

let
func = args@{ x, y, z, ... }: z + y + x + args.a;
in
func { x = 1; y = 2; z = 3; a = 4; }


This evaluates to 10.

We can also equivalently write

{ x, y, z, ... } @ args: z + y + x + args.a