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 , # memory allocated for virtualized build instance
27 , # The size of the root partition, in megabytes.
30 , # The name of the ZFS pool
34 rootPoolProperties ? {
37 , # pool-wide filesystem properties
38 rootPoolFilesystemProperties ? {
42 mountpoint = "legacy";
46 , # datasets, with per-attribute options:
47 # mount: (optional) mount point in the VM
48 # properties: (optional) ZFS properties on the dataset, like filesystemProperties
50 # 1. datasets will be created from shorter to longer names as a simple topo-sort
51 # 2. you should define a root's dataset's mount for `/`
54 , # The files and directories to be placed in the target file system.
55 # This is a list of attribute sets {source, target} where `source'
56 # is the file system object (regular file or directory) to be
57 # grafted in the file system at path `target'.
60 , # The initial NixOS configuration file to be copied to
61 # /etc/nixos/configuration.nix. This configuration will be embedded
62 # inside a configuration which includes the described ZFS fileSystems.
65 , # Shell code executed after the VM has finished.
68 , name ? "nixos-disk-image"
70 , # Disk image format, one of qcow2, qcow2-compressed, vdi, vpc, raw.
73 , # Include a copy of Nixpkgs in the disk image
77 formatOpt = if format == "qcow2-compressed" then "qcow2" else format;
79 compress = lib.optionalString (format == "qcow2-compressed") "-c";
81 filenameSuffix = "." + {
86 }.${formatOpt} or formatOpt;
87 rootFilename = "nixos.root${filenameSuffix}";
89 # FIXME: merge with channel.nix / make-channel.nix.
92 nixpkgs = lib.cleanSource pkgs.path;
94 pkgs.runCommand "nixos-${config.system.nixos.version}" { } ''
96 cp -prd ${nixpkgs.outPath} $out/nixos
97 chmod -R u+w $out/nixos
98 if [ ! -e $out/nixos/nixpkgs ]; then
99 ln -s . $out/nixos/nixpkgs
101 rm -rf $out/nixos/.git
102 echo -n ${config.system.nixos.versionSuffix} > $out/nixos/.version-suffix
105 closureInfo = pkgs.closureInfo {
106 rootPaths = [ config.system.build.toplevel ]
107 ++ (lib.optional includeChannel channelSources);
110 modulesTree = pkgs.aggregateModules
111 (with config.boot.kernelPackages; [ kernel zfs ]);
113 tools = lib.makeBinPath (
116 config.system.build.nixos-install
127 hasDefinedMount = disk: ((disk.mount or null) != null);
129 stringifyProperties = prefix: properties: lib.concatStringsSep " \\\n" (
132 property: value: "${prefix} ${lib.escapeShellArg property}=${lib.escapeShellArg value}"
139 datasetlist = lib.mapAttrsToList lib.nameValuePair datasets;
140 sorted = lib.sort (left: right: (lib.stringLength left.name) < (lib.stringLength right.name)) datasetlist;
141 cmd = { name, value }:
143 properties = stringifyProperties "-o" (value.properties or { });
145 "zfs create -p ${properties} ${name}";
147 lib.concatMapStringsSep "\n" cmd sorted;
151 datasetlist = lib.mapAttrsToList lib.nameValuePair datasets;
152 mounts = lib.filter ({ value, ... }: hasDefinedMount value) datasetlist;
153 sorted = lib.sort (left: right: (lib.stringLength left.value.mount) < (lib.stringLength right.value.mount)) mounts;
154 cmd = { name, value }:
156 mkdir -p /mnt${lib.escapeShellArg value.mount}
157 mount -t zfs ${name} /mnt${lib.escapeShellArg value.mount}
160 lib.concatMapStringsSep "\n" cmd sorted;
164 datasetlist = lib.mapAttrsToList lib.nameValuePair datasets;
165 mounts = lib.filter ({ value, ... }: hasDefinedMount value) datasetlist;
166 sorted = lib.sort (left: right: (lib.stringLength left.value.mount) > (lib.stringLength right.value.mount)) mounts;
167 cmd = { name, value }:
169 umount /mnt${lib.escapeShellArg value.mount}
172 lib.concatMapStringsSep "\n" cmd sorted;
177 mountable = lib.filterAttrs (_: value: hasDefinedMount value) datasets;
179 pkgs.runCommand "filesystem-config.nix"
181 buildInputs = with pkgs; [ jq nixpkgs-fmt ];
182 filesystems = builtins.toJSON {
183 fileSystems = lib.mapAttrs'
190 device = "${dataset}";
196 passAsFile = [ "filesystems" ];
199 echo "builtins.fromJSON '''"
200 jq . < "$filesystemsPath"
208 if configFile == null
209 then fileSystemsCfgFile
211 pkgs.runCommand "configuration.nix"
213 buildInputs = with pkgs; [ nixpkgs-fmt ];
218 printf "(%s)\n" "$(cat ${fileSystemsCfgFile})";
219 printf "(%s)\n" "$(cat ${configFile})";
227 pkgs.vmTools.override {
229 [ "zfs" "9p" "9pnet_virtio" "virtio_pci" "virtio_blk" ] ++
230 (pkgs.lib.optional pkgs.stdenv.hostPlatform.isx86 "rtc_cmos");
231 kernel = modulesTree;
237 QEMU_OPTS = "-drive file=$rootDiskImage,if=virtio,cache=unsafe,werror=report";
239 PATH=$PATH:${pkgs.qemu_kvm}/bin
242 rootDiskImage=root.raw
243 qemu-img create -f raw $rootDiskImage ${toString (bootSize + rootSize)}M
247 ${if formatOpt == "raw" then ''
248 mv $rootDiskImage $out/${rootFilename}
250 ${pkgs.qemu_kvm}/bin/qemu-img convert -f raw -O ${formatOpt} ${compress} $rootDiskImage $out/${rootFilename}
252 rootDiskImage=$out/${rootFilename}
257 export PATH=${tools}:$PATH
260 cp -sv /dev/vda /dev/sda
261 cp -sv /dev/vda /dev/xvda
263 parted --script /dev/vda -- \
265 mkpart no-fs 1MiB 2MiB \
267 align-check optimal 1 \
268 mkpart primary fat32 2MiB ${toString bootSize}MiB \
269 align-check optimal 2 \
270 mkpart primary fat32 ${toString bootSize}MiB -1MiB \
271 align-check optimal 3 \
274 sfdisk --dump /dev/vda
278 ${stringifyProperties " -o" rootPoolProperties} \
279 ${stringifyProperties " -O" rootPoolFilesystemProperties} \
280 ${rootPoolName} /dev/vda3
281 parted --script /dev/vda -- print
287 mkfs.vfat -n ESP /dev/vda2
288 mount /dev/vda2 /mnt/boot
292 # Install a configuration.nix
293 mkdir -p /mnt/etc/nixos
294 # `cat` so it is mutable on the fs
295 cat ${mergedConfig} > /mnt/etc/nixos/configuration.nix
297 export NIX_STATE_DIR=$TMPDIR/state
298 nix-store --load-db < ${closureInfo}/registration
303 --system ${config.system.build.toplevel} \
305 ${lib.optionalString includeChannel ''--channel ${channelSources}''}
312 zpool export ${rootPoolName}