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.

Already available here: https://hub.docker.com/r/nixos/nix

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 https://github.com/cloudwatt/nix-container-images#reuse-nixos-modules.

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.