In the amazing nixos, it is possible to launch a lightweight container that will run a system whose configuration.nix file is different from the host configuration by first compiling using:

$ nixos-rebuild build-vm -I nixos-config=./configuration.nix

and running via:

$ ./result/bin/run-*-vm

However, the first command cannot work on operating systems where nixos is not installed, like debian + nixpkgs, because nixos-rebuild does not exists.

How can I still use build-vm in non-nixos systems?

  • I believe this just builds the config rather than runs? Perhaps the title could be changed. Jun 29 at 14:42
  • 1
    @ChrisStryczynski well you can easily run the VM once it's built using something like ./result/bin/run-*-vm. Building without running may not be that interesting (except when you only want to check if it builds), that's why I was interested to actually run the VM. I'll edit the question to make it clear.
    – tobiasBora
    Jun 29 at 16:30

Thanks to clever and infinisil:

nix-build '<nixpkgs/nixos>' -A vm -I nixos-config=./configuration.nix
  • You can mark your answer as accepted since this is, well, the right answer Oct 5 '19 at 10:06
  • Keep in mind that such nix-build invocation is dependent on $NIX_PATH environment variable, specifically nixpkgs path. So it can produce different results. You can use NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/8f9022789211d6be645e5f97a258e0b6e9e24c9e.tar.gz nix-build ... or nix-build -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/8f9022789211d6be645e5f97a258e0b6e9e24c9e.tar.gz ... for deterministic builds. Or use nix flakes which avoids the issue.
    – AleXoundOS
    Jul 24 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.