From de7867c2262968f54c74a22b89e9ee0a19f3c83a Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 30 Nov 2024 12:26:26 +0100 Subject: [PATCH 1/6] runInLinuxVM: add simple structuredAttrs test --- pkgs/build-support/vm/test.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/build-support/vm/test.nix b/pkgs/build-support/vm/test.nix index 50dbfeb750be..620bf232a45c 100644 --- a/pkgs/build-support/vm/test.nix +++ b/pkgs/build-support/vm/test.nix @@ -24,6 +24,7 @@ in buildPatchelfInVM = runInLinuxVM patchelf; buildHelloInVM = runInLinuxVM hello; + buildStructuredAttrsHelloInVM = runInLinuxVM (hello.overrideAttrs { __structuredAttrs = true; }); buildPcmanrmInVM = runInLinuxVM (pcmanfm.overrideAttrs (old: { # goes out-of-memory with many cores From 3952f870fc7b7401aa06732b754c84cb94bacb61 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 30 Nov 2024 12:16:17 +0100 Subject: [PATCH 2/6] runInLinuxVM: clean up Those were left-over after 97ed6b4565e76286062e6942517a71ae4c9cac72. This also cleans up some confusion around TMPDIR. We had the following lines: mkdir xchg ... cd $TMPDIR ... path=$TMPDIR/xchg Those only worked because the **current directory** is the same as $TMPDIR. Both are /build by default. To refer to the same directory in two different ways is very confusing at best. --- pkgs/build-support/vm/default.nix | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/pkgs/build-support/vm/default.nix b/pkgs/build-support/vm/default.nix index 29ae21e50c4d..df014ae80c06 100644 --- a/pkgs/build-support/vm/default.nix +++ b/pkgs/build-support/vm/default.nix @@ -88,10 +88,6 @@ rec { set -- $(IFS==; echo $o) command=$2 ;; - out=*) - set -- $(IFS==; echo $o) - export out=$2 - ;; esac done @@ -153,7 +149,7 @@ rec { fi echo "starting stage 2 ($command)" - exec switch_root /fs $command $out + exec switch_root /fs $command ''; @@ -225,7 +221,6 @@ rec { -device virtio-rng-pci \ -virtfs local,path=${storeDir},security_model=none,mount_tag=store \ -virtfs local,path=/build,security_model=none,mount_tag=sa \ - -virtfs local,path=$TMPDIR/xchg,security_model=none,mount_tag=xchg \ ''${diskImage:+-drive file=$diskImage,if=virtio,cache=unsafe,werror=report} \ -kernel ${kernel}/${img} \ -initrd ${initrd}/initrd \ @@ -261,8 +256,6 @@ rec { cat > ./run-vm < Date: Sat, 30 Nov 2024 12:20:16 +0100 Subject: [PATCH 3/6] runInLinuxVM: minimize saved-env The export to saved-env was very intentionally done at the very beginning of vmRunCommand, even jumping through extra hoops just to avoid the PATH variable from polluting the saved variable. In 26eba25577388072ba13b1682860fe872ba39942 we loaded stdenv in the wrong place, we should do it after saving the previous environment. This is also more consistent with the order of how we load those values back in stage2Init. --- pkgs/build-support/vm/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/build-support/vm/default.nix b/pkgs/build-support/vm/default.nix index df014ae80c06..18044102c1bb 100644 --- a/pkgs/build-support/vm/default.nix +++ b/pkgs/build-support/vm/default.nix @@ -230,11 +230,11 @@ rec { vmRunCommand = qemuCommand: writeText "vm-run" '' + export > saved-env if [ -f "''${NIX_ATTRS_SH_FILE-}" ]; then source "$NIX_ATTRS_SH_FILE" fi source $stdenv/setup - export > saved-env PATH=${coreutils}/bin mkdir xchg From 437e6dbbb0f2cd8ec03da52fc2a0da0114e7c0b3 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 30 Nov 2024 12:57:40 +0100 Subject: [PATCH 4/6] runInLinuxVM: load stdenv/setup with fixed environment in stage2Init In [1] we started sourcing stdenv/setup in stage2Init to allow for structuredAttrs. We failed to take the changed NIX_BUILD_TOP etc. variables into account. We need to load stdenv/setup after changing them, because the structuredAttrs startup code makes use of it. [1]: 97ed6b4565e76286062e6942517a71ae4c9cac72 --- pkgs/build-support/vm/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/build-support/vm/default.nix b/pkgs/build-support/vm/default.nix index 18044102c1bb..e204a2b19543 100644 --- a/pkgs/build-support/vm/default.nix +++ b/pkgs/build-support/vm/default.nix @@ -169,7 +169,6 @@ rec { if [ -f "''${NIX_ATTRS_SH_FILE-}" ]; then source "$NIX_ATTRS_SH_FILE" fi - source $stdenv/setup export NIX_STORE=${storeDir} export NIX_BUILD_TOP=/tmp @@ -177,6 +176,7 @@ rec { export PATH=/empty cd "$NIX_BUILD_TOP" + source $stdenv/setup if ! test -e /bin/sh; then ${coreutils}/bin/mkdir -p /bin ${coreutils}/bin/ln -s ${bash}/bin/sh /bin/sh From 58570e75d9bddede2a854b0f7cb8df7f1818c541 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 30 Nov 2024 12:55:35 +0100 Subject: [PATCH 5/6] runInLinuxVM: refactor vmRunCommand This makes it simpler to copy more files to xchg for the structuredAttrs case in the next commit. --- pkgs/build-support/vm/default.nix | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pkgs/build-support/vm/default.nix b/pkgs/build-support/vm/default.nix index e204a2b19543..bb9c2c7368ac 100644 --- a/pkgs/build-support/vm/default.nix +++ b/pkgs/build-support/vm/default.nix @@ -230,16 +230,15 @@ rec { vmRunCommand = qemuCommand: writeText "vm-run" '' - export > saved-env + ${coreutils}/bin/mkdir xchg + export > xchg/saved-env + PATH=${coreutils}/bin + if [ -f "''${NIX_ATTRS_SH_FILE-}" ]; then source "$NIX_ATTRS_SH_FILE" fi source $stdenv/setup - PATH=${coreutils}/bin - mkdir xchg - mv saved-env xchg/ - eval "$preVM" if [ "$enableParallelBuilding" = 1 ]; then From d2593f01e1bf57aaf0235aec1d0ce6503a287a14 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 30 Nov 2024 12:23:24 +0100 Subject: [PATCH 6/6] runInLinuxVM: pass .attrs.sh explicitly instead of whole /build directory The approach taken in [1] breaks down as soon as vmRunCommand is manually called with an overriden TMPDIR, like disko does it. /build will just not be available. By moving the .attrs.sh file into the xchg folder explicitly, we can all the "exchange infrastructure" the same as before, thus avoid more breakage. This reverts some parts of [1]. [1]: 97ed6b4565e76286062e6942517a71ae4c9cac72 --- pkgs/build-support/vm/default.nix | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/pkgs/build-support/vm/default.nix b/pkgs/build-support/vm/default.nix index bb9c2c7368ac..1f4f8b35b663 100644 --- a/pkgs/build-support/vm/default.nix +++ b/pkgs/build-support/vm/default.nix @@ -125,15 +125,15 @@ rec { mkdir -p /fs${storeDir} mount -t 9p store /fs${storeDir} -o trans=virtio,version=9p2000.L,cache=loose,msize=131072 - echo "mounting host's build directory..." - mkdir -p /fs/build - mount -t 9p sa /fs/build -o trans=virtio,version=9p2000.L,cache=loose,msize=131072 - mkdir -p /fs/tmp /fs/run /fs/var mount -t tmpfs -o "mode=1777" none /fs/tmp mount -t tmpfs -o "mode=755" none /fs/run ln -sfn /run /fs/var/run + echo "mounting host's temporary directory..." + mkdir -p /fs/tmp/xchg + mount -t 9p xchg /fs/tmp/xchg -o trans=virtio,version=9p2000.L,msize=131072 + mkdir -p /fs/proc mount -t proc none /fs/proc @@ -165,9 +165,11 @@ rec { stage2Init = writeScript "vm-run-stage2" '' #! ${bash}/bin/sh set -euo pipefail - source /build/xchg/saved-env - if [ -f "''${NIX_ATTRS_SH_FILE-}" ]; then - source "$NIX_ATTRS_SH_FILE" + source /tmp/xchg/saved-env + if [ -f /tmp/xchg/.attrs.sh ]; then + source /tmp/xchg/.attrs.sh + export NIX_ATTRS_JSON_FILE=/tmp/xchg/.attrs.json + export NIX_ATTRS_SH_FILE=/tmp/xchg/.attrs.sh fi export NIX_STORE=${storeDir} @@ -177,6 +179,7 @@ rec { cd "$NIX_BUILD_TOP" source $stdenv/setup + if ! test -e /bin/sh; then ${coreutils}/bin/mkdir -p /bin ${coreutils}/bin/ln -s ${bash}/bin/sh /bin/sh @@ -201,7 +204,7 @@ rec { declare -a argsArray=() concatTo argsArray origArgs "$origBuilder" "''${argsArray[@]}" - echo $? > /build/xchg/in-vm-exit + echo $? > /tmp/xchg/in-vm-exit ${busybox}/bin/mount -o remount,ro dummy / @@ -220,7 +223,7 @@ rec { -nographic -no-reboot \ -device virtio-rng-pci \ -virtfs local,path=${storeDir},security_model=none,mount_tag=store \ - -virtfs local,path=/build,security_model=none,mount_tag=sa \ + -virtfs local,path=xchg,security_model=none,mount_tag=xchg \ ''${diskImage:+-drive file=$diskImage,if=virtio,cache=unsafe,werror=report} \ -kernel ${kernel}/${img} \ -initrd ${initrd}/initrd \ @@ -235,6 +238,7 @@ rec { PATH=${coreutils}/bin if [ -f "''${NIX_ATTRS_SH_FILE-}" ]; then + cp $NIX_ATTRS_JSON_FILE $NIX_ATTRS_SH_FILE xchg source "$NIX_ATTRS_SH_FILE" fi source $stdenv/setup