Skip to content

Commit d060364

Browse files
committed
OVMF: enable "cargo xtest run" under NixOS
1 parent 4dd7b7b commit d060364

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ trim_trailing_whitespace = true
1212
indent_style = space
1313
indent_size = 4
1414

15-
[*.{md,json}]
15+
[*.{json,md,nix,yml}]
1616
indent_style = space
1717
indent_size = 2

nix/nixpkgs.nix

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Pinned nixpkgs version.
2+
3+
let
4+
# Picked a recent commit from the nixos-22.11-small branch.
5+
# https://github.com/NixOS/nixpkgs/tree/nixos-22.11-small
6+
#
7+
# When you change this, also change the sha256 hash!
8+
rev = "a45745ac9e4e1eb86397ab22e2a8823120ab9a4c";
9+
in
10+
builtins.fetchTarball {
11+
url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
12+
sha256 = "sha256:1acllp8yxp1rwncxsxnxl9cwkm97wxfnd6ryclmvll3sa39j9b1z";
13+
}

shell.nix

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
let
2+
pkgsSrc = import ./nix/nixpkgs.nix;
3+
pkgs = import pkgsSrc {};
4+
in
5+
pkgs.mkShell rec {
6+
# CLI Utilities
7+
nativeBuildInputs = with pkgs; [
8+
];
9+
10+
# Header Files, Runtime Dependencies
11+
buildInputs = with pkgs; [
12+
];
13+
14+
# Set ENV var "OVMF"
15+
OVMF="${pkgs.OVMF.fd}";
16+
}

xtask/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ heck = "0.4.0"
1313
itertools = "0.10.5"
1414
mbrman = "0.5.1"
1515
nix = "0.26.1"
16+
os_info = { version = "3.6.0", default-features = false }
1617
proc-macro2 = "1.0.46"
1718
quote = "1.0.21"
1819
regex = "1.5.4"

xtask/src/qemu.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use std::io::{BufRead, BufReader, Read, Write};
1414
use std::path::{Path, PathBuf};
1515
use std::process::{Child, Command, Stdio};
1616
use tempfile::TempDir;
17+
#[cfg(target_os = "linux")]
18+
use {std::fs::Permissions, std::os::unix::fs::PermissionsExt};
1719

1820
#[derive(Clone, Copy, Debug)]
1921
enum OvmfFileType {
@@ -127,6 +129,34 @@ impl OvmfPaths {
127129
}
128130
}
129131

132+
/// Get the NixOS OVMF paths for the given guest arch.
133+
fn nixos(arch: UefiArch) -> Option<Self> {
134+
let os_info = os_info::get();
135+
if os_info.os_type() != os_info::Type::NixOS {
136+
return None;
137+
}
138+
let path = std::env::var_os("OVMF").expect("Must have OVMF env var. Run '$ nix-shell'");
139+
let path = path.to_str().unwrap();
140+
let paths = match arch {
141+
// Package "edk2-aarch64".
142+
UefiArch::AArch64 => Self {
143+
code: format!("{path}/FV/AAVMF_CODE.fd").into(),
144+
vars: format!("{path}/FV/AAVMF_VARS.fd").into(),
145+
},
146+
// Package "edk2-ovmf-ia32".
147+
UefiArch::IA32 => Self {
148+
code: format!("{path}/FV/OVMF_CODE.fd").into(),
149+
vars: format!("{path}/FV/OVMF_VARS.fd").into(),
150+
},
151+
// Package "edk2-ovmf".
152+
UefiArch::X86_64 => Self {
153+
code: format!("{path}/FV/OVMF_CODE.fd").into(),
154+
vars: format!("{path}/FV/OVMF_VARS.fd").into(),
155+
},
156+
};
157+
Some(paths)
158+
}
159+
130160
/// Get the Windows OVMF paths for the given guest arch.
131161
fn windows(arch: UefiArch) -> Self {
132162
match arch {
@@ -157,6 +187,9 @@ impl OvmfPaths {
157187
}
158188
candidates.push(Self::debian_linux(arch));
159189
candidates.push(Self::fedora_linux(arch));
190+
if let Some(paths) = Self::nixos(arch) {
191+
candidates.push(paths);
192+
}
160193
}
161194
if platform::is_windows() {
162195
candidates.push(Self::windows(arch));
@@ -497,6 +530,10 @@ pub fn run_qemu(arch: UefiArch, opt: &QemuOpt) -> Result<()> {
497530
// versions of OVMF won't boot if the vars file isn't writeable.
498531
let ovmf_vars = tmp_dir.join("ovmf_vars");
499532
fs_err::copy(&ovmf_paths.vars, &ovmf_vars)?;
533+
// Necessary, as for example on NixOS, the files are read-only inside
534+
// the Nix store.
535+
#[cfg(target_os = "linux")]
536+
fs_err::set_permissions(&ovmf_vars, Permissions::from_mode(0o666))?;
500537

501538
add_pflash_args(&mut cmd, &ovmf_paths.code, PflashMode::ReadOnly);
502539
add_pflash_args(&mut cmd, &ovmf_vars, PflashMode::ReadWrite);

0 commit comments

Comments
 (0)