NixOS Containers: Difference between revisions
update stateVersion |
Simplify and cleanup page |
||
| Line 1: | Line 1: | ||
Setup native [https://wiki.archlinux.org/title/systemd-nspawn systemd-nspawn] containers, which are running NixOS and are configured and managed by NixOS using the <code>containers</code> directive. | |||
See [[Docker]] page for OCI container (Docker, Podman) configuration. | |||
=== Configuration === | === Configuration === | ||
The following example creates a container called | The following example creates a container called webserver running a httpd web server. It will start automatically at boot and has its private network subnet. | ||
{{file|/etc/nixos/configuration.nix|nix|<nowiki> | {{file|/etc/nixos/configuration.nix|nix|<nowiki> | ||
| Line 16: | Line 16: | ||
}; | }; | ||
containers. | containers.webserver = { | ||
autoStart = true; | autoStart = true; | ||
privateNetwork = true; | privateNetwork = true; | ||
| Line 25: | Line 25: | ||
config = { config, pkgs, lib, ... }: { | config = { config, pkgs, lib, ... }: { | ||
services. | services.httpd = { | ||
enable = true; | enable = true; | ||
adminAddr = "admin@example.org"; | |||
}; | }; | ||
networking = { | |||
firewall.allowedTCPPorts = [ 80 ]; | |||
# Use systemd-resolved inside the container | # Use systemd-resolved inside the container | ||
# Workaround for bug https://github.com/NixOS/nixpkgs/issues/162686 | # Workaround for bug https://github.com/NixOS/nixpkgs/issues/162686 | ||
| Line 46: | Line 40: | ||
services.resolved.enable = true; | services.resolved.enable = true; | ||
system.stateVersion = "24.11"; | |||
}; | }; | ||
}; | }; | ||
| Line 99: | Line 94: | ||
Checking the status of the container | Checking the status of the container | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
# systemctl status container@ | # systemctl status container@webserver | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Login into the container | Login into the container | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
# nixos-container root-login | # nixos-container root-login webserver | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Start or stop a container | Start or stop a container | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
# nixos-container start | # nixos-container start webserver | ||
# nixos-container stop | # nixos-container stop webserver | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Destroy a container including its file system | Destroy a container including its file system | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
# nixos-container destroy | # nixos-container destroy webserver | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Further informations are available in the {{manual:nixos|sec=#ch-containers|chapter=NixOS manual}}. | Further informations are available in the {{manual:nixos|sec=#ch-containers|chapter=NixOS manual}}. | ||
== Troubleshooting == | == Troubleshooting == | ||