NixOS modules: Difference between revisions
VTimofeenko (talk | contribs) →Function: expand the description of the module arguments |
|||
(5 intermediate revisions by 4 users not shown) | |||
Line 16: | Line 16: | ||
options = { | options = { | ||
# Option declarations. | # Option declarations. | ||
# Declare what settings a user of this | # Declare what settings a user of this module can set. | ||
# Usually this includes a global "enable" option which defaults to false. | # Usually this includes a global "enable" option which defaults to false. | ||
}; | }; | ||
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 ==== | |||
The [[NixOS:config_argument|<code>config</code>]], <code>options</code>, <code>lib</code>, <code>pkgs</code>, and <code>modulesPath</code> arguments are passed automatically to modules, when the module is imported. | |||
For example, in the following Nix flake, the `./configuration.nix` file will be provided with the default set of arguments listed above, plus `extraArg`, which was set in the `specialArgs` argument to the `nixosGenerate` function.<syntaxhighlight lang="nix"> | |||
{ | |||
inputs = { | |||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11"; | |||
nixos-generators = { | |||
url = "github:nix-community/nixos-generators"; | |||
inputs.nixpkgs.follows = "nixpkgs"; | |||
}; | |||
xc = { | |||
url = "github:joerdav/xc"; | |||
inputs.nixpkgs.follows = "nixpkgs"; | |||
}; | |||
}; | |||
outputs = { nixpkgs, nixos-generators, xc, ... }: | |||
let | |||
pkgsForSystem = system: import nixpkgs { | |||
inherit system; | |||
overlays = [ | |||
(final: prev: { xc = xc.packages.${system}.xc; }) | |||
]; | |||
}; | |||
allVMs = [ "x86_64-linux" "aarch64-linux" ]; | |||
forAllVMs = f: nixpkgs.lib.genAttrs allVMs (system: f { | |||
inherit system; | |||
pkgs = pkgsForSystem system; | |||
}); | |||
in | |||
{ | |||
packages = forAllVMs ({ system, pkgs }: { | |||
vm = nixos-generators.nixosGenerate { | |||
system = system; | |||
specialArgs = { | |||
extraArg = "foobar"; | |||
}; | |||
modules = [ | |||
./configuration.nix | |||
]; | |||
format = "raw"; | |||
}; | |||
}); | |||
}; | |||
} | |||
</syntaxhighlight> | |||
==== modulesPath ==== | ==== modulesPath ==== | ||
Line 348: | Line 398: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
If you're developing on top of master, this will potentially cause the compilation of lots of packages, since changes on master might not cached on cache.nixos.org yet. To avoid that, you can develop your module on top of the <code>nixos-unstable</code> [[ | If you're developing on top of master, this will potentially cause the compilation of lots of packages, since changes on master might not cached on cache.nixos.org yet. To avoid that, you can develop your module on top of the <code>nixos-unstable</code> [[Channel branches|channel branch]], tracked by the eponymous branch in https://github.com/NixOS/nixpkgs: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 362: | Line 412: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Of course, it doesn't have to be {{|c|nixos-rebuild}} in particular. | Of course, it doesn't have to be {{|c|nixos-rebuild}} in particular. | ||
== References == | == References == | ||