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
|
|
|
|
2017-11-12 18:29:01 +00:00
|
|
|
, # Disk image format, one of qcow2, qcow2-compressed, vpc, raw.
|
2017-11-11 19:26:50 +00:00
|
|
|
format ? "raw"
|
2015-09-27 20:01:43 +01:00
|
|
|
}:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
2017-11-12 18:29:01 +00:00
|
|
|
let format' = format; in let
|
|
|
|
|
2017-11-13 14:56:30 +00:00
|
|
|
format = if (format' == "qcow2-compressed") then "qcow2" else format';
|
2017-11-12 18:29:01 +00:00
|
|
|
|
|
|
|
compress = optionalString (format' == "qcow2-compressed") "-c";
|
2017-11-11 19:27:37 +00:00
|
|
|
|
2017-11-12 18:21:33 +00:00
|
|
|
filename = "nixos." + {
|
2017-08-10 23:57:26 +01:00
|
|
|
qcow2 = "qcow2";
|
|
|
|
vpc = "vhd";
|
|
|
|
raw = "img";
|
2017-11-12 18:21:33 +00:00
|
|
|
}.${format};
|
2017-08-10 23:57:26 +01:00
|
|
|
|
2017-11-11 19:26:50 +00:00
|
|
|
nixpkgs = cleanSource pkgs.path;
|
2017-04-17 04:08:37 +01:00
|
|
|
|
|
|
|
channelSources = pkgs.runCommand "nixos-${config.system.nixosVersion}" {} ''
|
|
|
|
mkdir -p $out
|
|
|
|
cp -prd ${nixpkgs} $out/nixos
|
|
|
|
chmod -R u+w $out/nixos
|
|
|
|
if [ ! -e $out/nixos/nixpkgs ]; then
|
|
|
|
ln -s . $out/nixos/nixpkgs
|
|
|
|
fi
|
|
|
|
rm -rf $out/nixos/.git
|
|
|
|
echo -n ${config.system.nixosVersionSuffix} > $out/nixos/.version-suffix
|
|
|
|
'';
|
|
|
|
|
|
|
|
metaClosure = pkgs.writeText "meta" ''
|
|
|
|
${config.system.build.toplevel}
|
|
|
|
${config.nix.package.out}
|
|
|
|
${channelSources}
|
|
|
|
'';
|
|
|
|
|
2017-11-11 20:50:00 +00:00
|
|
|
prepareImageInputs = with pkgs; [ rsync utillinux parted e2fsprogs lkl fakeroot config.system.build.nixos-prepare-root ] ++ stdenv.initialPath;
|
2017-04-17 04:08:37 +01:00
|
|
|
|
|
|
|
# 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;
|
|
|
|
|
|
|
|
prepareImage = ''
|
2017-11-11 19:26:50 +00:00
|
|
|
export PATH=${makeSearchPathOutput "bin" "bin" prepareImageInputs}
|
2017-04-17 04:08:37 +01:00
|
|
|
|
|
|
|
mkdir $out
|
|
|
|
diskImage=nixos.raw
|
|
|
|
truncate -s ${toString diskSize}M $diskImage
|
|
|
|
|
|
|
|
${if partitioned then ''
|
2017-10-08 14:35:47 +01:00
|
|
|
parted --script $diskImage -- mklabel msdos mkpart primary ext4 1M -1s
|
2017-04-17 04:08:37 +01:00
|
|
|
offset=$((2048*512))
|
|
|
|
'' else ''
|
|
|
|
offset=0
|
|
|
|
''}
|
|
|
|
|
2017-11-11 20:50:00 +00:00
|
|
|
mkfs.${fsType} -F -L nixos -E offset=$offset $diskImage
|
2017-11-11 19:26:50 +00:00
|
|
|
|
2017-04-17 04:08:37 +01:00
|
|
|
root="$PWD/root"
|
|
|
|
mkdir -p $root
|
|
|
|
|
|
|
|
# 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_=(${concatStringsSep " " sources})
|
|
|
|
targets_=(${concatStringsSep " " 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 $root/$target
|
|
|
|
for fn in $source; do
|
|
|
|
rsync -a --no-o --no-g "$fn" $root/$target/
|
|
|
|
done
|
|
|
|
else
|
|
|
|
mkdir -p $root/$(dirname $target)
|
|
|
|
if ! [ -e $root/$target ]; then
|
|
|
|
rsync -a --no-o --no-g $source $root/$target
|
|
|
|
else
|
|
|
|
echo "duplicate entry $target -> $source"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# TODO: Nix really likes to chown things it creates to its current user...
|
|
|
|
fakeroot nixos-prepare-root $root ${channelSources} ${config.system.build.toplevel} closure
|
|
|
|
|
2017-12-16 18:21:26 +00:00
|
|
|
# fakeroot seems to always give the owner write permissions, which we do not want
|
2017-12-16 22:26:51 +00:00
|
|
|
find $root/nix/store -mindepth 1 -maxdepth 1 -type f -o -type d | xargs chmod -R a-w
|
2017-12-16 18:21:26 +00:00
|
|
|
|
2017-04-17 04:08:37 +01:00
|
|
|
echo "copying staging root to image..."
|
2017-11-12 18:21:33 +00:00
|
|
|
cptofs ${optionalString partitioned "-P 1"} -t ${fsType} -i $diskImage $root/* /
|
2017-04-17 04:08:37 +01:00
|
|
|
'';
|
|
|
|
in pkgs.vmTools.runInLinuxVM (
|
2016-03-15 12:34:41 +00:00
|
|
|
pkgs.runCommand name
|
2017-04-17 04:08:37 +01:00
|
|
|
{ preVM = prepareImage;
|
|
|
|
buildInputs = with pkgs; [ utillinux e2fsprogs ];
|
|
|
|
exportReferencesGraph = [ "closure" metaClosure ];
|
|
|
|
postVM = ''
|
|
|
|
${if format == "raw" then ''
|
2017-11-12 18:21:33 +00:00
|
|
|
mv $diskImage $out/${filename}
|
2017-04-17 04:08:37 +01:00
|
|
|
'' else ''
|
2017-11-11 19:27:37 +00:00
|
|
|
${pkgs.qemu}/bin/qemu-img convert -f raw -O ${format} ${compress} $diskImage $out/${filename}
|
2017-04-17 04:08:37 +01:00
|
|
|
''}
|
2017-11-12 18:21:33 +00:00
|
|
|
diskImage=$out/${filename}
|
2017-04-17 04:08:37 +01:00
|
|
|
${postVM}
|
|
|
|
'';
|
2015-10-06 14:03:26 +01:00
|
|
|
memSize = 1024;
|
2015-09-27 20:01:43 +01:00
|
|
|
}
|
|
|
|
''
|
|
|
|
${if partitioned then ''
|
|
|
|
rootDisk=/dev/vda1
|
|
|
|
'' else ''
|
|
|
|
rootDisk=/dev/vda
|
|
|
|
''}
|
|
|
|
|
2017-04-17 04:08:37 +01:00
|
|
|
# Some tools assume these exist
|
2016-08-16 04:02:20 +01:00
|
|
|
ln -s vda /dev/xvda
|
|
|
|
ln -s vda /dev/sda
|
2015-09-27 20:01:43 +01:00
|
|
|
|
2017-04-17 04:08:37 +01:00
|
|
|
mountPoint=/mnt
|
|
|
|
mkdir $mountPoint
|
|
|
|
mount $rootDisk $mountPoint
|
2015-09-27 20:01:43 +01:00
|
|
|
|
2017-04-17 04:08:37 +01:00
|
|
|
# Install a configuration.nix
|
2015-09-27 20:01:43 +01:00
|
|
|
mkdir -p /mnt/etc/nixos
|
|
|
|
${optionalString (configFile != null) ''
|
|
|
|
cp ${configFile} /mnt/etc/nixos/configuration.nix
|
|
|
|
''}
|
|
|
|
|
2017-04-17 04:08:37 +01:00
|
|
|
mount --rbind /dev $mountPoint/dev
|
|
|
|
mount --rbind /proc $mountPoint/proc
|
|
|
|
mount --rbind /sys $mountPoint/sys
|
2017-02-22 23:47:24 +00:00
|
|
|
|
2017-04-17 04:08:37 +01:00
|
|
|
# Set up core system link, GRUB, etc.
|
|
|
|
NIXOS_INSTALL_BOOTLOADER=1 chroot $mountPoint /nix/var/nix/profiles/system/bin/switch-to-configuration boot
|
2017-02-22 23:47:24 +00:00
|
|
|
|
2017-04-17 04:08:37 +01:00
|
|
|
# TODO: figure out if I should activate, but for now I won't
|
|
|
|
# chroot $mountPoint /nix/var/nix/profiles/system/activate
|
2017-02-22 23:47:24 +00:00
|
|
|
|
2017-04-17 04:08:37 +01:00
|
|
|
# The above scripts will generate a random machine-id and we don't want to bake a single ID into all our images
|
|
|
|
rm -f $mountPoint/etc/machine-id
|
2017-02-22 23:47:24 +00:00
|
|
|
|
2017-04-17 04:08:37 +01:00
|
|
|
umount -R /mnt
|
2015-09-27 20:01:43 +01:00
|
|
|
|
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
|
|
|
''
|
|
|
|
)
|