]> git.scottworley.com Git - nixos-qemu-vm-isolation/blame - modules/qemu-vm-isolation.nix
Restore squashfs support with a dubious kludge
[nixos-qemu-vm-isolation] / modules / qemu-vm-isolation.nix
CommitLineData
69619e0b
SW
1{ config, lib, modulesPath, pkgs, ... }:
2let
f78c24af 3 inherit (lib)
e4f516e1 4 escapeShellArg mkForce mkIf mkMerge mkOption mkVMOverride optional;
f78c24af
SW
5
6 cfg = config.virtualisation.qemu.isolation;
69619e0b 7
69619e0b
SW
8 storeMountPath = if config.virtualisation.writableStore then
9 "/nix/.ro-store"
10 else
11 "/nix/store";
12
f78c24af 13 hostPkgs = config.virtualisation.host.pkgs;
69619e0b 14
f78c24af
SW
15 storeContents =
16 hostPkgs.closureInfo { rootPaths = config.virtualisation.additionalPaths; };
68bdafb0 17
f78c24af 18 nixStoreImages = {
a8cf2d3d
SW
19 ext4 = "${
20 import (modulesPath + "/../lib/make-disk-image.nix") {
21 inherit pkgs config lib;
22 additionalPaths = [ storeContents ];
23 onlyNixStore = true;
24 label = "nix-store";
25 partitionTableType = "none";
26 installBootLoader = false;
27 diskSize = "auto";
28 additionalSpace = "0M";
29 copyChannel = false;
30 }
31 }/nixos.img";
32 erofs = "${
33 hostPkgs.runCommand "nix-store-image" { } ''
34 mkdir $out
35 cd ${builtins.storeDir}
36 ${hostPkgs.erofs-utils}/bin/mkfs.erofs \
37 --force-uid=0 \
38 --force-gid=0 \
39 -L nix-store \
40 -U eb176051-bd15-49b7-9e6b-462e0b467019 \
41 -T 0 \
42 --exclude-regex="$(
43 <${storeContents}/store-paths \
44 sed -e 's^.*/^^g' \
45 | cut -c -10 \
46 | ${hostPkgs.python3}/bin/python -c ${
47 escapeShellArg (builtins.readFile
48 (modulesPath + "/virtualisation/includes-to-excludes.py"))
49 } )" \
50 $out/nix-store.img \
51 .
52 ''
53 }/nix-store.img";
54 squashfs =
55 "${hostPkgs.callPackage (modulesPath + "/../lib/make-squashfs.nix") {
56 storeContents = config.virtualisation.additionalPaths;
57 }}";
f78c24af 58 };
69619e0b 59
f78c24af
SW
60in {
61 options = {
62 virtualisation.qemu.isolation.nixStoreFilesystemType = mkOption {
63 description = ''
64 What filesystem to use for the guest's Nix store.
69619e0b 65
f78c24af 66 erofs is more compact than ext4, but less mature.
a8cf2d3d
SW
67
68 squashfs support currently requires a dubious kludge that results in these
69 VMs not being able to mount any other squashfs volumes besides the nix store.
f78c24af 70 '';
a8cf2d3d 71 type = lib.types.enum [ "ext4" "erofs" "squashfs" ];
f78c24af
SW
72 default = "ext4";
73 };
74 };
75 config = mkMerge [
76 {
77 boot.initrd.kernelModules =
78 optional (cfg.nixStoreFilesystemType == "erofs") "erofs";
69619e0b 79
a8cf2d3d
SW
80 nixpkgs.overlays = optional (cfg.nixStoreFilesystemType == "squashfs")
81 (final: prev: {
82 util-linux = prev.util-linux.overrideAttrs (old: {
83 patches = (old.patches or [ ])
84 ++ [ ./libblkid-squashfs-nix-store-kludge.patch ];
85 });
86 });
87
f78c24af
SW
88 fileSystems = mkVMOverride {
89 "${storeMountPath}" = {
f78c24af
SW
90 fsType = cfg.nixStoreFilesystemType;
91 options = [ "ro" ];
92 neededForBoot = true;
e4f516e1 93 label = "nix-store";
f78c24af 94 };
26efd1b6 95 };
69619e0b 96
f78c24af
SW
97 system.build.nixStoreImage =
98 nixStoreImages."${cfg.nixStoreFilesystemType}";
99
100 virtualisation = {
101
102 sharedDirectories = mkForce { };
103
104 qemu.drives = [{
a8cf2d3d 105 file = config.system.build.nixStoreImage;
f78c24af
SW
106 driveExtraOpts = {
107 format = "raw";
108 read-only = "on";
109 werror = "report";
110 };
111 }];
112
113 };
114 }
115 (mkIf (cfg.nixStoreFilesystemType == "ext4") {
116 # We use this to disable fsck runs on the ext4 nix store image because stage-1
117 # fsck crashes (maybe because the device is read-only?), halting boot.
118 boot.initrd.checkJournalingFS = false;
119 })
120 ];
26efd1b6 121}