biglybt: 3.5.0.0 -> 3.6.0.0
[NixPkgs.git] / nixos / doc / manual / configuration / abstractions.section.md
blob5bc44aa72245745576c3a3bea8044f74fb19d670
1 # Abstractions {#sec-module-abstractions}
3 If you find yourself repeating yourself over and over, it’s time to abstract. Take, for instance, this Apache HTTP Server configuration:
5 ```nix
7   services.httpd.virtualHosts =
8     { "blog.example.org" = {
9         documentRoot = "/webroot/blog.example.org";
10         adminAddr = "alice@example.org";
11         forceSSL = true;
12         enableACME = true;
13         enablePHP = true;
14       };
15       "wiki.example.org" = {
16         documentRoot = "/webroot/wiki.example.org";
17         adminAddr = "alice@example.org";
18         forceSSL = true;
19         enableACME = true;
20         enablePHP = true;
21       };
22     };
24 ```
26 It defines two virtual hosts with nearly identical configuration; the only difference is the document root directories. To prevent this duplication, we can use a `let`:
27 ```nix
28 let
29   commonConfig =
30     { adminAddr = "alice@example.org";
31       forceSSL = true;
32       enableACME = true;
33     };
36   services.httpd.virtualHosts =
37     { "blog.example.org" = (commonConfig // { documentRoot = "/webroot/blog.example.org"; });
38       "wiki.example.org" = (commonConfig // { documentRoot = "/webroot/wiki.example.com"; });
39     };
41 ```
43 The `let commonConfig = ...` defines a variable named `commonConfig`. The `//` operator merges two attribute sets, so the configuration of the second virtual host is the set `commonConfig` extended with the document root option.
45 You can write a `let` wherever an expression is allowed. Thus, you also could have written:
47 ```nix
49   services.httpd.virtualHosts =
50     let commonConfig = { /* ... */ }; in
51     { "blog.example.org" = (commonConfig // { /* ... */ });
52       "wiki.example.org" = (commonConfig // { /* ... */ });
53     };
55 ```
57 but not `{ let commonConfig = ...; in ...; }` since attributes (as opposed to attribute values) are not expressions.
59 **Functions** provide another method of abstraction. For instance, suppose that we want to generate lots of different virtual hosts, all with identical configuration except for the document root. This can be done as follows:
61 ```nix
63   services.httpd.virtualHosts =
64     let
65       makeVirtualHost = webroot:
66         { documentRoot = webroot;
67           adminAddr = "alice@example.org";
68           forceSSL = true;
69           enableACME = true;
70         };
71     in
72       { "example.org" = (makeVirtualHost "/webroot/example.org");
73         "example.com" = (makeVirtualHost "/webroot/example.com");
74         "example.gov" = (makeVirtualHost "/webroot/example.gov");
75         "example.nl" = (makeVirtualHost "/webroot/example.nl");
76       };
78 ```
80 Here, `makeVirtualHost` is a function that takes a single argument `webroot` and returns the configuration for a virtual host. That function is then called for several names to produce the list of virtual host configurations.