NixOS:config argument: Difference between revisions

imported>Makefu
No edit summary
imported>Fadenb
Syntax highlighting
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, ...}:


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, ...}:


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, ...}:


Line 74: Line 74:
   };
   };
}
}
</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, ...}:


Line 103: Line 103:
   };
   };
}
}
</pre>
</syntaxhighlight>


[[Category:Discussion]]
[[Category:Discussion]]
[[Category:Pages_with_syntax_highlighting_errors]]
[[Category:Pages_with_syntax_highlighting_errors]]