Skip to content

Commit

Permalink
Enable full qemu emulation with TCG fallback when cross compiling ima…
Browse files Browse the repository at this point in the history
…ges via make-disk-image
  • Loading branch information
prinzdezibel committed Dec 5, 2024
1 parent 7889853 commit 962f359
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 9 deletions.
15 changes: 12 additions & 3 deletions nixos/lib/make-disk-image.nix
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,11 @@ let format' = format; in let
nixos-enter
nix
systemdMinimal
coreutils
findutils
util-linux
]
++ lib.optional deterministic gptfdisk
++ stdenv.initialPath);
++ lib.optional deterministic gptfdisk);

# I'm preserving the line below because I'm going to search for it across nixpkgs to consolidate
# image building logic. The comment right below this now appears in 4 different places in nixpkgs :)
Expand Down Expand Up @@ -541,9 +543,16 @@ let format' = format; in let
mkdir -p $out/nix-support
echo "file ${format}-image $out/${filename}" >> $out/nix-support/hydra-build-products
'';

buildSystem = pkgs.stdenv.buildPlatform.system;
hostSystem = pkgs.stdenv.hostPlatform.system;
crossPkgs = if (hostSystem != buildSystem) then
import ../.. /*nixpkgs*/ { system = hostSystem; }
else
pkgs;

buildImage = pkgs.vmTools.runInLinuxVM (
pkgs.runCommand name {
crossPkgs.runCommand name {
preVM = prepareImage + lib.optionalString touchEFIVars createEFIVars;
buildInputs = with pkgs; [ util-linux e2fsprogs dosfstools ];
postVM = moveOrConvertImage + createHydraBuildProducts + postVM;
Expand Down
2 changes: 1 addition & 1 deletion nixos/lib/qemu-common.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ rec {
guestSystem = pkgs.stdenv.hostPlatform.system;

linuxHostGuestMatrix = {
x86_64-linux = "${qemuPkg}/bin/qemu-kvm -cpu max";
x86_64-linux = "${qemuPkg}/bin/qemu-system-x86_64 -machine q35,accel=kvm:tcg -cpu max";
armv7l-linux = "${qemuPkg}/bin/qemu-system-arm -machine virt,accel=kvm:tcg -cpu max";
aarch64-linux = "${qemuPkg}/bin/qemu-system-aarch64 -machine virt,gic-version=max,accel=kvm:tcg -cpu max";
powerpc64le-linux = "${qemuPkg}/bin/qemu-system-ppc64 -machine powernv";
Expand Down
8 changes: 7 additions & 1 deletion pkgs/build-support/rust/hooks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
, rust
, rustc
, stdenv
, python3

# This confusingly-named parameter indicates the *subdirectory of
# `target/` from which to copy the build artifacts. It is derived
# from a stdenv platform (or a JSON file).
, target ? stdenv.hostPlatform.rust.cargoShortTarget
}:

let
inherit (python3) pythonOnBuildForHost;
pythonInterpreter = pythonOnBuildForHost.interpreter;
in
{
cargoBuildHook = callPackage ({ }:
makeSetupHook {
Expand Down Expand Up @@ -76,6 +80,7 @@
};
} ./cargo-setup-hook.sh) {};


maturinBuildHook = callPackage ({ pkgsHostTarget }:
makeSetupHook {
name = "maturin-build-hook.sh";
Expand All @@ -86,6 +91,7 @@
];
substitutions = {
inherit (rust.envVars) rustTargetPlatformSpec setEnv;
pythonInterpreter = builtins.elemAt (builtins.match "([0-9]+\.[0-9]+)\.*" python3.version) 0;
};
} ./maturin-build-hook.sh) {};

Expand Down
1 change: 1 addition & 0 deletions pkgs/build-support/rust/hooks/maturin-build-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ maturinBuildHook() {

local flagsArray=(
"--jobs=$NIX_BUILD_CORES"
"--interpreter=@pythonInterpreter@"
"--offline"
"--target" "@rustTargetPlatformSpec@"
"--manylinux" "off"
Expand Down
29 changes: 26 additions & 3 deletions pkgs/build-support/vm/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ in
rec {
qemu-common = import ../../../nixos/lib/qemu-common.nix { inherit lib pkgs; };

qemu = buildPackages.qemu_kvm;
#qemu = buildPackages.qemu_kvm;
qemu = buildPackages.qemu;

modulesClosure = pkgs.makeModulesClosure {
inherit kernel rootModules;
Expand Down Expand Up @@ -161,6 +162,27 @@ rec {
];
};

# Switch standard build environment to target host platform
stage2Stdenv =
let
buildSystem = pkgs.stdenv.buildPlatform.system;
hostSystem = pkgs.stdenv.hostPlatform.system;
in if (buildSystem == hostSystem) then stdenv else
let
targetArch = if ( hostSystem == "x86_64-linux") then
"gnu64" else
"aarch64-multiplatform";
in
pkgs.stdenv.override {
buildPlatform = pkgs.stdenv.hostPlatform;
cc = null;
preHook = "";
allowedRequisites = null;
initialPath = [pkgs.pkgsCross.${targetArch}.busybox];
shell = "${pkgs.pkgsCross.${targetArch}.bash}/bin/bash";
extraNativeBuildInputs = [];
}
;

stage2Init = writeScript "vm-run-stage2" ''
#! ${bash}/bin/sh
Expand All @@ -178,8 +200,9 @@ rec {
export PATH=/empty
cd "$NIX_BUILD_TOP"
source $stdenv/setup
source ${stage2Stdenv}/setup
export stdenv=${stage2Stdenv}
if ! test -e /bin/sh; then
${coreutils}/bin/mkdir -p /bin
${coreutils}/bin/ln -s ${bash}/bin/sh /bin/sh
Expand Down
3 changes: 2 additions & 1 deletion pkgs/by-name/ma/maturin/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ rustPlatform.buildRustPackage rec {

cargoHash = "sha256-yLKt/Xml7ig6QG3T5Qn39tW7U5NIN1hSOaLiSRMiy5I=";

buildInputs = lib.optionals stdenv.hostPlatform.isDarwin [

buildInputs = [python3] ++ lib.optionals stdenv.hostPlatform.isDarwin [
darwin.apple_sdk.frameworks.Security
libiconv
];
Expand Down
2 changes: 2 additions & 0 deletions pkgs/development/python-modules/rpds-py/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
rustc,
rustPlatform,
libiconv,
python3
}:

buildPythonPackage rec {
Expand All @@ -35,6 +36,7 @@ buildPythonPackage rec {
rustPlatform.maturinBuildHook
cargo
rustc
python3
];

buildInputs = lib.optionals stdenv.hostPlatform.isDarwin [ libiconv ];
Expand Down

0 comments on commit 962f359

Please sign in to comment.