NixOS modules: Difference between revisions
imported from old wiki |
→With Flakes: fix? typo and typesetting; idk what {{|c|nixos-rebuild}} was intended to be |
||
| (4 intermediate revisions by 2 users not shown) | |||
| Line 30: | Line 30: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
There is a shorthand for modules without any declarations: | There is a shorthand for modules without any option declarations: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 47: | Line 47: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Note that despite the name, <code>imports = [./module.nix]</code> should not be confused with the Nix [https://nixos.org/manual/nix/stable/language/builtins.html#builtins-import builtins] function <code>import module.nix</code>. | |||
<code>imports</code> expects a path to a file containing a NixOS module structured as described here. <code>import</code> can load arbitrary Nix expression from provided file with no expectation of structure. (no expected structure). See [https://discourse.nixos.org/t/import-list-in-configuration-nix-vs-import-function/11372/8 this post] for more details. | |||
Note: <code>imports</code> provides the same behavior as the obsolete <code>require</code>. There is no reason to use <code>require</code> anymore, however it may still linger in some legacy code. | Note: <code>imports</code> provides the same behavior as the obsolete <code>require</code>. There is no reason to use <code>require</code> anymore, however it may still linger in some legacy code. | ||
| Line 53: | Line 55: | ||
=== Function === | === Function === | ||
A module | A module may be a function accepting an attribute set. | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
| Line 63: | Line 65: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Following arguments are available in NixOS modules by default: | |||
<dl> | <dl> | ||
| Line 82: | Line 84: | ||
<dd>The location of the <code>module</code> directory of NixOS.</dd> | <dd>The location of the <code>module</code> directory of NixOS.</dd> | ||
</dl> | </dl>The "<code>...</code>" part of the argument attribute set indicates that this module does not depend on the rest of the arguments. When the module is defined as a function, this pattern is typically required, otherwise the evaluation will fail citing unexpected arguments. | ||
==== Passing custom values to modules ==== | ==== Passing custom values to modules ==== | ||
| Line 358: | Line 360: | ||
The module system itself is rather complex, but here's a short overview. A module evaluation consists of a set of "modules", which can do three things: | The module system itself is rather complex, but here's a short overview. A module evaluation consists of a set of "modules", which can do three things: | ||
* Import other modules (through imports = [ ./other-module.nix ]) | * Import other modules (through <code>imports = [ ./other-module.nix ]</code>) | ||
* Declare options (through options = { ... }) | * Declare options (through <code>options = { ... }</code>) | ||
* Define option values (through | * Define option values (through <code>config = { ... }</code>, or without the config key as a shorthand if you don't have imports or options) | ||
To do the actual evaluation, there's these rough steps: | To do the actual evaluation, there's these rough steps: | ||
| Line 407: | Line 409: | ||
If you want to develop a module from a git repo, you can use `--override-input`. For example, if you have an input in your flake called {{ic|jovian}},, you can use | If you want to develop a module from a git repo, you can use `--override-input`. For example, if you have an input in your flake called {{ic|jovian}},, you can use | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
nixos-rebuild switch --override-input jovian <path-to-url> | nixos-rebuild switch --override-input jovian <path-to-url> --flake <uri> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Of course, it doesn't have to | Of course, it doesn't have to [[nixos-rebuild|<code>nixos-rebuild</code>]] in particular. | ||
== References == | == References == | ||