Skip to content

Instantly share code, notes, and snippets.

@dacr
Created February 1, 2025 11:04
nix data types / published by https://github.com/dacr/code-examples-manager #7e8ab042-cffa-4d78-bf37-21fc5bc024d6/99277c1e7ab0c0cafc426e32760098aa71ee1951
## summary : nix data types
## keywords : nix, data-types
## publish : gist
## authors : David Crosson
## license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
## id : 7e8ab042-cffa-4d78-bf37-21fc5bc024d6
## created-on : 2025-02-01T10:49:21+01:00
## managed-by : https://github.com/dacr/code-examples-manager
## run-with : nix eval --file $file
#### run-with : nix-instantiate --eval $file
let
a_int = 2;
a_float = 40.0;
a_null = null;
a_bool = true;
a_string = "truc";
a_multiline_string = ''
truc1
truc2
'';
a_list = [ 42 "something" false ];
an_attribute_set = {
a = "blah";
b = 42;
c = true;
d = { d0 = 0; d1 = 1; d2 = 2; };
};
a_recursive_attribute_set = rec {
one = 1;
two = one + 1;
three = two + 1;
};
a_lambda = x: y: x+y; # currying
an_other_lambda = {x, y?0}: x+y; # With a default value for an attribute
an_other_lambda_again = {x, y?0,...}: x+y; # We don't care of any extra attribute
in {
computed =
a_int +
a_float +
an_attribute_set.d.d0 +
(a_lambda 1 2) +
(an_other_lambda {x=1;}) +
(an_other_lambda_again {x=1;gloups=42;}) +
( (x: x * 2) 10) + # direct lambda call
( (attrset: attrset.v * 10) {v = 5;}) + # lambda with parameter taken into account as an attribute set
( ( {x, y}: x + y) {x=1; y=2;} ) # destructuring
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment