2015-09-27 20:01:43 +01:00
|
|
|
{ pkgs
|
|
|
|
, lib
|
|
|
|
|
|
|
|
, # The NixOS configuration to be installed onto the disk image.
|
|
|
|
config
|
|
|
|
|
|
|
|
, # The size of the disk, in megabytes.
|
|
|
|
diskSize
|
|
|
|
|
2017-02-22 23:47:24 +00:00
|
|
|
# The files and directories to be placed in the target file system.
|
|
|
|
# This is a list of attribute sets {source, target} where `source'
|
|
|
|
# is the file system object (regular file or directory) to be
|
|
|
|
# grafted in the file system at path `target'.
|
|
|
|
, contents ? []
|
|
|
|
|
2015-09-27 20:01:43 +01:00
|
|
|
, # Whether the disk should be partitioned (with a single partition
|
|
|
|
# containing the root filesystem) or contain the root filesystem
|
|
|
|
# directly.
|
|
|
|
partitioned ? true
|
|
|
|
|
2016-08-15 19:39:39 +01:00
|
|
|
# Whether to invoke switch-to-configuration boot during image creation
|
|
|
|
, installBootLoader ? true
|
|
|
|
|
2015-09-27 20:01:43 +01:00
|
|
|
, # The root file system type.
|
|
|
|
fsType ? "ext4"
|
|
|
|
|
|
|
|
, # The initial NixOS configuration file to be copied to
|
|
|
|
# /etc/nixos/configuration.nix.
|
|
|
|
configFile ? null
|
|
|
|
|
2015-09-28 13:31:19 +01:00
|
|
|
, # Shell code executed after the VM has finished.
|
|
|
|
postVM ? ""
|
|
|
|
|
2016-03-15 12:34:41 +00:00
|
|
|
, name ? "nixos-disk-image"
|
2016-03-30 20:48:12 +01:00
|
|
|
|
2016-12-07 12:29:07 +00:00
|
|
|
# This prevents errors while checking nix-store validity, see
|
|
|
|
# https://github.com/NixOS/nix/issues/1134
|
|
|
|
, fixValidity ? true
|
|
|
|
|
2016-03-30 20:48:12 +01:00
|
|
|
, format ? "raw"
|
2015-09-27 20:01:43 +01:00
|
|
|
}:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
pkgs.vmTools.runInLinuxVM (
|
2016-03-15 12:34:41 +00:00
|
|
|
pkgs.runCommand name
|
2015-09-27 20:01:43 +01:00
|
|
|
{ preVM =
|
|
|
|
''
|
|
|
|
mkdir $out
|
2016-03-30 20:48:12 +01:00
|
|
|
diskImage=$out/nixos.${if format == "qcow2" then "qcow2" else "img"}
|
|
|
|
${pkgs.vmTools.qemu}/bin/qemu-img create -f ${format} $diskImage "${toString diskSize}M"
|
2015-09-27 20:01:43 +01:00
|
|
|
mv closure xchg/
|
|
|
|
'';
|
2017-02-22 23:47:24 +00:00
|
|
|
buildInputs = with pkgs; [ utillinux perl e2fsprogs parted rsync ];
|
|
|
|
|
|
|
|
# I'm preserving the line below because I'm going to search for it across nixpkgs to consolidate
|
|
|
|
# image building logic. The comment right below this now appears in 4 different places in nixpkgs :)
|
|
|
|
# !!! should use XML.
|
|
|
|
sources = map (x: x.source) contents;
|
|
|
|
targets = map (x: x.target) contents;
|
|
|
|
|
2015-09-27 20:01:43 +01:00
|
|
|
exportReferencesGraph =
|
|
|
|
[ "closure" config.system.build.toplevel ];
|
2015-09-28 13:31:19 +01:00
|
|
|
inherit postVM;
|
2015-10-06 14:03:26 +01:00
|
|
|
memSize = 1024;
|
2015-09-27 20:01:43 +01:00
|
|
|
}
|
|
|
|
''
|
|
|
|
${if partitioned then ''
|
|
|
|
# Create a single / partition.
|
|
|
|
parted /dev/vda mklabel msdos
|
|
|
|
parted /dev/vda -- mkpart primary ext2 1M -1s
|
|
|
|
. /sys/class/block/vda1/uevent
|
|
|
|
mknod /dev/vda1 b $MAJOR $MINOR
|
|
|
|
rootDisk=/dev/vda1
|
|
|
|
'' else ''
|
|
|
|
rootDisk=/dev/vda
|
|
|
|
''}
|
|
|
|
|
|
|
|
# Create an empty filesystem and mount it.
|
|
|
|
mkfs.${fsType} -L nixos $rootDisk
|
|
|
|
mkdir /mnt
|
|
|
|
mount $rootDisk /mnt
|
|
|
|
|
|
|
|
# Register the paths in the Nix database.
|
|
|
|
printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \
|
2016-08-16 04:02:20 +01:00
|
|
|
${config.nix.package.out}/bin/nix-store --load-db --option build-users-group ""
|
2015-09-27 20:01:43 +01:00
|
|
|
|
2016-12-07 12:29:07 +00:00
|
|
|
${if fixValidity then ''
|
|
|
|
# Add missing size/hash fields to the database. FIXME:
|
|
|
|
# exportReferencesGraph should provide these directly.
|
|
|
|
${config.nix.package.out}/bin/nix-store --verify --check-contents --option build-users-group ""
|
|
|
|
'' else ""}
|
2015-09-27 20:01:43 +01:00
|
|
|
|
2016-08-16 04:02:20 +01:00
|
|
|
# In case the bootloader tries to write to /dev/sda…
|
|
|
|
ln -s vda /dev/xvda
|
|
|
|
ln -s vda /dev/sda
|
2015-09-27 20:01:43 +01:00
|
|
|
|
2016-08-16 04:02:20 +01:00
|
|
|
# Install the closure onto the image
|
|
|
|
USER=root ${config.system.build.nixos-install}/bin/nixos-install \
|
|
|
|
--closure ${config.system.build.toplevel} \
|
|
|
|
--no-channel-copy \
|
|
|
|
--no-root-passwd \
|
|
|
|
${optionalString (!installBootLoader) "--no-bootloader"}
|
2015-09-27 20:01:43 +01:00
|
|
|
|
|
|
|
# Install a configuration.nix.
|
|
|
|
mkdir -p /mnt/etc/nixos
|
|
|
|
${optionalString (configFile != null) ''
|
|
|
|
cp ${configFile} /mnt/etc/nixos/configuration.nix
|
|
|
|
''}
|
|
|
|
|
2016-08-16 04:02:20 +01:00
|
|
|
# Remove /etc/machine-id so that each machine cloning this image will get its own id
|
|
|
|
rm -f /mnt/etc/machine-id
|
2015-09-27 20:01:43 +01:00
|
|
|
|
2017-02-22 23:47:24 +00:00
|
|
|
# Copy arbitrary other files into the image
|
|
|
|
# Semi-shamelessly copied from make-etc.sh. I (@copumpkin) shall factor this stuff out as part of
|
|
|
|
# https://github.com/NixOS/nixpkgs/issues/23052.
|
|
|
|
set -f
|
|
|
|
sources_=($sources)
|
|
|
|
targets_=($targets)
|
|
|
|
set +f
|
|
|
|
|
|
|
|
for ((i = 0; i < ''${#targets_[@]}; i++)); do
|
|
|
|
source="''${sources_[$i]}"
|
|
|
|
target="''${targets_[$i]}"
|
|
|
|
|
|
|
|
if [[ "$source" =~ '*' ]]; then
|
|
|
|
|
|
|
|
# If the source name contains '*', perform globbing.
|
|
|
|
mkdir -p /mnt/$target
|
|
|
|
for fn in $source; do
|
|
|
|
rsync -a --no-o --no-g "$fn" /mnt/$target/
|
|
|
|
done
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
mkdir -p /mnt/$(dirname $target)
|
|
|
|
if ! [ -e /mnt/$target ]; then
|
|
|
|
rsync -a --no-o --no-g $source /mnt/$target
|
|
|
|
else
|
|
|
|
echo "duplicate entry $target -> $source"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2015-09-27 20:01:43 +01:00
|
|
|
umount /mnt
|
|
|
|
|
2017-02-26 02:02:22 +00:00
|
|
|
# Make sure resize2fs works. Note that resize2fs has stricter criteria for resizing than a normal
|
|
|
|
# mount, so the `-c 0` and `-i 0` don't affect it. Setting it to `now` doesn't produce deterministic
|
|
|
|
# output, of course, but we can fix that when/if we start making images deterministic.
|
2016-12-06 15:34:18 +00:00
|
|
|
${optionalString (fsType == "ext4") ''
|
2017-02-26 02:02:22 +00:00
|
|
|
tune2fs -T now -c 0 -i 0 $rootDisk
|
2016-12-06 15:34:18 +00:00
|
|
|
''}
|
2015-09-27 20:01:43 +01:00
|
|
|
''
|
|
|
|
)
|