@@ -128,7 +128,7 @@ The following shows a *bare bones* flake using the
128128In general, a derivation is a declarative description of how to run an
129129* executable (builder)* on a set of * inputs (input derivations aka dependencies)*
130130to produce a set of * outputs (store paths)* .
131- ``` nix
131+ ``` nixos
132132{{#include nix/bare-bones-derivation/flake.nix}}
133133```
134134Understanding this example is crucial as any abstractions such as the
@@ -171,7 +171,7 @@ Alternatively there is `stdenvNoCC.mkDerivation` which provides an environment
171171without a C compiler.
172172
173173The example show a simple build and installation of a C file.
174- ` ` ` nix
174+ ` ` ` nixos
175175{{# include nix/stdenv/flake.nix}}
176176` ` `
177177> Use [` NIX_DEBUG=[0-7]` ][nixpkgs-nixdebug] to enable ` stdenv` debug logging.
@@ -211,7 +211,7 @@ an environment without a C compiler.
211211The example shows an environment with the ` zig` compiler and ` zls` , the zig lsp
212212server. Running ` nix develop` will drop into a shell where these packages are
213213available.
214- ` ` ` nix
214+ ` ` ` nixos
215215{{# include nix/devshell/flake.nix}}
216216` ` `
217217
@@ -248,7 +248,7 @@ is the [`nixpkgs.lib.genAttrs`][nixpkgs-genattrs] function, which allows to
248248create an attribute set from a list of keys and a lambda function which computes
249249the values of the attribute set.
250250
251- ` ` ` nix
251+ ` ` ` nixos
252252nix-repl> pkgs.lib.genAttrs [ " a" " b" ] (arg: " some-value-${arg} " )
253253{
254254 a = " some-value-a" ;
@@ -258,15 +258,24 @@ nix-repl> pkgs.lib.genAttrs [ "a" "b" ] (arg: "some-value-${arg}")
258258
259259This can be used to build a flake to support multiple systems. The following
260260shows a small example for two systems which defines a dev shell and a formatter.
261- ` ` ` nix
261+ ` ` ` nixos
262262{{# include nix/for-systems/flake.nix}}
263263` ` `
264264
265+ # ## a flake for multiple systems from scratch
266+ The following shows how to build the ` nixpkgs.lib.genAttrs` utility from scratch
267+ with nix ` builtins` . This mainly serves as learning but may come in handy when
268+ doing work without ` nixpkgs` .
269+
270+ ` ` ` nixos
271+ {{# include nix/for-systems-from-scratch/flake.nix}}
272+ ` ` `
273+
265274# # nix lang basics
266275Nix is a functional language, where everything is an expression.
267276The following shows enough nix lang to come quite far.
268277
269- ` ` ` nix
278+ ` ` ` nixos
270279$ nix repl
271280
272281nix-repl> 1 + 2
@@ -376,6 +385,26 @@ nix-repl> myfn { inherit x ; y = 2; }
376385> ` foo.nix` , which returns the function (the value), which is then
377386> called with the attribute set as input.
378387
388+ # ## useful builtins
389+
390+ ` ` ` nixos
391+ # The "map" builtin takes a function and a list, then calls the function on
392+ # each list value and replaces the value with the result of the function.
393+ nix-repl> builtins.map (val: " some val ${val} " ) [" a" " b" ]
394+ [
395+ " some val a"
396+ " some val b"
397+ ]
398+
399+ # The "listToAttrs" builtin takes a list of name / value pairs (attribute sets)
400+ # and turns the list into a attribute set build from the name / value pairs.
401+ nix-repl> builtins.listToAttrs [{ name = " A" ; value = " a" ; } { name = " B" ; value = " b" ; }]
402+ {
403+ A = " a" ;
404+ B = " b" ;
405+ }
406+ ` ` `
407+
379408# ## access flakes in the repl
380409
381410` ` `
0 commit comments