Created
April 10, 2025 04:49
-
-
Save tomdavidson/39451d1a0e6e7642652afc3f6f763ec3 to your computer and use it in GitHub Desktop.
qemu-devenv.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ pkgs, lib, config, ... }: { | |
inputs.microvm.url = "github:astro/microvm.nix"; | |
packages = [ | |
pkgs.qemu | |
pkgs.microvm | |
pkgs.utillinux # For kvm-ok check | |
]; | |
env = { | |
VM_NAME = builtins.baseNameOf (builtins.toString ./.); | |
KVM_AVAILABLE = let | |
kvmCheck = pkgs.runCommand "check-kvm" {} '' | |
${pkgs.utillinux}/bin/kvm-ok >/dev/null 2>&1 && touch $out || exit 1 | |
''; | |
in if builtins.pathExists kvmCheck then "1" else "0"; | |
}; | |
tasks.vm-setup = { | |
exec = '' | |
mkdir -p .devenv/microvm | |
cat > .devenv/microvm/flake.nix <<EOF | |
{ | |
inputs.microvm.url = "github:astro/microvm.nix"; | |
inputs.nixpkgs.url = "nixpkgs/nixos-unstable"; | |
outputs = { self, microvm, nixpkgs, ... }: { | |
nixosConfigurations.${config.env.VM_NAME} = nixpkgs.lib.nixosSystem { | |
system = "x86_64-linux"; | |
modules = [ | |
microvm.nixosModules.microvm | |
{ | |
microvm = { | |
hypervisor = "qemu"; | |
vcpu = 2; | |
mem = 2048; | |
shares = [{ | |
source = "\${builtins.toString ./.}"; | |
mountPoint = "/project"; | |
proto = "virtiofs"; | |
}]; | |
qemu.extraArgs = lib.optional (config.env.KVM_AVAILABLE == "0") "-enable-kvm"; | |
writableStoreOverlay = "/nix/.rwstore"; | |
}; | |
users.users.root.password = ""; | |
services.getty.autologinUser = "root"; | |
environment.sessionVariables = config.env; | |
environment.systemPackages = config.packages; | |
} | |
]; | |
}; | |
}; | |
} | |
EOF | |
''; | |
onEnter = true; | |
}; | |
enterShell = '' | |
if [ -z "$IN_MICROVM" ]; then | |
export IN_MICROVM=1 | |
devenv tasks run vm-setup | |
echo "Starting QEMU MicroVM (KVM: ${config.env.KVM_AVAILABLE})..." | |
nix run .devenv/microvm#nixosConfigurations.${config.env.VM_NAME}.config.microvm.declaredRunner | |
exit | |
fi | |
''; | |
cachix.pull = [ "devenv" ]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment