The Nix Language versus the NixOS Module System

From NixOS Wiki
Revision as of 19:17, 17 August 2024 by Sandro (talk | contribs) (Remove uncommon NMS abbreviation, cleanup page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

If you are configuring NixOS, it behooves you to understand the difference between the Nix language (hereafter: Nix) and the NixOS module system (hereafter: module system). The module system is implemented and configured using Nix.

Generally the module system way of doing things is preferred as it properly merges values, doesn't set empty settings and allows the module system to verify the structure even if conditions are false.

Here is a cheat sheet, with explanations below:

Feature In Nix In the module system
Merging two attrsets lib.recurisveUpdate attrs1 attrs2 lib.mkMerge [ attrs1 attrs2 ]
If-then

if cond then values else { }

lib.mkIf cond values
Imports import ./file.nix imports = [ ./file.nix ];