2015-09-27 21:01:43 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
2017-02-23 00:47:24 +01:00
|
|
|
let
|
|
|
|
cfg = config.amazonImage;
|
2021-08-25 15:38:46 +02:00
|
|
|
|
2017-02-23 00:47:24 +01:00
|
|
|
in {
|
2015-09-27 21:01:43 +02:00
|
|
|
|
2017-04-17 05:08:37 +02:00
|
|
|
imports = [ ../../../modules/virtualisation/amazon-image.nix ];
|
2015-09-27 21:01:43 +02:00
|
|
|
|
2020-03-16 00:18:51 +01:00
|
|
|
# Amazon recomments setting this to the highest possible value for a good EBS
|
|
|
|
# experience, which prior to 4.15 was 255.
|
2018-05-02 17:13:13 +02:00
|
|
|
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html#timeout-nvme-ebs-volumes
|
2020-03-16 00:18:51 +01:00
|
|
|
config.boot.kernelParams =
|
|
|
|
let timeout =
|
|
|
|
if pkgs.lib.versionAtLeast config.boot.kernelPackages.kernel.version "4.15"
|
|
|
|
then "4294967295"
|
|
|
|
else "255";
|
|
|
|
in [ "nvme_core.io_timeout=${timeout}" ];
|
2018-05-02 17:13:13 +02:00
|
|
|
|
2017-02-23 00:47:24 +01:00
|
|
|
options.amazonImage = {
|
2017-10-23 22:22:16 +02:00
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "The name of the generated derivation";
|
2019-05-31 21:19:18 +02:00
|
|
|
default = "nixos-amazon-image-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
|
2017-10-23 22:22:16 +02:00
|
|
|
};
|
|
|
|
|
2017-02-23 00:47:24 +01:00
|
|
|
contents = mkOption {
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression ''
|
2017-02-23 00:47:24 +01:00
|
|
|
[ { source = pkgs.memtest86 + "/memtest.bin";
|
|
|
|
target = "boot/memtest.bin";
|
|
|
|
}
|
|
|
|
]
|
|
|
|
'';
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
This option lists files to be copied to fixed locations in the
|
|
|
|
generated image. Glob patterns work.
|
|
|
|
'';
|
|
|
|
};
|
2017-08-10 23:40:21 +02:00
|
|
|
|
2017-08-11 03:57:38 +02:00
|
|
|
sizeMB = mkOption {
|
2020-12-19 19:41:11 +01:00
|
|
|
type = with types; either (enum [ "auto" ]) int;
|
2021-05-01 04:19:42 +02:00
|
|
|
default = if config.ec2.hvm then 2048 else 8192;
|
2020-12-19 19:41:11 +01:00
|
|
|
example = 8192;
|
2017-08-11 03:57:38 +02:00
|
|
|
description = "The size in MB of the image";
|
|
|
|
};
|
|
|
|
|
2017-08-10 23:40:21 +02:00
|
|
|
format = mkOption {
|
2017-08-11 00:57:26 +02:00
|
|
|
type = types.enum [ "raw" "qcow2" "vpc" ];
|
2019-05-31 19:21:24 +02:00
|
|
|
default = "vpc";
|
2017-08-10 23:40:21 +02:00
|
|
|
description = "The image format to output";
|
|
|
|
};
|
2017-02-23 00:47:24 +01:00
|
|
|
};
|
|
|
|
|
2021-08-25 15:38:46 +02:00
|
|
|
config.system.build.amazonImage = let
|
2015-09-27 21:01:43 +02:00
|
|
|
configFile = pkgs.writeText "configuration.nix"
|
|
|
|
''
|
2020-08-08 09:48:28 +02:00
|
|
|
{ modulesPath, ... }: {
|
|
|
|
imports = [ "''${modulesPath}/virtualisation/amazon-image.nix" ];
|
2015-09-27 21:01:43 +02:00
|
|
|
${optionalString config.ec2.hvm ''
|
|
|
|
ec2.hvm = true;
|
|
|
|
''}
|
2019-05-25 11:53:15 +02:00
|
|
|
${optionalString config.ec2.efi ''
|
|
|
|
ec2.efi = true;
|
|
|
|
''}
|
2021-08-25 15:38:46 +02:00
|
|
|
${optionalString config.ec2.zfs.enable ''
|
|
|
|
ec2.zfs.enable = true;
|
|
|
|
networking.hostId = "${config.networking.hostId}";
|
|
|
|
''}
|
2015-09-27 21:01:43 +02:00
|
|
|
}
|
|
|
|
'';
|
2021-08-25 15:38:46 +02:00
|
|
|
|
|
|
|
zfsBuilder = import ../../../lib/make-zfs-image.nix {
|
|
|
|
inherit lib config configFile;
|
|
|
|
inherit (cfg) contents format name;
|
|
|
|
pkgs = import ../../../.. { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package
|
|
|
|
|
|
|
|
includeChannel = true;
|
|
|
|
|
|
|
|
bootSize = 1000; # 1G is the minimum EBS volume
|
|
|
|
|
|
|
|
rootSize = cfg.sizeMB;
|
|
|
|
rootPoolProperties = {
|
|
|
|
ashift = 12;
|
|
|
|
autoexpand = "on";
|
|
|
|
};
|
|
|
|
|
|
|
|
datasets = config.ec2.zfs.datasets;
|
|
|
|
|
|
|
|
postVM = ''
|
|
|
|
extension=''${rootDiskImage##*.}
|
|
|
|
friendlyName=$out/${cfg.name}
|
|
|
|
rootDisk="$friendlyName.root.$extension"
|
|
|
|
bootDisk="$friendlyName.boot.$extension"
|
|
|
|
mv "$rootDiskImage" "$rootDisk"
|
|
|
|
mv "$bootDiskImage" "$bootDisk"
|
|
|
|
|
|
|
|
mkdir -p $out/nix-support
|
|
|
|
echo "file ${cfg.format} $bootDisk" >> $out/nix-support/hydra-build-products
|
|
|
|
echo "file ${cfg.format} $rootDisk" >> $out/nix-support/hydra-build-products
|
|
|
|
|
|
|
|
${pkgs.jq}/bin/jq -n \
|
2021-08-25 15:38:55 +02:00
|
|
|
--arg system_label ${lib.escapeShellArg config.system.nixos.label} \
|
2021-08-25 15:38:46 +02:00
|
|
|
--arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
|
2021-09-23 00:39:58 +02:00
|
|
|
--arg root_logical_bytes "$(${pkgs.qemu}/bin/qemu-img info --output json "$rootDisk" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
|
|
|
|
--arg boot_logical_bytes "$(${pkgs.qemu}/bin/qemu-img info --output json "$bootDisk" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
|
2021-08-25 15:38:46 +02:00
|
|
|
--arg root "$rootDisk" \
|
|
|
|
--arg boot "$bootDisk" \
|
2021-08-25 15:38:55 +02:00
|
|
|
'{}
|
|
|
|
| .label = $system_label
|
|
|
|
| .system = $system
|
|
|
|
| .disks.boot.logical_bytes = $boot_logical_bytes
|
|
|
|
| .disks.boot.file = $boot
|
|
|
|
| .disks.root.logical_bytes = $root_logical_bytes
|
|
|
|
| .disks.root.file = $root
|
|
|
|
' > $out/nix-support/image-info.json
|
2021-08-25 15:38:46 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extBuilder = import ../../../lib/make-disk-image.nix {
|
|
|
|
inherit lib config configFile;
|
|
|
|
|
|
|
|
inherit (cfg) contents format name;
|
|
|
|
pkgs = import ../../../.. { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package
|
|
|
|
|
|
|
|
fsType = "ext4";
|
|
|
|
partitionTableType = if config.ec2.efi then "efi"
|
|
|
|
else if config.ec2.hvm then "legacy+gpt"
|
|
|
|
else "none";
|
|
|
|
|
|
|
|
diskSize = cfg.sizeMB;
|
|
|
|
|
|
|
|
postVM = ''
|
|
|
|
extension=''${diskImage##*.}
|
|
|
|
friendlyName=$out/${cfg.name}.$extension
|
|
|
|
mv "$diskImage" "$friendlyName"
|
|
|
|
diskImage=$friendlyName
|
|
|
|
|
|
|
|
mkdir -p $out/nix-support
|
|
|
|
echo "file ${cfg.format} $diskImage" >> $out/nix-support/hydra-build-products
|
|
|
|
|
|
|
|
${pkgs.jq}/bin/jq -n \
|
2021-08-25 15:38:55 +02:00
|
|
|
--arg system_label ${lib.escapeShellArg config.system.nixos.label} \
|
2021-08-25 15:38:46 +02:00
|
|
|
--arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
|
|
|
|
--arg logical_bytes "$(${pkgs.qemu}/bin/qemu-img info --output json "$diskImage" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
|
|
|
|
--arg file "$diskImage" \
|
2021-08-25 15:38:55 +02:00
|
|
|
'{}
|
|
|
|
| .label = $system_label
|
|
|
|
| .system = $system
|
|
|
|
| .logical_bytes = $logical_bytes
|
|
|
|
| .file = $file
|
|
|
|
| .disks.root.logical_bytes = $logical_bytes
|
|
|
|
| .disks.root.file = $file
|
|
|
|
' > $out/nix-support/image-info.json
|
2021-08-25 15:38:46 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
in if config.ec2.zfs.enable then zfsBuilder else extBuilder;
|
2015-09-27 21:01:43 +02:00
|
|
|
}
|