NixOS modules: Difference between revisions

imported>Milahu
clarify modulesPath
imported>Milahu
add section modulesPath
Line 64: Line 64:


<dt><code>modulesPath</code></dt>
<dt><code>modulesPath</code></dt>
<dd><p>The location of the <code>module</code> directory of NixOS.</p>
<dd>The location of the <code>module</code> directory of NixOS.</dd>
<p>Parsed from the first entry in NIX_PATH, for example:<br>
<code>NIX_PATH=/nix/var/nix/profiles/per-user/root/channels/nixos:nixpkgs=/etc/nixos/nixpkgs:nixos-config=/etc/nixos/configuration.nix</code><br>
&rarr; <code>modulesPath = /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules</code></p>
<p>note: <code>NIX_PATH</code> is empty when [[Flakes#Using nix flakes with NixOS|Using nix flakes with NixOS]],<br>
and nix will throw <code>undefined variable 'modulesPath'</code></p>
</dd>


</dl>
</dl>
==== modulesPath ====
Some modules use <code>modulesPath</code> to import nixos libraries
For example <code>nixos/modules/virtualisation/digital-ocean-config.nix</code>
<pre>
{ config, pkgs, lib, modulesPath, ... }:
  imports = [
    (modulesPath + "/profiles/qemu-guest.nix")
    (modulesPath + "/virtualisation/digital-ocean-init.nix")
  ];
</pre>
The Nix variable <code>modulesPath</code> is parsed from the environment variable <code>NIX_PATH</code>
When <code>NIX_PATH</code> is empty, Nix can throw the error <code>undefined variable 'modulesPath'</code>
<code>NIX_PATH</code> should look something like this:
<pre>
echo $NIX_PATH | tr : '\n'
nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos
nixos-config=/etc/nixos/configuration.nix
/nix/var/nix/profiles/per-user/root/channels
</pre>
Here, the <code>modulesPath</code> is <code>/nix/var/nix/profiles/per-user/root/channels</code>
When a Nix expression calls <code>import <nixpkgs></code>,<br>
then Nix will load <code>/nix/var/nix/profiles/per-user/root/channels/nixos</code>


=== Imports ===
=== Imports ===