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,...}:{
dockerImg=with dockerTools;buildLayeredImage{
      # "${serverImg.toplevel}/sw" 
      # (runCommandNoCC "empty"{}"")
      touch /etc/NIXOS
      ln -s ${serverImg.toplevel} /run/current-system
      ${serverImg.toplevel}/bin/switch-to-configuration switch
      Cmd="manage shell_plus";

does not build,even through serverImg does.

Already available here:

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

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.