Home Explore Blog Models CI



nixpkgs

1st chunk of `nixos/doc/manual/configuration/modularity.section.md`
81af666f9d4c872c8783cd7ce97e91f97360afbee706379e0000000100000838
# Modularity {#sec-modularity}

The NixOS configuration mechanism is modular. If your
`configuration.nix` becomes too big, you can split it into multiple
files. Likewise, if you have multiple NixOS configurations (e.g. for
different computers) with some commonality, you can move the common
configuration into a shared file.

Modules have exactly the same syntax as `configuration.nix`. In fact,
`configuration.nix` is itself a module. You can use other modules by
including them from `configuration.nix`, e.g.:

```nix
{ config, pkgs, ... }:

{
  imports = [
    ./vpn.nix
    ./kde.nix
  ];
  services.httpd.enable = true;
  environment.systemPackages = [ pkgs.emacs ];
  # ...
}
```

Here, we include two modules from the same directory, `vpn.nix` and
`kde.nix`. The latter might look like this:

```nix
{ config, pkgs, ... }:

{
  services.xserver.enable = true;
  services.displayManager.sddm.enable = true;
  services.desktopManager.plasma6.enable = true;
  environment.systemPackages = [ pkgs.vim ];
}
```

Note that both `configuration.nix` and `kde.nix` define the option
[](#opt-environment.systemPackages). When multiple modules define an
option, NixOS will try to *merge* the definitions. In the case of
[](#opt-environment.systemPackages) the lists of packages will be
concatenated. The value in `configuration.nix` is
merged last, so for list-type options, it will appear at the end of the
merged list. If you want it to appear first, you can use `mkBefore`:

```nix
{ boot.kernelModules = mkBefore [ "kvm-intel" ]; }
```

This causes the `kvm-intel` kernel module to be loaded before any other
kernel modules.

For other types of options, a merge may not be possible. For instance,
if two modules define [](#opt-services.httpd.adminAddr),
`nixos-rebuild` will give an error:

```plain
The unique option `services.httpd.adminAddr' is defined multiple times, in `/etc/nixos/httpd.nix' and `/etc/nixos/configuration.nix'.
```

When that happens, it's possible to force one definition take precedence
over the others:

```nix
{ services.httpd.adminAddr = pkgs.lib.mkForce "bob@example.org"; }

Title: NixOS Configuration Modularity
Summary
NixOS configurations are modular, allowing users to split large `configuration.nix` files into multiple smaller files or share common configurations across different systems. Modules use the same syntax as `configuration.nix` and are included using the `imports` option. When multiple modules define the same option, NixOS attempts to merge them; for list-type options, values are concatenated (with `mkBefore` available to prepend), while for unique options, conflicting definitions result in an error unless `pkgs.lib.mkForce` is used to override other definitions.