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") {
52 squashfsTools = hostPkgs.squashfsTools.overrideAttrs (old: {
53 # We patch in support for squashfs labels because
54 # https://github.com/NixOS/nixpkgs/pull/236656 requires filesystems to have labels and
55 # https://github.com/plougher/squashfs-tools/issues/59 squashfs doesn't support labels
56 patches = (old.patches or []) ++ [ ./squashfs-tools-label.patch ];
57 buildInputs = (old.buildInputs or []) ++ [ hostPkgs.makeWrapper ];
58 postInstall = (old.postInstall or "") + ''
59 wrapProgram "$out/bin/mksquashfs" \
60 --append-flags "-label nix-store"
63 storeContents = config.virtualisation.additionalPaths;
69 virtualisation.qemu.isolation.nixStoreFilesystemType = mkOption {
71 What filesystem to use for the guest's Nix store.
73 erofs is more compact than ext4, but less mature.
75 squashfs is best, but requires patches to set the filesystem label.
77 type = lib.types.enum [ "ext4" "erofs" "squashfs" ];
83 boot.initrd.kernelModules =
84 optional (cfg.nixStoreFilesystemType == "erofs") "erofs";
86 nixpkgs.overlays = optional (cfg.nixStoreFilesystemType == "squashfs")
88 util-linux = prev.util-linux.overrideAttrs (old: {
89 # We patch in support for squashfs labels because
90 # https://github.com/NixOS/nixpkgs/pull/236656 requires filesystems to have labels and
91 # https://github.com/plougher/squashfs-tools/issues/59 squashfs doesn't support labels
92 patches = (old.patches or [ ]) ++ [ ./util-linux-squashfs-label.patch ];
96 fileSystems = mkVMOverride {
97 "${storeMountPath}" = {
98 fsType = cfg.nixStoreFilesystemType;
100 neededForBoot = true;
105 system.build.nixStoreImage =
106 nixStoreImages."${cfg.nixStoreFilesystemType}";
110 sharedDirectories = mkForce { };
113 file = config.system.build.nixStoreImage;
123 (mkIf (cfg.nixStoreFilesystemType == "ext4") {
124 # We use this to disable fsck runs on the ext4 nix store image because stage-1
125 # fsck crashes (maybe because the device is read-only?), halting boot.
126 boot.initrd.checkJournalingFS = false;