2015-09-27 21:01:43 +02:00
|
|
|
|
{ pkgs
|
|
|
|
|
, lib
|
|
|
|
|
|
|
|
|
|
, # The NixOS configuration to be installed onto the disk image.
|
|
|
|
|
config
|
|
|
|
|
|
|
|
|
|
, # The size of the disk, in megabytes.
|
2020-06-01 23:43:37 +02:00
|
|
|
|
# if "auto" size is calculated based on the contents copied to it and
|
|
|
|
|
# additionalSpace is taken into account.
|
|
|
|
|
diskSize ? "auto"
|
2015-09-27 21:01:43 +02:00
|
|
|
|
|
2020-06-01 23:43:37 +02:00
|
|
|
|
, # additional disk space to be added to the image if diskSize "auto"
|
|
|
|
|
# is used
|
|
|
|
|
additionalSpace ? "512M"
|
|
|
|
|
|
|
|
|
|
, # size of the boot partition, is only used if partitionTableType is
|
|
|
|
|
# either "efi" or "hybrid"
|
2021-04-24 05:20:49 +02:00
|
|
|
|
# This will be undersized slightly, as this is actually the offset of
|
|
|
|
|
# the end of the partition. Generally it will be 1MiB smaller.
|
2020-06-01 23:43:37 +02:00
|
|
|
|
bootSize ? "256M"
|
|
|
|
|
|
|
|
|
|
, # The files and directories to be placed in the target file system.
|
2020-11-19 17:18:02 +01:00
|
|
|
|
# This is a list of attribute sets {source, target, mode, user, group} where
|
|
|
|
|
# `source' is the file system object (regular file or directory) to be
|
|
|
|
|
# grafted in the file system at path `target', `mode' is a string containing
|
|
|
|
|
# the permissions that will be set (ex. "755"), `user' and `group' are the
|
|
|
|
|
# user and group name that will be set as owner of the files.
|
|
|
|
|
# `mode', `user', and `group' are optional.
|
|
|
|
|
# When setting one of `user' or `group', the other needs to be set too.
|
2020-06-01 23:43:37 +02:00
|
|
|
|
contents ? []
|
2017-02-23 00:47:24 +01:00
|
|
|
|
|
2017-12-07 01:00:21 +01:00
|
|
|
|
, # Type of partition table to use; either "legacy", "efi", or "none".
|
|
|
|
|
# For "efi" images, the GPT partition table is used and a mandatory ESP
|
|
|
|
|
# partition of reasonable size is created in addition to the root partition.
|
|
|
|
|
# For "legacy", the msdos partition table is used and a single large root
|
2020-06-01 23:43:37 +02:00
|
|
|
|
# partition is created.
|
2020-10-28 20:53:10 +01:00
|
|
|
|
# For "legacy+gpt", the GPT partition table is used, a 1MiB no-fs partition for
|
|
|
|
|
# use by the bootloader is created, and a single large root partition is
|
|
|
|
|
# created.
|
2020-06-01 23:43:37 +02:00
|
|
|
|
# For "hybrid", the GPT partition table is used and a mandatory ESP
|
|
|
|
|
# partition of reasonable size is created in addition to the root partition.
|
|
|
|
|
# Also a legacy MBR will be present.
|
2017-12-07 01:00:21 +01:00
|
|
|
|
# For "none", no partition table is created. Enabling `installBootLoader`
|
|
|
|
|
# most likely fails as GRUB will probably refuse to install.
|
|
|
|
|
partitionTableType ? "legacy"
|
2015-09-27 21:01:43 +02:00
|
|
|
|
|
2021-10-05 15:50:04 +02:00
|
|
|
|
, # Whether to invoke `switch-to-configuration boot` during image creation
|
|
|
|
|
installBootLoader ? true
|
|
|
|
|
|
2015-09-27 21:01:43 +02:00
|
|
|
|
, # The root file system type.
|
|
|
|
|
fsType ? "ext4"
|
|
|
|
|
|
2019-01-24 00:47:55 +01:00
|
|
|
|
, # Filesystem label
|
2021-10-20 17:52:12 +02:00
|
|
|
|
label ? if onlyNixStore then "nix-store" else "nixos"
|
2019-01-24 00:47:55 +01:00
|
|
|
|
|
2015-09-27 21:01:43 +02:00
|
|
|
|
, # The initial NixOS configuration file to be copied to
|
|
|
|
|
# /etc/nixos/configuration.nix.
|
|
|
|
|
configFile ? null
|
|
|
|
|
|
2015-09-28 14:31:19 +02:00
|
|
|
|
, # Shell code executed after the VM has finished.
|
|
|
|
|
postVM ? ""
|
|
|
|
|
|
2021-10-20 17:52:12 +02:00
|
|
|
|
, # 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
|
|
|
|
|
|
2016-03-15 13:34:41 +01:00
|
|
|
|
, name ? "nixos-disk-image"
|
2016-03-30 21:48:12 +02:00
|
|
|
|
|
2020-05-20 14:29:23 +02:00
|
|
|
|
, # Disk image format, one of qcow2, qcow2-compressed, vdi, vpc, raw.
|
2017-11-11 20:26:50 +01:00
|
|
|
|
format ? "raw"
|
2021-10-05 19:08:57 +02:00
|
|
|
|
|
|
|
|
|
, # Whether a nix channel based on the current source tree should be
|
|
|
|
|
# made available inside the image. Useful for interactive use of nix
|
|
|
|
|
# utils, but changes the hash of the image when the sources are
|
|
|
|
|
# updated.
|
|
|
|
|
copyChannel ? true
|
2021-10-05 20:56:25 +02:00
|
|
|
|
|
|
|
|
|
, # Additional store paths to copy to the image's store.
|
|
|
|
|
additionalPaths ? []
|
2015-09-27 21:01:43 +02:00
|
|
|
|
}:
|
|
|
|
|
|
2020-10-28 20:53:10 +01:00
|
|
|
|
assert partitionTableType == "legacy" || partitionTableType == "legacy+gpt" || partitionTableType == "efi" || partitionTableType == "hybrid" || partitionTableType == "none";
|
2017-12-07 01:00:21 +01:00
|
|
|
|
# We use -E offset=X below, which is only supported by e2fsprogs
|
|
|
|
|
assert partitionTableType != "none" -> fsType == "ext4";
|
2020-11-19 17:18:02 +01:00
|
|
|
|
# Either both or none of {user,group} need to be set
|
|
|
|
|
assert lib.all
|
|
|
|
|
(attrs: ((attrs.user or null) == null)
|
|
|
|
|
== ((attrs.group or null) == null))
|
|
|
|
|
contents;
|
2021-10-20 17:52:12 +02:00
|
|
|
|
assert onlyNixStore -> contents == [] && configFile == null && !installBootLoader;
|
2017-12-07 01:00:21 +01:00
|
|
|
|
|
2015-09-27 21:01:43 +02:00
|
|
|
|
with lib;
|
|
|
|
|
|
2017-11-12 19:29:01 +01:00
|
|
|
|
let format' = format; in let
|
|
|
|
|
|
2018-02-07 19:35:43 +01:00
|
|
|
|
format = if format' == "qcow2-compressed" then "qcow2" else format';
|
2017-11-12 19:29:01 +01:00
|
|
|
|
|
|
|
|
|
compress = optionalString (format' == "qcow2-compressed") "-c";
|
2017-11-11 20:27:37 +01:00
|
|
|
|
|
2017-11-12 19:21:33 +01:00
|
|
|
|
filename = "nixos." + {
|
2017-08-11 00:57:26 +02:00
|
|
|
|
qcow2 = "qcow2";
|
2020-05-20 14:29:23 +02:00
|
|
|
|
vdi = "vdi";
|
2017-08-11 00:57:26 +02:00
|
|
|
|
vpc = "vhd";
|
|
|
|
|
raw = "img";
|
2020-05-20 14:29:23 +02:00
|
|
|
|
}.${format} or format;
|
2017-08-11 00:57:26 +02:00
|
|
|
|
|
2017-12-07 01:00:21 +01:00
|
|
|
|
rootPartition = { # switch-case
|
|
|
|
|
legacy = "1";
|
2020-10-28 20:53:10 +01:00
|
|
|
|
"legacy+gpt" = "2";
|
2017-12-07 01:00:21 +01:00
|
|
|
|
efi = "2";
|
2020-06-01 23:43:37 +02:00
|
|
|
|
hybrid = "3";
|
2017-12-07 01:00:21 +01:00
|
|
|
|
}.${partitionTableType};
|
|
|
|
|
|
|
|
|
|
partitionDiskScript = { # switch-case
|
|
|
|
|
legacy = ''
|
|
|
|
|
parted --script $diskImage -- \
|
|
|
|
|
mklabel msdos \
|
|
|
|
|
mkpart primary ext4 1MiB -1
|
|
|
|
|
'';
|
2020-10-28 20:53:10 +01:00
|
|
|
|
"legacy+gpt" = ''
|
|
|
|
|
parted --script $diskImage -- \
|
|
|
|
|
mklabel gpt \
|
|
|
|
|
mkpart no-fs 1MB 2MB \
|
|
|
|
|
set 1 bios_grub on \
|
|
|
|
|
align-check optimal 1 \
|
|
|
|
|
mkpart primary ext4 2MB -1 \
|
|
|
|
|
align-check optimal 2 \
|
|
|
|
|
print
|
|
|
|
|
'';
|
2017-12-07 01:00:21 +01:00
|
|
|
|
efi = ''
|
|
|
|
|
parted --script $diskImage -- \
|
|
|
|
|
mklabel gpt \
|
2020-06-01 23:43:37 +02:00
|
|
|
|
mkpart ESP fat32 8MiB ${bootSize} \
|
2017-12-07 01:00:21 +01:00
|
|
|
|
set 1 boot on \
|
2020-06-01 23:43:37 +02:00
|
|
|
|
mkpart primary ext4 ${bootSize} -1
|
|
|
|
|
'';
|
|
|
|
|
hybrid = ''
|
|
|
|
|
parted --script $diskImage -- \
|
|
|
|
|
mklabel gpt \
|
|
|
|
|
mkpart ESP fat32 8MiB ${bootSize} \
|
|
|
|
|
set 1 boot on \
|
|
|
|
|
mkpart no-fs 0 1024KiB \
|
|
|
|
|
set 2 bios_grub on \
|
|
|
|
|
mkpart primary ext4 ${bootSize} -1
|
2017-12-07 01:00:21 +01:00
|
|
|
|
'';
|
|
|
|
|
none = "";
|
|
|
|
|
}.${partitionTableType};
|
|
|
|
|
|
2017-11-11 20:26:50 +01:00
|
|
|
|
nixpkgs = cleanSource pkgs.path;
|
2017-04-17 05:08:37 +02:00
|
|
|
|
|
2018-02-07 19:35:43 +01:00
|
|
|
|
# FIXME: merge with channel.nix / make-channel.nix.
|
2017-04-01 02:00:00 +02:00
|
|
|
|
channelSources = pkgs.runCommand "nixos-${config.system.nixos.version}" {} ''
|
2017-04-17 05:08:37 +02:00
|
|
|
|
mkdir -p $out
|
2018-12-20 23:17:05 +01:00
|
|
|
|
cp -prd ${nixpkgs.outPath} $out/nixos
|
2017-04-17 05:08:37 +02:00
|
|
|
|
chmod -R u+w $out/nixos
|
|
|
|
|
if [ ! -e $out/nixos/nixpkgs ]; then
|
|
|
|
|
ln -s . $out/nixos/nixpkgs
|
|
|
|
|
fi
|
|
|
|
|
rm -rf $out/nixos/.git
|
2017-04-01 02:00:00 +02:00
|
|
|
|
echo -n ${config.system.nixos.versionSuffix} > $out/nixos/.version-suffix
|
2017-04-17 05:08:37 +02:00
|
|
|
|
'';
|
|
|
|
|
|
2018-02-07 19:35:43 +01:00
|
|
|
|
binPath = with pkgs; makeBinPath (
|
|
|
|
|
[ rsync
|
2020-11-24 16:29:28 +01:00
|
|
|
|
util-linux
|
2018-02-07 19:35:43 +01:00
|
|
|
|
parted
|
|
|
|
|
e2fsprogs
|
|
|
|
|
lkl
|
|
|
|
|
config.system.build.nixos-install
|
|
|
|
|
config.system.build.nixos-enter
|
|
|
|
|
nix
|
2022-04-05 11:57:50 +02:00
|
|
|
|
systemdMinimal
|
2018-02-07 19:35:43 +01:00
|
|
|
|
] ++ stdenv.initialPath);
|
2017-04-17 05:08:37 +02: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;
|
2020-11-19 17:18:02 +01:00
|
|
|
|
modes = map (x: x.mode or "''") contents;
|
|
|
|
|
users = map (x: x.user or "''") contents;
|
|
|
|
|
groups = map (x: x.group or "''") contents;
|
2017-04-17 05:08:37 +02:00
|
|
|
|
|
2021-10-05 20:56:25 +02:00
|
|
|
|
basePaths = [ config.system.build.toplevel ]
|
|
|
|
|
++ lib.optional copyChannel channelSources;
|
|
|
|
|
|
|
|
|
|
additionalPaths' = subtractLists basePaths additionalPaths;
|
|
|
|
|
|
2021-10-05 19:08:57 +02:00
|
|
|
|
closureInfo = pkgs.closureInfo {
|
2021-10-05 20:56:25 +02:00
|
|
|
|
rootPaths = basePaths ++ additionalPaths';
|
2021-10-05 19:08:57 +02:00
|
|
|
|
};
|
2018-02-07 19:35:43 +01:00
|
|
|
|
|
2021-04-23 10:15:14 +02:00
|
|
|
|
blockSize = toString (4 * 1024); # ext4fs block size (not block device sector size)
|
|
|
|
|
|
2017-04-17 05:08:37 +02:00
|
|
|
|
prepareImage = ''
|
2018-02-07 19:35:43 +01:00
|
|
|
|
export PATH=${binPath}
|
2017-12-07 01:00:21 +01:00
|
|
|
|
|
|
|
|
|
# Yes, mkfs.ext4 takes different units in different contexts. Fun.
|
|
|
|
|
sectorsToKilobytes() {
|
|
|
|
|
echo $(( ( "$1" * 512 ) / 1024 ))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sectorsToBytes() {
|
|
|
|
|
echo $(( "$1" * 512 ))
|
|
|
|
|
}
|
2017-04-17 05:08:37 +02:00
|
|
|
|
|
2021-04-23 10:15:14 +02:00
|
|
|
|
# Given lines of numbers, adds them together
|
|
|
|
|
sum_lines() {
|
|
|
|
|
local acc=0
|
|
|
|
|
while read -r number; do
|
|
|
|
|
acc=$((acc+number))
|
|
|
|
|
done
|
|
|
|
|
echo "$acc"
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-25 21:24:45 +02:00
|
|
|
|
mebibyte=$(( 1024 * 1024 ))
|
|
|
|
|
|
2021-04-24 05:18:08 +02:00
|
|
|
|
# Approximative percentage of reserved space in an ext4 fs over 512MiB.
|
|
|
|
|
# 0.05208587646484375
|
|
|
|
|
# × 1000, integer part: 52
|
|
|
|
|
compute_fudge() {
|
|
|
|
|
echo $(( $1 * 52 / 1000 ))
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 05:08:37 +02:00
|
|
|
|
mkdir $out
|
|
|
|
|
|
|
|
|
|
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})
|
2020-11-19 17:18:02 +01:00
|
|
|
|
modes_=(${concatStringsSep " " modes})
|
2017-04-17 05:08:37 +02:00
|
|
|
|
set +f
|
|
|
|
|
|
|
|
|
|
for ((i = 0; i < ''${#targets_[@]}; i++)); do
|
|
|
|
|
source="''${sources_[$i]}"
|
|
|
|
|
target="''${targets_[$i]}"
|
2020-11-19 17:18:02 +01:00
|
|
|
|
mode="''${modes_[$i]}"
|
2017-04-17 05:08:37 +02:00
|
|
|
|
|
2020-11-19 17:18:02 +01:00
|
|
|
|
if [ -n "$mode" ]; then
|
|
|
|
|
rsync_chmod_flags="--chmod=$mode"
|
|
|
|
|
else
|
|
|
|
|
rsync_chmod_flags=""
|
|
|
|
|
fi
|
|
|
|
|
# Unfortunately cptofs only supports modes, not ownership, so we can't use
|
|
|
|
|
# rsync's --chown option. Instead, we change the ownerships in the
|
|
|
|
|
# VM script with chown.
|
|
|
|
|
rsync_flags="-a --no-o --no-g $rsync_chmod_flags"
|
2017-04-17 05:08:37 +02:00
|
|
|
|
if [[ "$source" =~ '*' ]]; then
|
|
|
|
|
# If the source name contains '*', perform globbing.
|
|
|
|
|
mkdir -p $root/$target
|
|
|
|
|
for fn in $source; do
|
2020-11-19 17:18:02 +01:00
|
|
|
|
rsync $rsync_flags "$fn" $root/$target/
|
2017-04-17 05:08:37 +02:00
|
|
|
|
done
|
|
|
|
|
else
|
|
|
|
|
mkdir -p $root/$(dirname $target)
|
|
|
|
|
if ! [ -e $root/$target ]; then
|
2020-11-19 17:18:02 +01:00
|
|
|
|
rsync $rsync_flags $source $root/$target
|
2017-04-17 05:08:37 +02:00
|
|
|
|
else
|
|
|
|
|
echo "duplicate entry $target -> $source"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
2018-02-07 19:35:43 +01:00
|
|
|
|
export HOME=$TMPDIR
|
|
|
|
|
|
|
|
|
|
# Provide a Nix database so that nixos-install can copy closures.
|
|
|
|
|
export NIX_STATE_DIR=$TMPDIR/state
|
|
|
|
|
nix-store --load-db < ${closureInfo}/registration
|
2017-04-17 05:08:37 +02:00
|
|
|
|
|
2020-07-09 20:38:39 +02:00
|
|
|
|
chmod 755 "$TMPDIR"
|
2019-10-07 17:21:28 +02:00
|
|
|
|
echo "running nixos-install..."
|
|
|
|
|
nixos-install --root $root --no-bootloader --no-root-passwd \
|
2021-10-05 19:08:57 +02:00
|
|
|
|
--system ${config.system.build.toplevel} \
|
|
|
|
|
${if copyChannel then "--channel ${channelSources}" else "--no-channel-copy"} \
|
|
|
|
|
--substituters ""
|
2017-12-16 19:21:26 +01:00
|
|
|
|
|
2021-10-05 20:56:25 +02:00
|
|
|
|
${optionalString (additionalPaths' != []) ''
|
2021-11-26 14:11:05 +01:00
|
|
|
|
nix --extra-experimental-features nix-command copy --to $root --no-check-sigs ${concatStringsSep " " additionalPaths'}
|
2021-10-05 20:56:25 +02:00
|
|
|
|
''}
|
|
|
|
|
|
2020-06-01 23:43:37 +02:00
|
|
|
|
diskImage=nixos.raw
|
|
|
|
|
|
|
|
|
|
${if diskSize == "auto" then ''
|
|
|
|
|
${if partitionTableType == "efi" || partitionTableType == "hybrid" then ''
|
2021-04-24 05:20:49 +02:00
|
|
|
|
# Add the GPT at the end
|
|
|
|
|
gptSpace=$(( 512 * 34 * 1 ))
|
|
|
|
|
# Normally we'd need to account for alignment and things, if bootSize
|
|
|
|
|
# represented the actual size of the boot partition. But it instead
|
|
|
|
|
# represents the offset at which it ends.
|
|
|
|
|
# So we know bootSize is the reserved space in front of the partition.
|
|
|
|
|
reservedSpace=$(( gptSpace + $(numfmt --from=iec '${bootSize}') ))
|
|
|
|
|
'' else if partitionTableType == "legacy+gpt" then ''
|
|
|
|
|
# Add the GPT at the end
|
|
|
|
|
gptSpace=$(( 512 * 34 * 1 ))
|
|
|
|
|
# And include the bios_grub partition; the ext4 partition starts at 2MB exactly.
|
2021-04-25 21:24:45 +02:00
|
|
|
|
reservedSpace=$(( gptSpace + 2 * mebibyte ))
|
2021-04-24 05:20:49 +02:00
|
|
|
|
'' else if partitionTableType == "legacy" then ''
|
|
|
|
|
# Add the 1MiB aligned reserved space (includes MBR)
|
2021-04-25 21:24:45 +02:00
|
|
|
|
reservedSpace=$(( mebibyte ))
|
2020-06-01 23:43:37 +02:00
|
|
|
|
'' else ''
|
2021-04-24 05:20:49 +02:00
|
|
|
|
reservedSpace=0
|
2020-06-01 23:43:37 +02:00
|
|
|
|
''}
|
2021-04-24 05:20:49 +02:00
|
|
|
|
additionalSpace=$(( $(numfmt --from=iec '${additionalSpace}') + reservedSpace ))
|
2021-04-23 10:15:14 +02:00
|
|
|
|
|
|
|
|
|
# Compute required space in filesystem blocks
|
2021-06-22 04:58:07 +02:00
|
|
|
|
diskUsage=$(find . ! -type d -print0 | du --files0-from=- --apparent-size --block-size "${blockSize}" | cut -f1 | sum_lines)
|
2021-04-24 05:18:08 +02:00
|
|
|
|
# Each inode takes space!
|
|
|
|
|
numInodes=$(find . | wc -l)
|
|
|
|
|
# Convert to bytes, inodes take two blocks each!
|
|
|
|
|
diskUsage=$(( (diskUsage + 2 * numInodes) * ${blockSize} ))
|
|
|
|
|
# Then increase the required space to account for the reserved blocks.
|
|
|
|
|
fudge=$(compute_fudge $diskUsage)
|
|
|
|
|
requiredFilesystemSpace=$(( diskUsage + fudge ))
|
|
|
|
|
|
|
|
|
|
diskSize=$(( requiredFilesystemSpace + additionalSpace ))
|
2021-04-25 21:24:45 +02:00
|
|
|
|
|
|
|
|
|
# Round up to the nearest mebibyte.
|
|
|
|
|
# This ensures whole 512 bytes sector sizes in the disk image
|
|
|
|
|
# and helps towards aligning partitions optimally.
|
|
|
|
|
if (( diskSize % mebibyte )); then
|
|
|
|
|
diskSize=$(( ( diskSize / mebibyte + 1) * mebibyte ))
|
|
|
|
|
fi
|
|
|
|
|
|
2021-04-23 10:15:14 +02:00
|
|
|
|
truncate -s "$diskSize" $diskImage
|
|
|
|
|
|
|
|
|
|
printf "Automatic disk size...\n"
|
2021-04-24 05:18:08 +02:00
|
|
|
|
printf " Closure space use: %d bytes\n" $diskUsage
|
|
|
|
|
printf " fudge: %d bytes\n" $fudge
|
|
|
|
|
printf " Filesystem size needed: %d bytes\n" $requiredFilesystemSpace
|
2021-04-23 10:15:14 +02:00
|
|
|
|
printf " Additional space: %d bytes\n" $additionalSpace
|
|
|
|
|
printf " Disk image size: %d bytes\n" $diskSize
|
2020-06-01 23:43:37 +02:00
|
|
|
|
'' else ''
|
|
|
|
|
truncate -s ${toString diskSize}M $diskImage
|
|
|
|
|
''}
|
|
|
|
|
|
|
|
|
|
${partitionDiskScript}
|
|
|
|
|
|
|
|
|
|
${if partitionTableType != "none" then ''
|
|
|
|
|
# Get start & length of the root partition in sectors to $START and $SECTORS.
|
|
|
|
|
eval $(partx $diskImage -o START,SECTORS --nr ${rootPartition} --pairs)
|
|
|
|
|
|
2021-04-23 10:15:14 +02:00
|
|
|
|
mkfs.${fsType} -b ${blockSize} -F -L ${label} $diskImage -E offset=$(sectorsToBytes $START) $(sectorsToKilobytes $SECTORS)K
|
2020-06-01 23:43:37 +02:00
|
|
|
|
'' else ''
|
2021-04-23 10:15:14 +02:00
|
|
|
|
mkfs.${fsType} -b ${blockSize} -F -L ${label} $diskImage
|
2020-06-01 23:43:37 +02:00
|
|
|
|
''}
|
|
|
|
|
|
2017-04-17 05:08:37 +02:00
|
|
|
|
echo "copying staging root to image..."
|
2021-10-20 17:52:12 +02:00
|
|
|
|
cptofs -p ${optionalString (partitionTableType != "none") "-P ${rootPartition}"} \
|
|
|
|
|
-t ${fsType} \
|
|
|
|
|
-i $diskImage \
|
|
|
|
|
$root${optionalString onlyNixStore builtins.storeDir}/* / ||
|
2021-01-22 19:58:23 +01:00
|
|
|
|
(echo >&2 "ERROR: cptofs failed. diskSize might be too small for closure."; exit 1)
|
2017-04-17 05:08:37 +02:00
|
|
|
|
'';
|
2021-10-20 17:52:12 +02:00
|
|
|
|
|
|
|
|
|
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;
|
2020-11-24 16:29:28 +01:00
|
|
|
|
buildInputs = with pkgs; [ util-linux e2fsprogs dosfstools ];
|
2021-10-20 17:52:12 +02:00
|
|
|
|
postVM = moveOrConvertImage + postVM;
|
2015-10-06 15:03:26 +02:00
|
|
|
|
memSize = 1024;
|
2021-10-20 17:52:12 +02:00
|
|
|
|
} ''
|
2018-02-07 19:35:43 +01:00
|
|
|
|
export PATH=${binPath}:$PATH
|
|
|
|
|
|
2017-12-07 01:00:21 +01:00
|
|
|
|
rootDisk=${if partitionTableType != "none" then "/dev/vda${rootPartition}" else "/dev/vda"}
|
2015-09-27 21:01:43 +02:00
|
|
|
|
|
2017-04-17 05:08:37 +02:00
|
|
|
|
# Some tools assume these exist
|
2016-08-16 05:02:20 +02:00
|
|
|
|
ln -s vda /dev/xvda
|
|
|
|
|
ln -s vda /dev/sda
|
2021-04-20 23:00:05 +02:00
|
|
|
|
# make systemd-boot find ESP without udev
|
|
|
|
|
mkdir /dev/block
|
|
|
|
|
ln -s /dev/vda1 /dev/block/254:1
|
2015-09-27 21:01:43 +02:00
|
|
|
|
|
2017-04-17 05:08:37 +02:00
|
|
|
|
mountPoint=/mnt
|
|
|
|
|
mkdir $mountPoint
|
|
|
|
|
mount $rootDisk $mountPoint
|
2015-09-27 21:01:43 +02:00
|
|
|
|
|
2017-12-07 01:00:21 +01:00
|
|
|
|
# Create the ESP and mount it. Unlike e2fsprogs, mkfs.vfat doesn't support an
|
|
|
|
|
# '-E offset=X' option, so we can't do this outside the VM.
|
2020-06-01 23:43:37 +02:00
|
|
|
|
${optionalString (partitionTableType == "efi" || partitionTableType == "hybrid") ''
|
2017-12-07 01:00:21 +01:00
|
|
|
|
mkdir -p /mnt/boot
|
|
|
|
|
mkfs.vfat -n ESP /dev/vda1
|
|
|
|
|
mount /dev/vda1 /mnt/boot
|
|
|
|
|
''}
|
|
|
|
|
|
2017-04-17 05:08:37 +02:00
|
|
|
|
# Install a configuration.nix
|
2015-09-27 21:01:43 +02:00
|
|
|
|
mkdir -p /mnt/etc/nixos
|
|
|
|
|
${optionalString (configFile != null) ''
|
|
|
|
|
cp ${configFile} /mnt/etc/nixos/configuration.nix
|
|
|
|
|
''}
|
|
|
|
|
|
2021-10-05 15:50:04 +02:00
|
|
|
|
${lib.optionalString installBootLoader ''
|
|
|
|
|
# Set up core system link, GRUB, etc.
|
|
|
|
|
NIXOS_INSTALL_BOOTLOADER=1 nixos-enter --root $mountPoint -- /nix/var/nix/profiles/system/bin/switch-to-configuration boot
|
2017-02-23 00:47:24 +01:00
|
|
|
|
|
2021-10-05 15:50:04 +02: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-23 00:47:24 +01:00
|
|
|
|
|
2020-11-19 17:18:02 +01:00
|
|
|
|
# Set the ownerships of the contents. The modes are set in preVM.
|
|
|
|
|
# No globbing on targets, so no need to set -f
|
|
|
|
|
targets_=(${concatStringsSep " " targets})
|
|
|
|
|
users_=(${concatStringsSep " " users})
|
|
|
|
|
groups_=(${concatStringsSep " " groups})
|
|
|
|
|
for ((i = 0; i < ''${#targets_[@]}; i++)); do
|
|
|
|
|
target="''${targets_[$i]}"
|
|
|
|
|
user="''${users_[$i]}"
|
|
|
|
|
group="''${groups_[$i]}"
|
|
|
|
|
if [ -n "$user$group" ]; then
|
|
|
|
|
# We have to nixos-enter since we need to use the user and group of the VM
|
|
|
|
|
nixos-enter --root $mountPoint -- chown -R "$user:$group" "$target"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
2017-04-17 05:08:37 +02:00
|
|
|
|
umount -R /mnt
|
2015-09-27 21:01:43 +02:00
|
|
|
|
|
2017-02-26 03:02:22 +01: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 16:34:18 +01:00
|
|
|
|
${optionalString (fsType == "ext4") ''
|
2017-02-26 03:02:22 +01:00
|
|
|
|
tune2fs -T now -c 0 -i 0 $rootDisk
|
2016-12-06 16:34:18 +01:00
|
|
|
|
''}
|
2015-09-27 21:01:43 +02:00
|
|
|
|
''
|
2021-10-20 17:52:12 +02:00
|
|
|
|
);
|
|
|
|
|
in
|
|
|
|
|
if onlyNixStore then
|
|
|
|
|
pkgs.runCommand name {}
|
|
|
|
|
(prepareImage + moveOrConvertImage + postVM)
|
|
|
|
|
else buildImage
|