NixOS 101 Course / Section 5: NixOS containers
Premium

5.2 Imperative NixOS containers with nixos-container

Lesson 21 of 28 · 3:32

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Imperative Container Management Overview
  2. 0:28 Setting Up nixos-container and Host Requirements
  3. 0:58 Creating and Listing the Container
  4. 1:35 Starting the Container and Checking Status
  5. 2:06 Logging In and Verifying the Service
  6. 2:45 Accessing the Service and Host Integration
Transcript

Imperative Container Management Overview

0:00 Okay, back to container management but this time imperatively. So in the last video we just had a look at a declarative container specification which is cool because you can simply change your NixOS configuration, wrap some service description and config inside the container stuff and then rebuild your system and go. But that means that you also will have to rebuild your whole system whenever you change some configuration part even in a container because the container and your system are

Setting Up nixos-container and Host Requirements

0:31 one config file and you need to run NixOS. If you're running Ubuntu or whatever else legacy distribution or you simply want to have imperative container management then this is for you. So in general there's a tool called nixos-container and if you don't have it installed you can nix shell it quickly, nix shell nixpkgs#nixos-container, right? If you're running NixOS as a host system for that enable boot.enableContainers equals true

Creating and Listing the Container

1:01 in your config briefly but on other systems you don't need that. Now with that tool we can create a new configuration, I already did that. So I have the well-known hello-as-a-service.nix file and then we can look into container.nix which basically just says we import the service definition on declaration and then enable it that should be easy now and now we can say sudo nixos-container create hello and then config file container.nix and now it creates a new container which actually has

The full transcript comes with the lesson.

About this lesson

Not every host runs NixOS, and not every container change should need a full system rebuild. This lesson creates, starts, and enters a NixOS container with the nixos-container tool, which also works on Ubuntu and other distributions.

In this video

  • Getting nixos-container
  • Creating a container from a small configuration file
  • Listing, starting, and logging into containers

After this lesson

You can run NixOS services in containers on any Linux machine that has Nix.

Links