1 { config, lib, modulesPath, pkgs, ... }:
4 escapeShellArg mkForce mkIf mkMerge mkOption mkVMOverride optional;
6 cfg = config.virtualisation.qemu.isolation;
8 storeMountPath = if config.virtualisation.writableStore then
13 hostPkgs = config.virtualisation.host.pkgs;
16 hostPkgs.closureInfo { rootPaths = config.virtualisation.additionalPaths; };
20 import (modulesPath + "/../lib/make-disk-image.nix") {
21 inherit pkgs config lib;
22 additionalPaths = [ storeContents ];
25 partitionTableType = "none";
26 installBootLoader = false;
28 additionalSpace = "0M";
33 hostPkgs.runCommand "nix-store-image" { } ''
35 ${hostPkgs.gnutar}/bin/tar --create \
37 --verbatim-files-from \
38 --transform 'flags=rSh;s|/nix/store/||' \
39 --files-from ${storeContents}/store-paths \
40 | ${hostPkgs.erofs-utils}/bin/mkfs.erofs \
44 -U eb176051-bd15-49b7-9e6b-462e0b467019 \
51 "${hostPkgs.callPackage (modulesPath + "/../lib/make-squashfs.nix") {
53 (hostPkgs.extend (import ../overlays/squashfs-labels)).squashfsTools.overrideAttrs
55 buildInputs = (old.buildInputs or [ ]) ++ [ hostPkgs.makeWrapper ];
56 postInstall = (old.postInstall or "") + ''
57 wrapProgram "$out/bin/mksquashfs" \
58 --append-flags "-label nix-store"
61 storeContents = config.virtualisation.additionalPaths;
67 virtualisation.qemu.isolation.nixStoreFilesystemType = mkOption {
69 What filesystem to use for the guest's Nix store.
71 squashfs is best, but requires patches to set the filesystem label.
73 ext4 images are ~4x larger than squashfs images.
75 erofs images are only ~2.5x larger than squashfs images, but erofs is less mature than either squashfs or ext4.
78 type = lib.types.enum [ "ext4" "erofs" "squashfs" ];
84 boot.initrd.kernelModules =
85 optional (cfg.nixStoreFilesystemType == "erofs") "erofs";
87 nixpkgs.overlays = optional (cfg.nixStoreFilesystemType == "squashfs")
88 (import ../overlays/squashfs-labels);
90 fileSystems = mkVMOverride {
91 "${storeMountPath}" = {
92 fsType = cfg.nixStoreFilesystemType;
99 system.build.nixStoreImage =
100 nixStoreImages."${cfg.nixStoreFilesystemType}";
104 sharedDirectories = mkForce { };
107 file = config.system.build.nixStoreImage;
117 (mkIf (cfg.nixStoreFilesystemType == "ext4") {
118 # We use this to disable fsck runs on the ext4 nix store image because stage-1
119 # fsck crashes (maybe because the device is read-only?), halting boot.
120 boot.initrd.checkJournalingFS = false;