1# Options for Program Settings {#sec-settings-options}
2
3Many programs have configuration files where program-specific settings
4can be declared. File formats can be separated into two categories:
5
6- Nix-representable ones: These can trivially be mapped to a subset of
7 Nix syntax. E.g. JSON is an example, since its values like
8 `{"foo":{"bar":10}}` can be mapped directly to Nix:
9 `{ foo = { bar = 10; }; }`. Other examples are INI, YAML and TOML.
10 The following section explains the convention for these settings.
11
12- Non-nix-representable ones: These can\'t be trivially mapped to a
13 subset of Nix syntax. Most generic programming languages are in this
14 group, e.g. bash, since the statement `if true; then echo hi; fi`
15 doesn\'t have a trivial representation in Nix.
16
17 Currently there are no fixed conventions for these, but it is common
18 to have a `configFile` option for setting the configuration file
19 path directly. The default value of `configFile` can be an
20 auto-generated file, with convenient options for controlling the
21 contents. For example an option of type `attrsOf str` can be used
22 for representing environment variables which generates a section
23 like `export FOO="foo"`. Often it can also be useful to also include
24 an `extraConfig` option of type `lines` to allow arbitrary text
25 after the autogenerated part of the file.
26
27## Nix-representable Formats (JSON, YAML, TOML, INI, \...) {#sec-settings-nix-representable}
28
29By convention, formats like this are handled with a generic `settings`
30option, representing the full program configuration as a Nix value. The
31type of this option should represent the format. The most common formats
32have a predefined type and string generator already declared under
33`pkgs.formats`:
34
35`pkgs.formats.javaProperties` { *`comment`* ? `"Generated with Nix"` }
36
37: A function taking an attribute set with values
38
39 `comment`
40
41 : A string to put at the start of the
42 file in a comment. It can have multiple
43 lines.
44
45 It returns the `type`: `attrsOf str` and a function
46 `generate` to build a Java `.properties` file, taking
47 care of the correct escaping, etc.
48
49`pkgs.formats.json` { }
50
51: A function taking an empty attribute set (for future extensibility)
52 and returning a set with JSON-specific attributes `type` and
53 `generate` as specified [below](#pkgs-formats-result).
54
55`pkgs.formats.yaml` { }
56
57: A function taking an empty attribute set (for future extensibility)
58 and returning a set with YAML-specific attributes `type` and
59 `generate` as specified [below](#pkgs-formats-result).
60
61`pkgs.formats.ini` { *`listsAsDuplicateKeys`* ? false, *`listToValue`* ? null, \... }
62
63: A function taking an attribute set with values
64
65 `listsAsDuplicateKeys`
66
67 : A boolean for controlling whether list values can be used to
68 represent duplicate INI keys
69
70 `listToValue`
71
72 : A function for turning a list of values into a single value.
73
74 It returns a set with INI-specific attributes `type` and `generate`
75 as specified [below](#pkgs-formats-result).
76
77`pkgs.formats.toml` { }
78
79: A function taking an empty attribute set (for future extensibility)
80 and returning a set with TOML-specific attributes `type` and
81 `generate` as specified [below](#pkgs-formats-result).
82
83`pkgs.formats.elixirConf { elixir ? pkgs.elixir }`
84
85: A function taking an attribute set with values
86
87 `elixir`
88
89 : The Elixir package which will be used to format the generated output
90
91 It returns a set with Elixir-Config-specific attributes `type`, `lib`, and
92 `generate` as specified [below](#pkgs-formats-result).
93
94 The `lib` attribute contains functions to be used in settings, for
95 generating special Elixir values:
96
97 `mkRaw elixirCode`
98
99 : Outputs the given string as raw Elixir code
100
101 `mkGetEnv { envVariable, fallback ? null }`
102
103 : Makes the configuration fetch an environment variable at runtime
104
105 `mkAtom atom`
106
107 : Outputs the given string as an Elixir atom, instead of the default
108 Elixir binary string. Note: lowercase atoms still needs to be prefixed
109 with `:`
110
111 `mkTuple array`
112
113 : Outputs the given array as an Elixir tuple, instead of the default
114 Elixir list
115
116 `mkMap attrset`
117
118 : Outputs the given attribute set as an Elixir map, instead of the
119 default Elixir keyword list
120
121
122::: {#pkgs-formats-result}
123These functions all return an attribute set with these values:
124:::
125
126`type`
127
128: A module system type representing a value of the format
129
130`lib`
131
132: Utility functions for convenience, or special interactions with the format.
133 This attribute is optional. It may contain inside a `types` attribute
134 containing types specific to this format.
135
136`generate` *`filename jsonValue`*
137
138: A function that can render a value of the format to a file. Returns
139 a file path.
140
141 ::: {.note}
142 This function puts the value contents in the Nix store. So this
143 should be avoided for secrets.
144 :::
145
146::: {#ex-settings-nix-representable .example}
147::: {.title}
148**Example: Module with conventional `settings` option**
149:::
150The following shows a module for an example program that uses a JSON
151configuration file. It demonstrates how above values can be used, along
152with some other related best practices. See the comments for
153explanations.
154
155```nix
156{ options, config, lib, pkgs, ... }:
157let
158 cfg = config.services.foo;
159 # Define the settings format used for this program
160 settingsFormat = pkgs.formats.json {};
161in {
162
163 options.services.foo = {
164 enable = lib.mkEnableOption "foo service";
165
166 settings = lib.mkOption {
167 # Setting this type allows for correct merging behavior
168 type = settingsFormat.type;
169 default = {};
170 description = ''
171 Configuration for foo, see
172 <link xlink:href="https://example.com/docs/foo"/>
173 for supported settings.
174 '';
175 };
176 };
177
178 config = lib.mkIf cfg.enable {
179 # We can assign some default settings here to make the service work by just
180 # enabling it. We use `mkDefault` for values that can be changed without
181 # problems
182 services.foo.settings = {
183 # Fails at runtime without any value set
184 log_level = lib.mkDefault "WARN";
185
186 # We assume systemd's `StateDirectory` is used, so we require this value,
187 # therefore no mkDefault
188 data_path = "/var/lib/foo";
189
190 # Since we use this to create a user we need to know the default value at
191 # eval time
192 user = lib.mkDefault "foo";
193 };
194
195 environment.etc."foo.json".source =
196 # The formats generator function takes a filename and the Nix value
197 # representing the format value and produces a filepath with that value
198 # rendered in the format
199 settingsFormat.generate "foo-config.json" cfg.settings;
200
201 # We know that the `user` attribute exists because we set a default value
202 # for it above, allowing us to use it without worries here
203 users.users.${cfg.settings.user} = { isSystemUser = true; };
204
205 # ...
206 };
207}
208```
209:::
210
211### Option declarations for attributes {#sec-settings-attrs-options}
212
213Some `settings` attributes may deserve some extra care. They may need a
214different type, default or merging behavior, or they are essential
215options that should show their documentation in the manual. This can be
216done using [](#sec-freeform-modules).
217
218We extend above example using freeform modules to declare an option for
219the port, which will enforce it to be a valid integer and make it show
220up in the manual.
221
222::: {#ex-settings-typed-attrs .example}
223::: {.title}
224**Example: Declaring a type-checked `settings` attribute**
225:::
226```nix
227settings = lib.mkOption {
228 type = lib.types.submodule {
229
230 freeformType = settingsFormat.type;
231
232 # Declare an option for the port such that the type is checked and this option
233 # is shown in the manual.
234 options.port = lib.mkOption {
235 type = lib.types.port;
236 default = 8080;
237 description = ''
238 Which port this service should listen on.
239 '';
240 };
241
242 };
243 default = {};
244 description = ''
245 Configuration for Foo, see
246 <link xlink:href="https://example.com/docs/foo"/>
247 for supported values.
248 '';
249};
250```
251:::