YOUR JOURNEY
08 / 18
02THE NIXOS SYSTEM 6 MIN READ
LESSON 08 / COMPOSE WITH MODULES

Smaller files.
One system.

Split a configuration by responsibility, then compose it.

Modules contribute option definitions. Imports connect those definitions into a single evaluated system.

Explore the lesson
~$ NixOS examples · try them in a VM.
THE NIX MODEL01 / 03
Drag to rotate

Separate concerns into small modules.

hardware.nixdesktop.nixtools.nix
08.1DIVIDE BY CONCERN

Give each file a clear job.

A module may be a plain attribute set or a function receiving arguments such as config, lib, and pkgs. The imports list brings other modules into the same evaluation; it is not a sequence of shell operations.

Move a small set of related options into tools.nix, then import that file from your existing configuration. The paths are relative to the file that contains them.

two module excerpts
# tools.nix
{ pkgs, ... }: {
  environment.systemPackages = [ pkgs.git pkgs.hello ];
}
 
# In configuration.nix
imports = [
  ./hardware-configuration.nix
  ./tools.nix
];
Next section: Definitions combine by option type.
08.2MERGING HAS RULES

Definitions combine by option type.

List-valued options often concatenate. Incompatible definitions for a single-value option usually cause an evaluation error rather than silently choosing whichever file came last.

lib.mkDefault supplies a low-priority default; lib.mkForce overrides ordinary priorities. Use them intentionally. If a conflict is surprising, first look for duplicate definitions instead of immediately forcing a value.

TAKE THIS WITH YOU

Modules merge declarations. Their import order is not an imperative setup script.

Go a little deeperThe Nix module system
UP NEXT

Build, test, switch

Building a system and activating it are different actions.

Next lesson