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`type` 126 127: A module system type representing a value of the format 128 129`lib` 130 131: Utility functions for convenience, or special interactions with the format. 132 This attribute is optional. It may contain inside a `types` attribute 133 containing types specific to this format. 134 135`generate` *`filename jsonValue`* 136 137: A function that can render a value of the format to a file. Returns 138 a file path. 139 140 ::: {.note} 141 This function puts the value contents in the Nix store. So this 142 should be avoided for secrets. 143 ::: 144 145::: {#ex-settings-nix-representable .example} 146### Module with conventional `settings` option 147 148The following shows a module for an example program that uses a JSON 149configuration file. It demonstrates how above values can be used, along 150with some other related best practices. See the comments for 151explanations. 152 153```nix 154{ options, config, lib, pkgs, ... }: 155let 156 cfg = config.services.foo; 157 # Define the settings format used for this program 158 settingsFormat = pkgs.formats.json {}; 159in { 160 161 options.services.foo = { 162 enable = lib.mkEnableOption "foo service"; 163 164 settings = lib.mkOption { 165 # Setting this type allows for correct merging behavior 166 type = settingsFormat.type; 167 default = {}; 168 description = '' 169 Configuration for foo, see 170 <link xlink:href="https://example.com/docs/foo"/> 171 for supported settings. 172 ''; 173 }; 174 }; 175 176 config = lib.mkIf cfg.enable { 177 # We can assign some default settings here to make the service work by just 178 # enabling it. We use `mkDefault` for values that can be changed without 179 # problems 180 services.foo.settings = { 181 # Fails at runtime without any value set 182 log_level = lib.mkDefault "WARN"; 183 184 # We assume systemd's `StateDirectory` is used, so we require this value, 185 # therefore no mkDefault 186 data_path = "/var/lib/foo"; 187 188 # Since we use this to create a user we need to know the default value at 189 # eval time 190 user = lib.mkDefault "foo"; 191 }; 192 193 environment.etc."foo.json".source = 194 # The formats generator function takes a filename and the Nix value 195 # representing the format value and produces a filepath with that value 196 # rendered in the format 197 settingsFormat.generate "foo-config.json" cfg.settings; 198 199 # We know that the `user` attribute exists because we set a default value 200 # for it above, allowing us to use it without worries here 201 users.users.${cfg.settings.user} = { isSystemUser = true; }; 202 203 # ... 204 }; 205} 206``` 207::: 208 209### Option declarations for attributes {#sec-settings-attrs-options} 210 211Some `settings` attributes may deserve some extra care. They may need a 212different type, default or merging behavior, or they are essential 213options that should show their documentation in the manual. This can be 214done using [](#sec-freeform-modules). 215 216We extend above example using freeform modules to declare an option for 217the port, which will enforce it to be a valid integer and make it show 218up in the manual. 219 220::: {#ex-settings-typed-attrs .example} 221### Declaring a type-checked `settings` attribute 222```nix 223settings = lib.mkOption { 224 type = lib.types.submodule { 225 226 freeformType = settingsFormat.type; 227 228 # Declare an option for the port such that the type is checked and this option 229 # is shown in the manual. 230 options.port = lib.mkOption { 231 type = lib.types.port; 232 default = 8080; 233 description = '' 234 Which port this service should listen on. 235 ''; 236 }; 237 238 }; 239 default = {}; 240 description = '' 241 Configuration for Foo, see 242 <link xlink:href="https://example.com/docs/foo"/> 243 for supported values. 244 ''; 245}; 246``` 247:::