1 # Declarative Container Specification {#sec-declarative-containers}
3 You can also specify containers and their configuration in the host's
4 `configuration.nix`. For example, the following specifies that there
5 shall be a container named `database` running PostgreSQL:
11 { config, pkgs, ... }:
12 { services.postgresql.enable = true;
13 services.postgresql.package = pkgs.postgresql_14;
19 If you run `nixos-rebuild switch`, the container will be built. If the
20 container was already running, it will be updated in place, without
21 rebooting. The container can be configured to start automatically by
22 setting `containers.database.autoStart = true` in its configuration.
24 By default, declarative containers share the network namespace of the
25 host, meaning that they can listen on (privileged) ports. However, they
26 cannot change the network configuration. You can give a container its
27 own network as follows:
31 containers.database = {
32 privateNetwork = true;
33 hostAddress = "192.168.100.10";
34 localAddress = "192.168.100.11";
39 This gives the container a private virtual Ethernet interface with IP
40 address `192.168.100.11`, which is hooked up to a virtual Ethernet
41 interface on the host with IP address `192.168.100.10`. (See the next
42 section for details on container networking.)
44 To disable the container, just remove it from `configuration.nix` and
46 switch`. Note that this will not delete the root directory of the
47 container in `/var/lib/nixos-containers`. Containers can be destroyed using
48 the imperative method: `nixos-container destroy foo`.
50 Declarative containers can be started and stopped using the
51 corresponding systemd service, e.g.
52 `systemctl start container@database`.