make-disk-image: Add an argument to only build a Nix store image

Add the `onlyNixStore` argument which enables building images
containing the contents of the Nix store at the root, instead of a
full system.

talyz 46f75211 ed2497b4

Changed files
+32 -17
nixos
+32 -17
nixos/lib/make-disk-image.nix
···
fsType ? "ext4"
, # Filesystem label
-
label ? "nixos"
, # The initial NixOS configuration file to be copied to
# /etc/nixos/configuration.nix.
···
, # Shell code executed after the VM has finished.
postVM ? ""
, name ? "nixos-disk-image"
···
(attrs: ((attrs.user or null) == null)
== ((attrs.group or null) == null))
contents;
with lib;
···
''}
echo "copying staging root to image..."
-
cptofs -p ${optionalString (partitionTableType != "none") "-P ${rootPartition}"} -t ${fsType} -i $diskImage $root/* / ||
(echo >&2 "ERROR: cptofs failed. diskSize might be too small for closure."; exit 1)
'';
-
in pkgs.vmTools.runInLinuxVM (
-
pkgs.runCommand name
-
{ preVM = prepareImage;
buildInputs = with pkgs; [ util-linux e2fsprogs dosfstools ];
-
postVM = ''
-
${if format == "raw" then ''
-
mv $diskImage $out/${filename}
-
'' else ''
-
${pkgs.qemu}/bin/qemu-img convert -f raw -O ${format} ${compress} $diskImage $out/${filename}
-
''}
-
diskImage=$out/${filename}
-
${postVM}
-
'';
memSize = 1024;
-
}
-
''
export PATH=${binPath}:$PATH
rootDisk=${if partitionTableType != "none" then "/dev/vda${rootPartition}" else "/dev/vda"}
···
tune2fs -T now -c 0 -i 0 $rootDisk
''}
''
-
)
···
fsType ? "ext4"
, # Filesystem label
+
label ? if onlyNixStore then "nix-store" else "nixos"
, # The initial NixOS configuration file to be copied to
# /etc/nixos/configuration.nix.
···
, # Shell code executed after the VM has finished.
postVM ? ""
+
+
, # Copy the contents of the Nix store to the root of the image and
+
# skip further setup. Incompatible with `contents`,
+
# `installBootLoader` and `configFile`.
+
onlyNixStore ? false
, name ? "nixos-disk-image"
···
(attrs: ((attrs.user or null) == null)
== ((attrs.group or null) == null))
contents;
+
assert onlyNixStore -> contents == [] && configFile == null && !installBootLoader;
with lib;
···
''}
echo "copying staging root to image..."
+
cptofs -p ${optionalString (partitionTableType != "none") "-P ${rootPartition}"} \
+
-t ${fsType} \
+
-i $diskImage \
+
$root${optionalString onlyNixStore builtins.storeDir}/* / ||
(echo >&2 "ERROR: cptofs failed. diskSize might be too small for closure."; exit 1)
'';
+
+
moveOrConvertImage = ''
+
${if format == "raw" then ''
+
mv $diskImage $out/${filename}
+
'' else ''
+
${pkgs.qemu}/bin/qemu-img convert -f raw -O ${format} ${compress} $diskImage $out/${filename}
+
''}
+
diskImage=$out/${filename}
+
'';
+
+
buildImage = pkgs.vmTools.runInLinuxVM (
+
pkgs.runCommand name {
+
preVM = prepareImage;
buildInputs = with pkgs; [ util-linux e2fsprogs dosfstools ];
+
postVM = moveOrConvertImage + postVM;
memSize = 1024;
+
} ''
export PATH=${binPath}:$PATH
rootDisk=${if partitionTableType != "none" then "/dev/vda${rootPartition}" else "/dev/vda"}
···
tune2fs -T now -c 0 -i 0 $rootDisk
''}
''
+
);
+
in
+
if onlyNixStore then
+
pkgs.runCommand name {}
+
(prepareImage + moveOrConvertImage + postVM)
+
else buildImage