NixOS:config argument: Difference between revisions

imported>Makefu
No edit summary
Raf (talk | contribs)
m changed instances of pkgs.lib to more commonly available lib
 
(4 intermediate revisions by 3 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.


<pre>
<syntaxhighlight lang="nix">
{config, pkgs, ...}:
{config, pkgs, lib ...}:


{
{
   options = {
   options = {
     foo = pkgs.lib.mkOption {
     foo = lib.mkOption {
       description = "...";
       description = "...";
     };
     };
Line 19: Line 19:
   };
   };
}
}
</pre>
</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.


<pre>
<syntaxhighlight lang="nix">
{config, pkgs, ...}:
{config, pkgs, lib, ...}:


{
{
   options = {
   options = {
     foo = pkgs.lib.mkOption {
     foo = lib.mkOption {
       default = false;
       default = false;
       type = with pkgs.lib.types; bool;
       type = with lib.types; bool;
       description = "enable foo";
       description = "foo boolean option";
     };
     };
   };
   };
Line 47: Line 47:
       {};
       {};
}
}
</pre>
</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:


<pre>
<syntaxhighlight lang="nix">
{config, pkgs, ...}:
{config, pkgs, lib, ...}:


{
{
   options = {
   options = {
     foo = pkgs.lib.mkOption {
     foo = lib.mkOption {
       default = false;
       default = false;
       type = with pkgs.lib.types; bool;
       type = with lib.types; bool;
       description = "enable foo";
       description = "foo boolean";
     };
     };
   };
   };


   config = pkgs.lib.mkIf config.foo {
   config = lib.mkIf config.foo {
     bar = 42;
     bar = 42;
   };
   };
}
}
</pre>
</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.


<pre>
<syntaxhighlight lang="nix">
{config, pkgs, ...}:
{config, pkgs, lib, ...}:


let
let
Line 89: Line 89:
in
in


with pkgs.lib; {
with lib; {
   options = {
   options = {
     foo.bar.baz = {
     foo.bar.baz = {
Line 103: Line 103:
   };
   };
}
}
</pre>
</syntaxhighlight>


[[Category:Discussion]]
[[Category:Reference]]
[[Category:Pages_with_syntax_highlighting_errors]]
[[Category:NixOS]]