8

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?

2
  • I believe this just builds the config rather than runs? Perhaps the title could be changed. Jun 29, 2021 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, 2021 at 16:30

1 Answer 1

6

Thanks to clever and infinisil:

nix-build '<nixpkgs/nixos>' -A vm -I nixos-config=./configuration.nix
2
  • You can mark your answer as accepted since this is, well, the right answer Oct 5, 2019 at 10:06
  • 1
    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, 2021 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .