Create Modules¶
Style 1 — Basic¶
Manual enable option:
{ config, lib, ... }:
let
cfg = config.modules.gnome;
in
{
options.modules.gnome = {
enable = lib.mkEnableOption "Enable gnome settings";
};
config = lib.mkIf cfg.enable {
};
}
Style 2 — Advanced¶
Path-derived option with extra options:
{ config, lib, getRelPath, ... }:
let
relPath = getRelPath ./.;
pathList = ["modules"] ++ (lib.splitString "/" relPath);
cfg = lib.getAttrFromPath pathList config;
in
{
options = lib.setAttrByPath pathList {
enable = lib.mkEnableOption "Enable x11 settings";
screen.dpi = lib.mkOption {
type = lib.types.int;
default = 144;
description = "Set screen DPI [96, 144, 192]";
};
};
config = lib.mkIf cfg.enable {
};
}
Style 3 — Function Shorthand (Most Common)¶
mkModule config path content automatically:
- Derives the module's option path from its filesystem path
- e.g.
home-manager/programs/zsh→options.modules.home-manager.programs.zsh.enable
- e.g.
- Wraps
contentunderlib.mkIf cfg.enable
Adding a New Module¶
- Create
home-manager/<category>/<name>/default.nixusingmkModule config ./. { ... } - Enable it per-device in
hosts/{device}/home.nixundermodules.home-manager.<category>.<name>.enable = true
The module is auto-discovered — no import list to update.