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 a single disk image, partitioned like this:
6 # * partition #1: a very small, 1MiB partition to leave room for Grub.
8 # * partition #2: boot, a partition formatted with FAT to be used for /boot.
9 # FAT is chosen to support EFI.
11 # * partition #3: nixos, a partition dedicated to a zpool.
13 # This single-disk approach does not satisfy ZFS's requirements for autoexpand,
14 # however automation can expand it anyway. For example, with
15 # `services.zfs.expandOnBoot`.
18 , # The NixOS configuration to be installed onto the disk image.
21 , # size of the FAT partition, in megabytes.
24 , # The size of the root partition, in megabytes.
27 , # The name of the ZFS pool
31 rootPoolProperties ? {
34 , # pool-wide filesystem properties
35 rootPoolFilesystemProperties ? {
39 mountpoint = "legacy";
43 , # datasets, with per-attribute options:
44 # mount: (optional) mount point in the VM
45 # properties: (optional) ZFS properties on the dataset, like filesystemProperties
47 # 1. datasets will be created from shorter to longer names as a simple topo-sort
48 # 2. you should define a root's dataset's mount for `/`
51 , # The files and directories to be placed in the target file system.
52 # This is a list of attribute sets {source, target} where `source'
53 # is the file system object (regular file or directory) to be
54 # grafted in the file system at path `target'.
57 , # The initial NixOS configuration file to be copied to
58 # /etc/nixos/configuration.nix. This configuration will be embedded
59 # inside a configuration which includes the described ZFS fileSystems.
62 , # Shell code executed after the VM has finished.
65 , name ? "nixos-disk-image"
67 , # Disk image format, one of qcow2, qcow2-compressed, vdi, vpc, raw.
70 , # Include a copy of Nixpkgs in the disk image
74 formatOpt = if format == "qcow2-compressed" then "qcow2" else format;
76 compress = lib.optionalString (format == "qcow2-compressed") "-c";
78 filenameSuffix = "." + {
83 }.${formatOpt} or formatOpt;
84 rootFilename = "nixos.root${filenameSuffix}";
86 # FIXME: merge with channel.nix / make-channel.nix.
89 nixpkgs = lib.cleanSource pkgs.path;
91 pkgs.runCommand "nixos-${config.system.nixos.version}" { } ''
93 cp -prd ${nixpkgs.outPath} $out/nixos
94 chmod -R u+w $out/nixos
95 if [ ! -e $out/nixos/nixpkgs ]; then
96 ln -s . $out/nixos/nixpkgs
98 rm -rf $out/nixos/.git
99 echo -n ${config.system.nixos.versionSuffix} > $out/nixos/.version-suffix
102 closureInfo = pkgs.closureInfo {
103 rootPaths = [ config.system.build.toplevel ]
104 ++ (lib.optional includeChannel channelSources);
107 modulesTree = pkgs.aggregateModules
108 (with config.boot.kernelPackages; [ kernel zfs ]);
110 tools = lib.makeBinPath (
112 config.system.build.nixos-enter
113 config.system.build.nixos-install
124 hasDefinedMount = disk: ((disk.mount or null) != null);
126 stringifyProperties = prefix: properties: lib.concatStringsSep " \\\n" (
129 property: value: "${prefix} ${lib.escapeShellArg property}=${lib.escapeShellArg value}"
136 datasetlist = lib.mapAttrsToList lib.nameValuePair datasets;
137 sorted = lib.sort (left: right: (lib.stringLength left.name) < (lib.stringLength right.name)) datasetlist;
138 cmd = { name, value }:
140 properties = stringifyProperties "-o" (value.properties or { });
142 "zfs create -p ${properties} ${name}";
144 lib.concatMapStringsSep "\n" cmd sorted;
148 datasetlist = lib.mapAttrsToList lib.nameValuePair datasets;
149 mounts = lib.filter ({ value, ... }: hasDefinedMount value) datasetlist;
150 sorted = lib.sort (left: right: (lib.stringLength left.value.mount) < (lib.stringLength right.value.mount)) mounts;
151 cmd = { name, value }:
153 mkdir -p /mnt${lib.escapeShellArg value.mount}
154 mount -t zfs ${name} /mnt${lib.escapeShellArg value.mount}
157 lib.concatMapStringsSep "\n" cmd sorted;
161 datasetlist = lib.mapAttrsToList lib.nameValuePair datasets;
162 mounts = lib.filter ({ value, ... }: hasDefinedMount value) datasetlist;
163 sorted = lib.sort (left: right: (lib.stringLength left.value.mount) > (lib.stringLength right.value.mount)) mounts;
164 cmd = { name, value }:
166 umount /mnt${lib.escapeShellArg value.mount}
169 lib.concatMapStringsSep "\n" cmd sorted;
174 mountable = lib.filterAttrs (_: value: hasDefinedMount value) datasets;
176 pkgs.runCommand "filesystem-config.nix"
178 buildInputs = with pkgs; [ jq nixpkgs-fmt ];
179 filesystems = builtins.toJSON {
180 fileSystems = lib.mapAttrs'
187 device = "${dataset}";
193 passAsFile = [ "filesystems" ];
196 echo "builtins.fromJSON '''"
197 jq . < "$filesystemsPath"
205 if configFile == null
206 then fileSystemsCfgFile
208 pkgs.runCommand "configuration.nix"
210 buildInputs = with pkgs; [ nixpkgs-fmt ];
215 printf "(%s)\n" "$(cat ${fileSystemsCfgFile})";
216 printf "(%s)\n" "$(cat ${configFile})";
224 pkgs.vmTools.override {
226 [ "zfs" "9p" "9pnet_virtio" "virtio_pci" "virtio_blk" ] ++
227 (pkgs.lib.optional pkgs.stdenv.hostPlatform.isx86 "rtc_cmos");
228 kernel = modulesTree;
234 QEMU_OPTS = "-drive file=$rootDiskImage,if=virtio,cache=unsafe,werror=report";
236 PATH=$PATH:${pkgs.qemu_kvm}/bin
239 rootDiskImage=root.raw
240 qemu-img create -f raw $rootDiskImage ${toString (bootSize + rootSize)}M
244 ${if formatOpt == "raw" then ''
245 mv $rootDiskImage $out/${rootFilename}
247 ${pkgs.qemu_kvm}/bin/qemu-img convert -f raw -O ${formatOpt} ${compress} $rootDiskImage $out/${rootFilename}
249 rootDiskImage=$out/${rootFilename}
254 export PATH=${tools}:$PATH
257 cp -sv /dev/vda /dev/sda
258 cp -sv /dev/vda /dev/xvda
260 parted --script /dev/vda -- \
262 mkpart no-fs 1MiB 2MiB \
264 align-check optimal 1 \
265 mkpart primary fat32 2MiB ${toString bootSize}MiB \
266 align-check optimal 2 \
267 mkpart primary fat32 ${toString bootSize}MiB -1MiB \
268 align-check optimal 3 \
271 sfdisk --dump /dev/vda
275 ${stringifyProperties " -o" rootPoolProperties} \
276 ${stringifyProperties " -O" rootPoolFilesystemProperties} \
277 ${rootPoolName} /dev/vda3
278 parted --script /dev/vda -- print
284 mkfs.vfat -n ESP /dev/vda2
285 mount /dev/vda2 /mnt/boot
289 # Install a configuration.nix
290 mkdir -p /mnt/etc/nixos
291 # `cat` so it is mutable on the fs
292 cat ${mergedConfig} > /mnt/etc/nixos/configuration.nix
294 export NIX_STATE_DIR=$TMPDIR/state
295 nix-store --load-db < ${closureInfo}/registration
300 --system ${config.system.build.toplevel} \
302 ${lib.optionalString includeChannel ''--channel ${channelSources}''}
309 zpool export ${rootPoolName}