Nixos in docker image

How do I create a docker image containing a given NixOS system?
I know of dockerTools and of the docker-image profile,but this

serverImg= nixos({config,lib,...}:{
    imports=[
      ./configuration.nix
      <nixpkgs/nixos/modules/profiles/docker-container.nix>
    ];
  });
dockerImg=with dockerTools;buildLayeredImage{
    name="test-server";
    contents=[
      # "${serverImg.toplevel}/sw" 
      # (runCommandNoCC "empty"{}"")
      coreutils
    ];
    extraCommands=''
      #!${pkgs.runtimeShell}
      touch /etc/NIXOS
      ln -s ${serverImg.toplevel} /run/current-system
      ${serverImg.toplevel}/bin/switch-to-configuration switch
    '';
    config={
      ExposedPorts."8080"={};
      Cmd="manage shell_plus";
    };
  };

does not build,even through serverImg does.

1 Like

Already available here: Docker

Hmm, isn’t that an Alpine Linux based image with Nix installed?

I don’t think you could run switch-to-configuration as an extraCommand since IIRC, it requires access to / for instance. And if you want to run NixOS services, you will also need to run systemd in the container.

This is not exactly what you want to do, but a related project is GitHub - cloudwatt/nix-container-images: Write container images as NixOS machines.

Yes, it is a Docker image with Nix installed.

So,apparently (pkgs.nixos<nixpkgs/nixos/virtualisation/docker-image.nix>).tarball is,in fact,a docker image,but apparently it needs to run as --privileged.
Neither in the NixOS manual nor in the NixOS wiki this is even suggested,and I stumbled on this file on accident while loooking for the docker-image.nix profile.

3 Likes