From: Alyssa Ross <hi@alyssa.is>
To: devel@spectrum-os.org
Subject: [PATCH 19/22] vm/app/nix: add
Date: Mon, 10 Oct 2022 23:28:59 +0000 [thread overview]
Message-ID: <20221010232909.1953738-20-hi@alyssa.is> (raw)
In-Reply-To: <20221010232909.1953738-1-hi@alyssa.is>
This is a VM that provides a shell set up to run Nix. It has network
access, and full filesystem access to the user data partition. The
included vm-rebuild command makes it possible to build Spectrum VMs
with Nix. It's expected that vm-config/vms.nix be a Nix expression
that builds a directory of Spectrum VM definitions. Code from
Spectrum (i.e. vm-lib) can be placed at vm-config/spectrum and will
appear as <spectrum> in the NIX_PATH. This code is not included as
part of the Nix VM, because it would be bad for reproducibility if
updating the host system changed the Nix expressions used to build
VMs.
Nix-built VMs are each individually symlinked into svc/data, so that
managing VMs with Nix is not all-or-nothing.
vm-rebuild will not yet remove symlinks pointing to VMs that no longer
exist in the current generation, but that shouldn't be difficult to
fix — just delete any broken symlinks pointing into the Nix store.
Signed-off-by: Alyssa Ross <hi@alyssa.is>
---
host/initramfs/extfs.nix | 2 ++
vm/app/nix/bin/vm-rebuild | 25 +++++++++++++++++++++++
vm/app/nix/default.nix | 43 +++++++++++++++++++++++++++++++++++++++
3 files changed, 70 insertions(+)
create mode 100755 vm/app/nix/bin/vm-rebuild
create mode 100644 vm/app/nix/default.nix
diff --git a/host/initramfs/extfs.nix b/host/initramfs/extfs.nix
index a510c02..9f00793 100644
--- a/host/initramfs/extfs.nix
+++ b/host/initramfs/extfs.nix
@@ -12,6 +12,7 @@ let
appvm-catgirl = import ../../vm/app/catgirl.nix { inherit config; };
appvm-lynx = import ../../vm/app/lynx.nix { inherit config; };
appvm-mg = import ../../vm/app/mg.nix { inherit config; };
+ appvm-nix = import ../../vm/app/nix { inherit config; };
in
runCommand "ext.ext4" {
@@ -26,6 +27,7 @@ runCommand "ext.ext4" {
tar -C ${appvm-catgirl} -c . | tar -C svc/data/appvm-catgirl -x
tar -C ${appvm-lynx} -c . | tar -C svc/data/appvm-lynx -x
tar -C ${appvm-mg} -c . | tar -C svc/data/appvm-mg -x
+ tar -C ${appvm-nix} -c . | tar -C svc/data/appvm-nix -x
mkfs.ext4 -d . $out 16T
resize2fs -M $out
diff --git a/vm/app/nix/bin/vm-rebuild b/vm/app/nix/bin/vm-rebuild
new file mode 100755
index 0000000..98eae10
--- /dev/null
+++ b/vm/app/nix/bin/vm-rebuild
@@ -0,0 +1,25 @@
+#!/bin/execlineb -S1
+# SPDX-License-Identifier: EUPL-1.2+
+# SPDX-FileCopyrightText: 2022 Alyssa Ross <hi@alyssa.is>
+
+if -n {
+ if -n { test $# -eq 1 -a $1 = switch }
+ fdmove -c 1 2
+ echo "Usage: ${0} switch"
+}
+
+cd /run/virtiofs/virtiofs0
+
+backtick -E dir { mktemp -d }
+foreground {
+ if { nix-build -o ${dir}/system <spectrum-vms> }
+ if { nix-env -p nix/var/nix/profiles/vms --set ${dir}/system }
+ backtick -E vmsdir { resolve_in_root . nix/var/nix/profiles/vms }
+ cd $vmsdir
+ elglob -0 glob *
+ forx -E vm { $glob }
+ ln -s /nix/var/nix/profiles/vms/${vm} /run/virtiofs/virtiofs0/svc/data
+}
+importas -iu ? ?
+background { rm -rf $dir }
+exit $?
diff --git a/vm/app/nix/default.nix b/vm/app/nix/default.nix
new file mode 100644
index 0000000..9427ca4
--- /dev/null
+++ b/vm/app/nix/default.nix
@@ -0,0 +1,43 @@
+# SPDX-License-Identifier: MIT
+# SPDX-FileCopyrightText: 2022 Alyssa Ross <hi@alyssa.is>
+
+{ config ? import ../../../../nix/eval-config.nix {} }:
+
+import ../../make-vm.nix { inherit config; } {
+ providers.net = [ "netvm" ];
+ sharedDirs.virtiofs0.path = "/ext";
+ run = config.pkgs.pkgsStatic.callPackage (
+ { lib, runCommand, writeScript, nix }:
+ let
+ inherit (lib) concatStringsSep const hasSuffix makeBinPath;
+
+ bin = builtins.filterSource (name: _type:
+ name == toString bin/. || name == toString bin/vm-rebuild) ./.;
+
+ nixPath = [
+ "nixpkgs=https://nixos.org/channels/nixos-unstable/nixexprs.tar.xz"
+ "spectrum=/run/virtiofs/virtiofs0/vm-config/spectrum"
+ "spectrum-vms=/run/virtiofs/virtiofs0/vm-config/vms.nix"
+ ];
+
+ resolve_in_root = import ../../../tools/resolve_in_root {
+ config = config // { pkgs = config.pkgs.pkgsStatic; };
+ };
+ in
+ writeScript "run-nix" ''
+ #!/bin/execlineb -P
+ importas -i PATH PATH
+ export NIX_CONFIG "build-users-group ="
+ export NIX_REMOTE /run/virtiofs/virtiofs0
+ export NIX_PATH ${concatStringsSep ":" nixPath}
+ export PATH ${makeBinPath [ bin nix resolve_in_root ]}:''${PATH}
+ export XDG_CACHE_HOME /run/cache
+
+ # FIXME: can be removed when we have nix#7070.
+ export XDG_DATA_HOME /run/data
+
+ if { /etc/mdev/wait virtiofs0 }
+ /bin/sh -il
+ ''
+ ) { };
+}
--
2.37.1
next prev parent reply other threads:[~2022-10-10 23:33 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-10 23:28 [PATCH 00/22] Implement managing VMs with Nix Alyssa Ross
2022-10-10 23:28 ` [PATCH 01/22] host/start-vm: use MAP_SHARED memory for VMs Alyssa Ross
2023-02-26 19:17 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 02/22] host/start-vm: implement shared directories Alyssa Ross
2023-02-26 19:17 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 03/22] host/rootfs: generate virtiofsd services Alyssa Ross
2023-02-26 19:17 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 04/22] Documentation: explain VM shared directories Alyssa Ross
2023-02-26 19:17 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 05/22] vm-lib/make-vm.nix: support " Alyssa Ross
2023-02-26 19:17 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 06/22] img/app: add support for testing virtiofs Alyssa Ross
2023-02-26 19:17 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 07/22] img/app: don't block app startup on network online Alyssa Ross
2023-02-26 19:17 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 08/22] img/app: auto-mount virtiofs0 filesystem Alyssa Ross
2023-02-26 19:17 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 09/22] vm/app/mg.nix: init Alyssa Ross
2023-02-26 19:17 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 10/22] vm/app/mg.nix: open virtio filesystem in dired Alyssa Ross
2023-02-26 19:17 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 11/22] host/rootfs: move ext mounting to s6-rc service Alyssa Ross
2022-11-14 1:14 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 12/22] host/rootfs: automatically grow user partition Alyssa Ross
2022-11-14 1:14 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 13/22] host/rootfs: use a bigger test ext partition Alyssa Ross
2022-11-14 1:14 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 14/22] host/initramfs/extfs.nix: tar2ext4 -> mkfs.ext4 -d Alyssa Ross
2022-11-14 1:14 ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 15/22] host/start-vm: resolve VM symlinks with /ext root Alyssa Ross
2022-10-10 23:28 ` [PATCH 16/22] host/rootfs: " Alyssa Ross
2022-10-10 23:28 ` [PATCH 17/22] Documentation: explain /ext symlink resolution Alyssa Ross
2022-10-10 23:28 ` [PATCH 18/22] host/start-vm: increase memory size to 512M Alyssa Ross
2022-10-10 23:28 ` Alyssa Ross [this message]
2022-10-10 23:29 ` [PATCH 20/22] vm-lib/make-vms.nix: add Alyssa Ross
2022-10-10 23:29 ` [PATCH 21/22] host/initramfs/extfs.nix: add example Nix-built VM Alyssa Ross
2022-10-10 23:29 ` [PATCH 22/22] Documentation: add how-to guide for Nix-built VMs Alyssa Ross
2022-10-10 23:29 ` [PATCH v2 0/6] Introduce a shared base for application VMs Alyssa Ross
2022-10-10 23:37 ` Alyssa Ross
2022-10-10 23:29 ` [PATCH v2 1/6] host/start-vm: support multiple block devices Alyssa Ross
2022-10-10 23:29 ` [PATCH v2 2/6] scripts/make-gpt.sh: add support for labels Alyssa Ross
2022-10-10 23:29 ` [PATCH v2 3/6] vm: build GPT images Alyssa Ross
2022-10-10 23:29 ` [PATCH v2 4/6] host/start-vm: boot using partition label Alyssa Ross
2022-10-10 23:29 ` [PATCH v2 5/6] release: rename from "img" Alyssa Ross
2022-10-10 23:29 ` [PATCH v2 6/6] img/app: extract from appvm-{lynx,catgirl} Alyssa Ross
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221010232909.1953738-20-hi@alyssa.is \
--to=hi@alyssa.is \
--cc=devel@spectrum-os.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://spectrum-os.org/git/crosvm
https://spectrum-os.org/git/doc
https://spectrum-os.org/git/mktuntap
https://spectrum-os.org/git/nixpkgs
https://spectrum-os.org/git/spectrum
https://spectrum-os.org/git/ucspi-vsock
https://spectrum-os.org/git/www
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).