Skip to content

Commit 33ad0fd

Browse files
committed
feat(qemu): Add Nix support for building and running aarch64 VM
1 parent b17edf1 commit 33ad0fd

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

16_qemu/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Demonstrate how to get aarch64 machine running on qemu (ubuntu)
44

5+
NOTES:
6+
7+
* `armv6l-linux` & `armv7l-linux` are 32-bit, whereas `aarch64` is 64-bit
8+
59
## Prereqs
610

711
```sh
@@ -24,6 +28,7 @@ qemu-system-arm -M help
2428
qemu-system-aarch64 -version
2529
qemu-system-aarch64 -M help
2630

31+
# on debian
2732
apt-file list qemu-kvm
2833
apt-file list qemu-system
2934
```

16_qemu/nix/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# NIX
2+
3+
Build an image with nix and run as aarch64.
4+
5+
```sh
6+
just nix
7+
```
8+
9+
## Resources
10+
11+
* https://gist.github.com/lheckemann/63c52f2115346e6c9bbc6ecdfde9f43b
12+
* Spawn a Linux virtual machine on Arm using QEMU (KVM) [here](https://community.arm.com/oss-platforms/w/docs/510/spawn-a-linux-virtual-machine-on-arm-using-qemu-kvm)

16_qemu/nix/flake.lock

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

16_qemu/nix/flake.nix

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
description = "Nix flake to build and run a NixOS VM for aarch64";
3+
4+
inputs = {
5+
nixpkgs.url = "nixpkgs/nixos-24.11";
6+
};
7+
8+
outputs = { self, nixpkgs }:
9+
let
10+
pkgs = import nixpkgs { system = "x86_64-linux"; };
11+
pkgsAarch64 = import nixpkgs { system = "aarch64-linux"; };
12+
13+
iso = (pkgsAarch64.nixos {
14+
imports = [ "${nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-base.nix" ];
15+
}).config.system.build.isoImage;
16+
17+
vmScript = pkgs.writeScriptBin "run-nixos-vm" ''
18+
#!${pkgs.runtimeShell}
19+
${pkgs.qemu}/bin/qemu-system-aarch64 \
20+
-machine virt,gic-version=max \
21+
-cpu max \
22+
-m 2G \
23+
-smp 4 \
24+
-drive file=$(echo ${iso}/iso/*.iso),format=raw,readonly=on \
25+
-nographic \
26+
-bios ${pkgsAarch64.OVMF.fd}/FV/QEMU_EFI.fd
27+
'';
28+
29+
in {
30+
defaultPackage.x86_64-linux = vmScript;
31+
};
32+
}

16_qemu/nix/justfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env just --justfile
2+
# ^ A shebang isn't required, but allows a justfile to be executed
3+
# like a script, with `./justfile test`, for example.
4+
5+
set dotenv-load := true
6+
7+
# default lists actions
8+
default:
9+
@just -f justfile --list
10+
11+
nix:
12+
#!/usr/bin/env bash
13+
set -eufo pipefail
14+
nix run

0 commit comments

Comments
 (0)