1 # Nixpkgs/NixOS option handling.
28 inherit (lib.attrsets)
42 prioritySuggestion = ''
43 Use `lib.mkForce value` or `lib.mkDefault value` to change the priority on any of these definitions.
48 /* Returns true when the given argument is an option
50 Type: isOption :: a -> bool
53 isOption 1 // => false
54 isOption (mkOption {}) // => true
56 isOption = lib.isType "option";
58 /* Creates an Option attribute set. mkOption accepts an attribute set with the following keys:
60 All keys default to `null` when not given.
63 mkOption { } // => { _type = "option"; }
64 mkOption { default = "foo"; } // => { _type = "option"; default = "foo"; }
68 # Default value used when no definition is given in the configuration.
70 # Textual representation of the default, for the manual.
72 # Example value used in the manual.
74 # String describing the option.
76 # Related packages used in the manual (see `genRelatedPackages` in ../nixos/lib/make-options-doc/default.nix).
77 relatedPackages ? null,
78 # Option type, providing type-checking and value merging.
80 # Function that converts the option value to something else.
82 # Whether the option is for NixOS developers only.
84 # Whether the option shows up in the manual. Default: true. Use false to hide the option and any sub-options from submodules. Use "shallow" to hide only sub-options.
86 # Whether the option can be set only once
89 attrs // { _type = "option"; };
91 /* Creates an Option attribute set for a boolean value option i.e an
92 option to be toggled on or off:
96 => { _type = "option"; default = false; description = "Whether to enable foo."; example = true; type = { ... }; }
99 # Name for the created option
103 description = "Whether to enable ${name}.";
104 type = lib.types.bool;
107 /* Creates an Option attribute set for an option that specifies the
108 package a module should use for some purpose.
110 The package is specified in the third argument under `default` as a list of strings
111 representing its attribute path in nixpkgs (or another package set).
112 Because of this, you need to pass nixpkgs itself (usually `pkgs` in a module;
113 alternatively to nixpkgs itself, another package set) as the first argument.
115 If you pass another package set you should set the `pkgsText` option.
116 This option is used to display the expression for the package set. It is `"pkgs"` by default.
117 If your expression is complex you should parenthesize it, as the `pkgsText` argument
118 is usually immediately followed by an attribute lookup (`.`).
120 The second argument may be either a string or a list of strings.
121 It provides the display name of the package in the description of the generated option
122 (using only the last element if the passed value is a list)
123 and serves as the fallback value for the `default` argument.
125 To include extra information in the description, pass `extraDescription` to
126 append arbitrary text to the generated description.
128 You can also pass an `example` value, either a literal string or an attribute path.
130 The `default` argument can be omitted if the provided name is
131 an attribute of pkgs (if `name` is a string) or a valid attribute path in pkgs (if `name` is a list).
132 You can also set `default` to just a string in which case it is interpreted as an attribute name
133 (a singleton attribute path, if you will).
135 If you wish to explicitly provide no default, pass `null` as `default`.
137 If you want users to be able to set no package, pass `nullable = true`.
138 In this mode a `default = null` will not be interpreted as no default and is interpreted literally.
140 Type: mkPackageOption :: pkgs -> (string|[string]) -> { nullable? :: bool, default? :: string|[string], example? :: null|string|[string], extraDescription? :: string, pkgsText? :: string } -> option
143 mkPackageOption pkgs "hello" { }
144 => { ...; default = pkgs.hello; defaultText = literalExpression "pkgs.hello"; description = "The hello package to use."; type = package; }
147 mkPackageOption pkgs "GHC" {
149 example = "pkgs.haskell.packages.ghc92.ghc.withPackages (hkgs: [ hkgs.primes ])";
151 => { ...; default = pkgs.ghc; defaultText = literalExpression "pkgs.ghc"; description = "The GHC package to use."; example = literalExpression "pkgs.haskell.packages.ghc92.ghc.withPackages (hkgs: [ hkgs.primes ])"; type = package; }
154 mkPackageOption pkgs [ "python3Packages" "pytorch" ] {
155 extraDescription = "This is an example and doesn't actually do anything.";
157 => { ...; default = pkgs.python3Packages.pytorch; defaultText = literalExpression "pkgs.python3Packages.pytorch"; description = "The pytorch package to use. This is an example and doesn't actually do anything."; type = package; }
160 mkPackageOption pkgs "nushell" {
163 => { ...; default = pkgs.nushell; defaultText = literalExpression "pkgs.nushell"; description = "The nushell package to use."; type = nullOr package; }
166 mkPackageOption pkgs "coreutils" {
169 => { ...; description = "The coreutils package to use."; type = package; }
172 mkPackageOption pkgs "dbus" {
176 => { ...; default = null; description = "The dbus package to use."; type = nullOr package; }
179 mkPackageOption pkgs.javaPackages "OpenJFX" {
180 default = "openjfx20";
181 pkgsText = "pkgs.javaPackages";
183 => { ...; default = pkgs.javaPackages.openjfx20; defaultText = literalExpression "pkgs.javaPackages.openjfx20"; description = "The OpenJFX package to use."; type = package; }
186 # Package set (an instantiation of nixpkgs such as pkgs in modules or another package set)
188 # Name for the package, shown in option description
191 # Whether the package can be null, for example to disable installing a package altogether (defaults to false)
193 # The attribute path where the default package is located (may be omitted, in which case it is copied from `name`)
195 # A string or an attribute path to use as an example (may be omitted)
197 # Additional text to include in the option description (may be omitted)
198 extraDescription ? "",
199 # Representation of the package set passed as pkgs (defaults to `"pkgs"`)
203 name' = if isList name then last name else name;
204 default' = if isList default then default else [ default ];
205 defaultText = concatStringsSep "." default';
206 defaultValue = attrByPath default'
207 (throw "${defaultText} cannot be found in ${pkgsText}") pkgs;
208 defaults = if default != null then {
209 default = defaultValue;
210 defaultText = literalExpression ("${pkgsText}." + defaultText);
211 } else optionalAttrs nullable {
214 in mkOption (defaults // {
215 description = "The ${name'} package to use."
216 + (if extraDescription == "" then "" else " ") + extraDescription;
217 type = with lib.types; (if nullable then nullOr else lib.id) package;
218 } // optionalAttrs (example != null) {
219 example = literalExpression
220 (if isList example then "${pkgsText}." + concatStringsSep "." example else example);
223 /* Alias of mkPackageOption. Previously used to create options with markdown
224 documentation, which is no longer required.
226 mkPackageOptionMD = mkPackageOption;
228 /* This option accepts anything, but it does not produce any result.
230 This is useful for sharing a module across different module sets
231 without having to implement similar features as long as the
232 values of the options are not accessed. */
233 mkSinkUndeclaredOptions = attrs: mkOption ({
237 description = "Sink for option definitions.";
238 type = mkOptionType {
241 merge = loc: defs: false;
243 apply = x: throw "Option value is not readable because the option is not declared.";
246 mergeDefaultOption = loc: defs:
247 let list = getValues defs; in
248 if length list == 1 then head list
249 else if all isFunction list then x: mergeDefaultOption loc (map (f: f x) list)
250 else if all isList list then concatLists list
251 else if all isAttrs list then foldl' lib.mergeAttrs {} list
252 else if all isBool list then foldl' lib.or false list
253 else if all isString list then lib.concatStrings list
254 else if all isInt list && all (x: x == head list) list then head list
255 else throw "Cannot merge definitions of `${showOption loc}'. Definition values:${showDefs defs}";
257 mergeOneOption = mergeUniqueOption { message = ""; };
259 mergeUniqueOption = { message }: loc: defs:
261 then (head defs).value
262 else assert length defs > 1;
263 throw "The option `${showOption loc}' is defined multiple times while it's expected to be unique.\n${message}\nDefinition values:${showDefs defs}\n${prioritySuggestion}";
265 /* "Merge" option definitions by checking that they all have the same value. */
266 mergeEqualOption = loc: defs:
267 if defs == [] then abort "This case should never happen."
268 # Return early if we only have one element
269 # This also makes it work for functions, because the foldl' below would try
270 # to compare the first element with itself, which is false for functions
271 else if length defs == 1 then (head defs).value
272 else (foldl' (first: def:
273 if def.value != first.value then
274 throw "The option `${showOption loc}' has conflicting definition values:${showDefs [ first def ]}\n${prioritySuggestion}"
276 first) (head defs) (tail defs)).value;
278 /* Extracts values of all "value" keys of the given list.
280 Type: getValues :: [ { value :: a; } ] -> [a]
283 getValues [ { value = 1; } { value = 2; } ] // => [ 1 2 ]
284 getValues [ ] // => [ ]
286 getValues = map (x: x.value);
288 /* Extracts values of all "file" keys of the given list
290 Type: getFiles :: [ { file :: a; } ] -> [a]
293 getFiles [ { file = "file1"; } { file = "file2"; } ] // => [ "file1" "file2" ]
294 getFiles [ ] // => [ ]
296 getFiles = map (x: x.file);
298 # Generate documentation template from the list of option declaration like
299 # the set generated with filterOptionSets.
300 optionAttrSetToDocList = optionAttrSetToDocList' [];
302 optionAttrSetToDocList' = _: options:
305 name = showOption opt.loc;
309 description = opt.description or null;
310 declarations = filter (x: x != unknownModule) opt.declarations;
311 internal = opt.internal or false;
313 if (opt?visible && opt.visible == "shallow")
315 else opt.visible or true;
316 readOnly = opt.readOnly or false;
317 type = opt.type.description or "unspecified";
319 // optionalAttrs (opt ? example) {
321 builtins.addErrorContext "while evaluating the example of option `${name}`" (
322 renderOptionValue opt.example
325 // optionalAttrs (opt ? defaultText || opt ? default) {
327 builtins.addErrorContext "while evaluating the ${if opt?defaultText then "defaultText" else "default value"} of option `${name}`" (
328 renderOptionValue (opt.defaultText or opt.default)
331 // optionalAttrs (opt ? relatedPackages && opt.relatedPackages != null) { inherit (opt) relatedPackages; };
334 let ss = opt.type.getSubOptions opt.loc;
335 in if ss != {} then optionAttrSetToDocList' opt.loc ss else [];
336 subOptionsVisible = docOption.visible && opt.visible or null != "shallow";
338 # To find infinite recursion in NixOS option docs:
339 # builtins.trace opt.loc
340 [ docOption ] ++ optionals subOptionsVisible subOptions) (collect isOption options);
343 /* This function recursively removes all derivation attributes from
344 `x` except for the `name` attribute.
346 This is to make the generation of `options.xml` much more
347 efficient: the XML representation of derivations is very large
348 (on the order of megabytes) and is not actually used by the
351 This function was made obsolete by renderOptionValue and is kept for
352 compatibility with out-of-tree code.
354 scrubOptionValue = x:
355 if isDerivation x then
356 { type = "derivation"; drvPath = x.name; outPath = x.name; name = x.name; }
357 else if isList x then map scrubOptionValue x
358 else if isAttrs x then mapAttrs (n: v: scrubOptionValue v) (removeAttrs x ["_args"])
362 /* Ensures that the given option value (default or example) is a `_type`d string
363 by rendering Nix values to `literalExpression`s.
365 renderOptionValue = v:
366 if v ? _type && v ? text then v
367 else literalExpression (lib.generators.toPretty {
369 allowPrettyValues = true;
373 /* For use in the `defaultText` and `example` option attributes. Causes the
374 given string to be rendered verbatim in the documentation as Nix code. This
375 is necessary for complex values, e.g. functions, or values that depend on
376 other values or packages.
378 literalExpression = text:
379 if ! isString text then throw "literalExpression expects a string."
380 else { _type = "literalExpression"; inherit text; };
382 literalExample = lib.warn "literalExample is deprecated, use literalExpression instead, or use literalMD for a non-Nix description." literalExpression;
384 /* Transition marker for documentation that's already migrated to markdown
385 syntax. This is a no-op and no longer needed.
389 /* For use in the `defaultText` and `example` option attributes. Causes the
390 given MD text to be inserted verbatim in the documentation, for when
391 a `literalExpression` would be too hard to read.
394 if ! isString text then throw "literalMD expects a string."
395 else { _type = "literalMD"; inherit text; };
399 /* Convert an option, described as a list of the option parts to a
400 human-readable version.
403 (showOption ["foo" "bar" "baz"]) == "foo.bar.baz"
404 (showOption ["foo" "bar.baz" "tux"]) == "foo.\"bar.baz\".tux"
405 (showOption ["windowManager" "2bwm" "enable"]) == "windowManager.\"2bwm\".enable"
407 Placeholders will not be quoted as they are not actual values:
408 (showOption ["foo" "*" "bar"]) == "foo.*.bar"
409 (showOption ["foo" "<name>" "bar"]) == "foo.<name>.bar"
411 showOption = parts: let
412 escapeOptionPart = part:
414 # We assume that these are "special values" and not real configuration data.
415 # If it is real configuration data, it is rendered incorrectly.
416 specialIdentifiers = [
417 "<name>" # attrsOf (submodule {})
418 "*" # listOf (submodule {})
419 "<function body>" # functionTo
421 in if builtins.elem part specialIdentifiers
423 else lib.strings.escapeNixIdentifier part;
424 in (concatStringsSep ".") (map escapeOptionPart parts);
425 showFiles = files: concatStringsSep " and " (map (f: "`${f}'") files);
427 showDefs = defs: concatMapStrings (def:
429 # Pretty print the value for display, if successful
430 prettyEval = builtins.tryEval
431 (lib.generators.toPretty { }
432 (lib.generators.withRecursion { depthLimit = 10; throwOnDepthLimit = false; } def.value));
433 # Split it into its lines
434 lines = filter (v: ! isList v) (builtins.split "\n" prettyEval.value);
435 # Only display the first 5 lines, and indent them for better visibility
436 value = concatStringsSep "\n " (take 5 lines ++ optional (length lines > 5) "...");
438 # Don't print any value if evaluating the value strictly fails
439 if ! prettyEval.success then ""
440 # Put it on a new line if it consists of multiple
441 else if length lines > 1 then ":\n " + value
443 in "\n- In `${def.file}'${result}"
446 showOptionWithDefLocs = opt: ''
447 ${showOption opt.loc}, with values defined in:
448 ${concatMapStringsSep "\n" (defFile: " - ${defFile}") opt.files}
451 unknownModule = "<unknown-file>";