The Revolutionary Power of Nix: Simplifying Package Management and Reproducible Builds
Nix is a powerful package manager and builds a system that allows for declarative, reproducible builds. In addition to managing software packages, Nix can also be used to build and manage container images.
Here's a step-by-step guide to building a container image with Nix:
Install Nix: macOS version
You can install Nix by running the following command in your terminal:
sh <(curl -L https://nixos.org/nix/install)
downloading Nix 2.15.0 binary tarball for aarch64-darwin from 'https://releases.nixos.org/nix/nix-2.15.0/nix-2.15.0-aarch64-darwin.tar.xz' to '/var/folders/jq/7hgry1mx35x2yqycw56_4_x40000gn/T/nix-binary-tarball-unpack.XXXXXXXXXX.wraXLUTl'...
Switching to the Multi-user Installer
Welcome to the Multi-User Nix Installation
This installation tool will set up your computer with the Nix package
manager. This will happen in a few stages:
1. Make sure your computer doesn't already have Nix. If it does, I
will show you instructions on how to clean up your old install.
2. Show you what I am going to install and where. Then I will ask
if you are ready to continue.
3. Create the system users and groups that the Nix daemon uses to run
builds.
4. Perform the basic installation of the Nix files daemon.
5. Configure your shell to import special Nix Profile files, so you
can use Nix.
6. Start the Nix daemon.
Would you like to see a more detailed list of what I will do?
[y/n] y
The script will ask a series of questions as mentioned and once done just follow the instruction and once it's successful.
Try it! Open a new terminal, and type:
$ nix-shell -p nix-info --run "nix-info -m"
#add channel
$ nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs
nix --version
nix (Nix) 2.15.0
Create a Nix expression for your container image:
A Nix expression is a declarative description of the software and dependencies that you want to include in your container image. For example, here's a Nix expression that installs the Apache web server:
{ pkgs ? import <nixpkgs> {} }:
pkgs.dockerTools.buildLayeredImage {
name = "my-nix-image";
tag = "latest";
contents = [
pkgs.bash
pkgs.coreutils
];
config.Cmd = [ "${pkgs.bash}/bin/bash" ];
}
This Nix expression defines a Docker image layer that includes the bash
and coreutils
packages, and specifies that the container should start with a Bash shell as the command. When you run nix-build
on this expression, Nix will build the Docker image and create a layer with the specified name and tag.
Please note : you have to save the file as myimage.nix <weyl>
nix-build myimage.nix
------------------
Done.
/nix/store/jjnflh4pmv0b4jd2a0n1vqaj1j4bsslv-my-nix-image.tar.gz
This will build the Docker image and save it to the nix/store
directory.
While this is a simpler example, it still demonstrates the declarative nature of Nix expressions and the ability to specify package dependencies and commands to run in a container image.
With more complex expressions, Nix can handle complex build and dependency management tasks with ease and provides powerful package management and reproducibility features.
Docker load/Import the image:
$ docker import /nix/store/jjnflh4pmv0b4jd2a0n1vqaj1j4bsslv-my-nix-image.tar.gz my-nix-image:latest
$ docker image ls | grep my-nix-image
my-nix-image latest 06e4dc851b64 1 second ago 2.09MB
This will create a Docker image with the name my-nginx-image
and the tag latest
from the tar archive. You can then use the docker image ls | grep my-nix-image
command to view the size of the image.
Overall, Nix is a powerful tool for building and managing container images. While it may take some time to learn and get comfortable with, Nix can do many things, among others it can also build Docker images. Not only can it build docker images, but it can build docker images better than docker build
itself.
More info - https://nixos.org/
Great demo - https://www.youtube.com/watch?v=WP_oAmV6C2U