Jump to content

The Nix Language versus the NixOS Module System

From NixOS Wiki
Revision as of 00:00, 1 March 2025 by Rhendric (talk | contribs)

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
Deep merging two attrsets lib.recursiveUpdate attrs1 attrs2 lib.mkMerge [ attrs1 attrs2 ]
If-then if cond then values else { }
lib.mkIf cond values
Imports import ./file.nix
imports = [ ./file.nix ];
Recursive references
rec {
  foo = 1;
  bar = foo;
}
{ config, ... }:
{
  foo = 1;
  bar = config.foo;
}