NixOS:config argument: Difference between revisions
imported>Fadenb Created page with "This argument gives access to the entire configuration of the system. It is computed from option declarations and option definitions defined inside all modules used for the s..." |
m changed instances of pkgs.lib to more commonly available lib |
||
(5 intermediate revisions by 4 users not shown) | |||
Line 5: | Line 5: | ||
The following code is used to support the explanation. | The following code is used to support the explanation. | ||
< | <syntaxhighlight lang="nix"> | ||
{config, pkgs, ...}: | {config, pkgs, lib ...}: | ||
{ | { | ||
options = { | options = { | ||
foo = | foo = lib.mkOption { | ||
description = "..."; | description = "..."; | ||
}; | }; | ||
Line 19: | Line 19: | ||
}; | }; | ||
} | } | ||
</ | </syntaxhighlight> | ||
This snippet of code is a module which declare the option "<code>foo</code>" and define the option "<code>bar</code>". The option "<code>bar</code>" is defined with the value <code>config.foo</code>. This attribute <code>foo</code> of the <code>config</code> argument, is the result of the [[NixOS:Declaration#Evaluation|evaluation]] of the definitions and the declarations of the option "<code>foo</code>". Definitions of the option "<code>foo</code>" may exists in other module used by the system. | This snippet of code is a module which declare the option "<code>foo</code>" and define the option "<code>bar</code>". The option "<code>bar</code>" is defined with the value <code>config.foo</code>. This attribute <code>foo</code> of the <code>config</code> argument, is the result of the [[NixOS:Declaration#Evaluation|evaluation]] of the definitions and the declarations of the option "<code>foo</code>". Definitions of the option "<code>foo</code>" may exists in other module used by the system. | ||
Line 29: | Line 29: | ||
The process of module computation is highly recursive and may cause trouble when you want to add control flow statements. A common mistake is to use "<code>if</code>" or "<code>assert</code>" statements in the computation of a module. | The process of module computation is highly recursive and may cause trouble when you want to add control flow statements. A common mistake is to use "<code>if</code>" or "<code>assert</code>" statements in the computation of a module. | ||
< | <syntaxhighlight lang="nix"> | ||
{config, pkgs, ...}: | {config, pkgs, lib, ...}: | ||
{ | { | ||
options = { | options = { | ||
foo = | foo = lib.mkOption { | ||
default = false; | default = false; | ||
type = with | type = with lib.types; bool; | ||
description = " | description = "foo boolean option"; | ||
}; | }; | ||
}; | }; | ||
Line 47: | Line 47: | ||
{}; | {}; | ||
} | } | ||
</ | </syntaxhighlight> | ||
The previous module cause an infinite recursion: | The previous module cause an infinite recursion: | ||
Line 58: | Line 58: | ||
To avoid such infinite recursion, [[NixOS:Properties|properties]] have been introduced, thus the previous code should be rewritten in: | To avoid such infinite recursion, [[NixOS:Properties|properties]] have been introduced, thus the previous code should be rewritten in: | ||
< | <syntaxhighlight lang="nix"> | ||
{config, pkgs, ...}: | {config, pkgs, lib, ...}: | ||
{ | { | ||
options = { | options = { | ||
foo = | foo = lib.mkOption { | ||
default = false; | default = false; | ||
type = with | type = with lib.types; bool; | ||
description = " | description = "foo boolean"; | ||
}; | }; | ||
}; | }; | ||
config = | config = lib.mkIf config.foo { | ||
bar = 42; | bar = 42; | ||
}; | }; | ||
} | } | ||
</ | </syntaxhighlight> | ||
== Common Pattern == | == Common Pattern == | ||
Line 80: | Line 80: | ||
Often, the module declare options embedded inside an attribute set. To access these options, we add an attribute <code>cfg</code> as a shortcut notation. | Often, the module declare options embedded inside an attribute set. To access these options, we add an attribute <code>cfg</code> as a shortcut notation. | ||
< | <syntaxhighlight lang="nix"> | ||
{config, pkgs, ...}: | {config, pkgs, lib, ...}: | ||
let | let | ||
Line 89: | Line 89: | ||
in | in | ||
with | with lib; { | ||
options = { | options = { | ||
foo.bar.baz = { | foo.bar.baz = { | ||
Line 103: | Line 103: | ||
}; | }; | ||
} | } | ||
</ | </syntaxhighlight> | ||
[[Category:Reference]] | |||
[[Category:NixOS]] |