1 # Note: This is a private API, internal to NixOS. Its interface is subject
2 # to change without notice.
4 # The result of this builder is two disk images:
6 # * `boot` - a small disk formatted with FAT to be used for /boot. FAT is
7 # chosen to support EFI.
8 # * `root` - a larger disk with a zpool taking the entire disk.
10 # This two-disk approach is taken to satisfy ZFS's requirements for
13 # # Why doesn't autoexpand work with ZFS in a partition?
15 # When ZFS owns the whole disk doesn’t really use a partition: it has
16 # a marker partition at the start and a marker partition at the end of
19 # If ZFS is constrained to a partition, ZFS leaves expanding the partition
20 # up to the user. Obviously, the user may not choose to do so.
22 # Once the user expands the partition, calling zpool online -e expands the
23 # vdev to use the whole partition. It doesn’t happen automatically
24 # presumably because zed doesn’t get an event saying it’s partition grew,
25 # whereas it can and does get an event saying the whole disk it is on is
29 , # The NixOS configuration to be installed onto the disk image.
32 , # size of the FAT boot disk, in megabytes.
35 , # The size of the root disk, in megabytes.
38 , # The name of the ZFS pool
42 rootPoolProperties ? {
45 , # pool-wide filesystem properties
46 rootPoolFilesystemProperties ? {
50 mountpoint = "legacy";
54 , # datasets, with per-attribute options:
55 # mount: (optional) mount point in the VM
56 # properties: (optional) ZFS properties on the dataset, like filesystemProperties
58 # 1. datasets will be created from shorter to longer names as a simple topo-sort
59 # 2. you should define a root's dataset's mount for `/`
62 , # The files and directories to be placed in the target file system.
63 # This is a list of attribute sets {source, target} where `source'
64 # is the file system object (regular file or directory) to be
65 # grafted in the file system at path `target'.
68 , # The initial NixOS configuration file to be copied to
69 # /etc/nixos/configuration.nix. This configuration will be embedded
70 # inside a configuration which includes the described ZFS fileSystems.
73 , # Shell code executed after the VM has finished.
76 , name ? "nixos-disk-image"
78 , # Disk image format, one of qcow2, qcow2-compressed, vdi, vpc, raw.
81 , # Include a copy of Nixpkgs in the disk image
85 formatOpt = if format == "qcow2-compressed" then "qcow2" else format;
87 compress = lib.optionalString (format == "qcow2-compressed") "-c";
89 filenameSuffix = "." + {
94 }.${formatOpt} or formatOpt;
95 bootFilename = "nixos.boot${filenameSuffix}";
96 rootFilename = "nixos.root${filenameSuffix}";
98 # FIXME: merge with channel.nix / make-channel.nix.
101 nixpkgs = lib.cleanSource pkgs.path;
103 pkgs.runCommand "nixos-${config.system.nixos.version}" {} ''
105 cp -prd ${nixpkgs.outPath} $out/nixos
106 chmod -R u+w $out/nixos
107 if [ ! -e $out/nixos/nixpkgs ]; then
108 ln -s . $out/nixos/nixpkgs
110 rm -rf $out/nixos/.git
111 echo -n ${config.system.nixos.versionSuffix} > $out/nixos/.version-suffix
114 closureInfo = pkgs.closureInfo {
115 rootPaths = [ config.system.build.toplevel ]
116 ++ (lib.optional includeChannel channelSources);
119 modulesTree = pkgs.aggregateModules
120 (with config.boot.kernelPackages; [ kernel zfs ]);
122 tools = lib.makeBinPath (
124 config.system.build.nixos-enter
125 config.system.build.nixos-install
136 hasDefinedMount = disk: ((disk.mount or null) != null);
138 stringifyProperties = prefix: properties: lib.concatStringsSep " \\\n" (
141 property: value: "${prefix} ${lib.escapeShellArg property}=${lib.escapeShellArg value}"
148 datasetlist = lib.mapAttrsToList lib.nameValuePair datasets;
149 sorted = lib.sort (left: right: (lib.stringLength left.name) < (lib.stringLength right.name)) datasetlist;
150 cmd = { name, value }:
152 properties = stringifyProperties "-o" (value.properties or {});
154 "zfs create -p ${properties} ${name}";
156 lib.concatMapStringsSep "\n" cmd sorted;
160 datasetlist = lib.mapAttrsToList lib.nameValuePair datasets;
161 mounts = lib.filter ({ value, ... }: hasDefinedMount value) datasetlist;
162 sorted = lib.sort (left: right: (lib.stringLength left.value.mount) < (lib.stringLength right.value.mount)) mounts;
163 cmd = { name, value }:
165 mkdir -p /mnt${lib.escapeShellArg value.mount}
166 mount -t zfs ${name} /mnt${lib.escapeShellArg value.mount}
169 lib.concatMapStringsSep "\n" cmd sorted;
173 datasetlist = lib.mapAttrsToList lib.nameValuePair datasets;
174 mounts = lib.filter ({ value, ... }: hasDefinedMount value) datasetlist;
175 sorted = lib.sort (left: right: (lib.stringLength left.value.mount) > (lib.stringLength right.value.mount)) mounts;
176 cmd = { name, value }:
178 umount /mnt${lib.escapeShellArg value.mount}
181 lib.concatMapStringsSep "\n" cmd sorted;
186 mountable = lib.filterAttrs (_: value: hasDefinedMount value) datasets;
188 pkgs.runCommand "filesystem-config.nix" {
189 buildInputs = with pkgs; [ jq nixpkgs-fmt ];
190 filesystems = builtins.toJSON {
191 fileSystems = lib.mapAttrs'
198 device = "${dataset}";
204 passAsFile = [ "filesystems" ];
207 echo "builtins.fromJSON '''"
208 jq . < "$filesystemsPath"
216 if configFile == null
217 then fileSystemsCfgFile
219 pkgs.runCommand "configuration.nix" {
220 buildInputs = with pkgs; [ nixpkgs-fmt ];
225 printf "(%s)\n" "$(cat ${fileSystemsCfgFile})";
226 printf "(%s)\n" "$(cat ${configFile})";
234 pkgs.vmTools.override {
236 [ "zfs" "9p" "9pnet_virtio" "virtio_pci" "virtio_blk" ] ++
237 (pkgs.lib.optional pkgs.stdenv.hostPlatform.isx86 "rtc_cmos");
238 kernel = modulesTree;
243 QEMU_OPTS = "-drive file=$bootDiskImage,if=virtio,cache=unsafe,werror=report"
244 + " -drive file=$rootDiskImage,if=virtio,cache=unsafe,werror=report";
246 PATH=$PATH:${pkgs.qemu_kvm}/bin
248 bootDiskImage=boot.raw
249 qemu-img create -f raw $bootDiskImage ${toString bootSize}M
251 rootDiskImage=root.raw
252 qemu-img create -f raw $rootDiskImage ${toString rootSize}M
256 ${if formatOpt == "raw" then ''
257 mv $bootDiskImage $out/${bootFilename}
258 mv $rootDiskImage $out/${rootFilename}
260 ${pkgs.qemu}/bin/qemu-img convert -f raw -O ${formatOpt} ${compress} $bootDiskImage $out/${bootFilename}
261 ${pkgs.qemu}/bin/qemu-img convert -f raw -O ${formatOpt} ${compress} $rootDiskImage $out/${rootFilename}
263 bootDiskImage=$out/${bootFilename}
264 rootDiskImage=$out/${rootFilename}
269 export PATH=${tools}:$PATH
272 cp -sv /dev/vda /dev/sda
273 cp -sv /dev/vda /dev/xvda
275 parted --script /dev/vda -- \
277 mkpart no-fs 1MiB 2MiB \
279 align-check optimal 1 \
280 mkpart ESP fat32 2MiB -1MiB \
281 align-check optimal 2 \
284 sfdisk --dump /dev/vda
288 ${stringifyProperties " -o" rootPoolProperties} \
289 ${stringifyProperties " -O" rootPoolFilesystemProperties} \
290 ${rootPoolName} /dev/vdb
291 parted --script /dev/vdb -- print
297 mkfs.vfat -n ESP /dev/vda2
298 mount /dev/vda2 /mnt/boot
302 # Install a configuration.nix
303 mkdir -p /mnt/etc/nixos
304 # `cat` so it is mutable on the fs
305 cat ${mergedConfig} > /mnt/etc/nixos/configuration.nix
307 export NIX_STATE_DIR=$TMPDIR/state
308 nix-store --load-db < ${closureInfo}/registration
313 --system ${config.system.build.toplevel} \
315 ${lib.optionalString includeChannel ''--channel ${channelSources}''}
322 zpool export ${rootPoolName}