The Nix Language versus the NixOS Module System

From NixOS Wiki
Revision as of 03:41, 15 August 2024 by Rhendric (talk | contribs) (Created page with "If you are configuring NixOS, it behooves you to understand the difference between the Nix language (hereafter: Nix) and the NixOS module system (hereafter: NMS). This is a confusing topic because they are closely related: * NMS is implemented in, and configured using, Nix. * Many of the same things are possible in both. * You can 80% get away with using the Nix way of doing things when you should be using NMS, but that...")
(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: NMS). This is a confusing topic because they are closely related:

  • NMS is implemented in, and configured using, Nix.
  • Many of the same things are possible in both.
  • You can 80% get away with using the Nix way of doing things when you should be using NMS, but that 20% will bite you.

Here is a cheatsheet, with explanations below:

Feature In Nix In NMS
Merging attrs1 // attrs2 lib.mkMerge [ attrs1 attrs2 ]
If-then

if cond then values else { }
lib.optional cond values
lib.optionals cond valueList

lib.mkIf cond values
If-then-else if cond then v1 else v2
lib.mkMerge [
  (lib.mkIf cond v1)
  (lib.mkIf (!cond) v2)
]
Imports import ./file.nix imports = [ ./file.nix ];