Skip to content

Instantly share code, notes, and snippets.

@573
Last active January 17, 2024 21:32
Show Gist options
  • Save 573/c1d73a4fd04b8f8ca63885393856f9ea to your computer and use it in GitHub Desktop.
Save 573/c1d73a4fd04b8f8ca63885393856f9ea to your computer and use it in GitHub Desktop.
Using a nixos qemu machine for fun and profit howto, as well creating iso files.

As pointed out here one needs just to:

# generate a machine
nix-build '<nixpkgs/nixos>' -A vm --arg configuration "{ imports = [ <nixpkgs/nixos/maintainers/scripts/openstack/openstack-image.nix> ]; }"
# the qcow2 image generated goes like this, see https://github.com/NixOS/nixpkgs/blob/master/nixos/maintainers/scripts/openstack/openstack-image.nix, is i. e. here then: /nix/store/40iv1f54ky0fxxil5wrzxpck9g121inv-nixos-disk-image/nixos.qcow2
nix-build '<nixpkgs/nixos>' -A config.system.build.openstackImage --arg configuration "{ imports = [ <nixpkgs/nixos/maintainers/scripts/openstack/openstack-image.nix> ]; }"
# starting vm, needs a while to boot though
./result/bin/run-noname-vm  

As well see the pointers given in the files' comments.

Comment:

In the vm

nixos-version

19.09pre191265.c4adeddb5f8 (Loris)

ls -1 /nix/store | sort -R -t - -k 2 | grep -i nixos-system

... v3861maxbs0yzvaiw5nqh4x21ch23i2h-nixos-system-nixos-19.09pre191265.c4adeddb5f8

ls /nix/store/v3861maxbs0yzvaiw5nqh4x21ch23i2h-nixos-system-nixos-19.09pre191265.c4adeddb5f8/init

Guess that is the init param for grub then

The usbstick-generation is even simplified as the isos generated seem to be hybrid, meaning one can burn them 1:1 to usbsticks, https://nixos.org/nixos/manual/index.html#sec-booting-from-usb

Sources:
https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/virtualisation/qemu-vm.nix (see options = { for tunings, i. e. the disk size, virtualisation.diskSize = 1024 * 2048; in machine-config.nix)
https://github.com/teto/nixpkgs/blob/33b0ad83e976ed6bb22b6862ca0637dd2fb765f7/nixos/modules/virtualisation/qemu-vm.nix#L100 (further options, such as setting ssh or adding devices can be tuned by injecting QEMU_OPTS as in QEMU_NET_OPTS=hostfwd=tcp::2221-:22 QEMU_OPTS="-cdrom /nix/store/435n08rj0w488342jfccigyjnvlfnwmc-nixos.iso/iso/nixos.iso -drive file=/tmp/bootima.img,if=none,id=newstick,format=raw -drive file=/mnt/c/temp/kubrick-usb-base.img,if=none,id=bootstick,format=raw -device nec-usb-xhci,id=xhci -device usb-storage,bus=xhci.0,drive=bootstick,port=1 -device usb-storage,bus=xhci.0,drive=newstick,port=2" ./result/bin/run-nixos-vm & followed by ssh -o "StrictHostKeyChecking=no" -i ~/.ssh/id_rsa_github root@localhost -p 2221 assumed machine-config.nix has openssh keys as in attached file)

https://nixos.mayflower.consulting/blog/2018/09/11/custom-images/

nix-community/home-manager#1154 (comment)

{ pkgs, lib, ... }:
with lib;
{
config = {
i18n.defaultLocale = "de_DE.UTF-8";
time.timeZone = "Europe/Paris";
services = {
timesyncd.enable = lib.mkDefault true;
openssh = {
enable = true;
permitRootLogin = "yes";
};
};
environment.systemPackages = with pkgs; [
dosfstools vim netcat curl openjdk8
];
users.extraUsers.root.password = "";
users.mutableUsers = false;
users.users.root = {
extraGroups = [ "wheel" ];
openssh.authorizedKeys.keys = [
"ssh-rsa ... "
];
};
# to keep usbstick booting, see https://github.com/NixOS/nixpkgs/issues/7132
#isoImage.volumeID = lib.mkDefault "NIXOS_ISO ";
isoImage.contents =
[ { source = /home/me/kubrick;
target = "/kubrick";
}
];
};
}
# nix-build '<nixpkgs/nixos>' -A config.system.build.isoImage --arg configuration "{ imports = [ ./iso.nix ]; }"
# or nixos-generate -f iso -c iso.nix
# This module defines a small NixOS installation CD. It does not
# contain any graphical stuff.
{config, pkgs, ...}:
{
imports = [
<nixpkgs/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix>
./iso-config.nix
# Provide an initial copy of the NixOS channel so that the user
# doesn't need to run "nix-channel --update" first.
<nixpkgs/nixos/modules/installer/cd-dvd/channel.nix>
];
}
{ pkgs, lib, ... }:
with lib;
{
imports = [
<nixpkgs/nixos/modules/profiles/qemu-guest.nix>
];
config = {
fileSystems."/" = {
device = "/dev/disk/by-label/nixos";
fsType = "ext4";
autoResize = true;
};
# Set VM disk size (in MB)
virtualisation.diskSize = 1024 * 2048;
boot.growPartition = true;
boot.kernelParams = [ "console=ttyS0" ];
boot.loader.grub.device = "/dev/vda";
boot.loader.timeout = 0;
i18n.defaultLocale = "de_DE.UTF-8";
time.timeZone = "Europe/Paris";
services = {
timesyncd.enable = lib.mkDefault true;
openssh = {
enable = true;
permitRootLogin = "yes";
};
};
environment.systemPackages = with pkgs; [
dosfstools vim netcat curl openjdk8
];
users.extraUsers.root.password = "";
users.mutableUsers = false;
users.users.root = {
extraGroups = [ "wheel" ];
openssh.authorizedKeys.keys = [
"ssh-rsa ... "
];
};
};
}
# nix-build '<nixpkgs/nixos>' -A vm --arg configuration "{ imports = [ ./vm.nix ]; }"
# see https://search.nix.gsc.io/?q=system.build.vm%20%3D&i=nope&files=&repos=
# nix-build '<nixpkgs/nixos>' -A vm -I nixos-config=vm.nix
# ./result/bin/run-nixos-vm
# or nixos-generate -f vm -c vm.nix --run
# nixos-generate --help
# nixos-generate --list
{ config, lib, pkgs, ... }:
with lib;
{
imports =
[ <nixpkgs/nixos/modules/installer/cd-dvd/channel.nix>
./vm-config.nix
];
system.build.qemuvmImage = import <nixpkgs/nixos/lib/make-disk-image.nix> {
inherit lib config;
pkgs = import <nixpkgs/nixos> { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package
diskSize = 8192;
format = "qcow2";
configFile = pkgs.writeText "configuration.nix"
''
{
imports = [ <./vm-config.nix> ];
}
'';
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment