2015-09-27 21:01:43 +02:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
|
2017-02-22 23:47:24 +00:00
|
|
|
let
|
2024-07-15 21:51:36 -07:00
|
|
|
inherit (lib)
|
|
|
|
mkOption
|
|
|
|
optionalString
|
|
|
|
types
|
|
|
|
versionAtLeast
|
|
|
|
;
|
|
|
|
inherit (lib.options) literalExpression;
|
2017-02-22 23:47:24 +00:00
|
|
|
cfg = config.amazonImage;
|
2021-11-10 14:12:01 -07:00
|
|
|
amiBootMode = if config.ec2.efi then "uefi" else "legacy-bios";
|
2017-02-22 23:47:24 +00:00
|
|
|
in
|
|
|
|
{
|
2024-09-10 19:14:43 +02:00
|
|
|
imports = [
|
|
|
|
../../../modules/virtualisation/amazon-image.nix
|
|
|
|
../../../modules/virtualisation/disk-size-option.nix
|
2024-11-15 01:10:33 +01:00
|
|
|
../../../modules/image/file-options.nix
|
2024-09-10 19:14:43 +02:00
|
|
|
(lib.mkRenamedOptionModuleWith {
|
|
|
|
sinceRelease = 2411;
|
|
|
|
from = [
|
|
|
|
"amazonImage"
|
|
|
|
"sizeMB"
|
|
|
|
];
|
|
|
|
to = [
|
|
|
|
"virtualisation"
|
|
|
|
"diskSize"
|
|
|
|
];
|
|
|
|
})
|
2024-11-15 01:10:33 +01:00
|
|
|
(lib.mkRenamedOptionModuleWith {
|
|
|
|
sinceRelease = 2505;
|
|
|
|
from = [
|
|
|
|
"amazonImage"
|
|
|
|
"name"
|
|
|
|
];
|
|
|
|
to = [
|
|
|
|
"image"
|
|
|
|
"baseName"
|
|
|
|
];
|
|
|
|
})
|
2024-09-10 19:14:43 +02:00
|
|
|
];
|
2015-09-27 21:01:43 +02:00
|
|
|
|
2023-05-19 22:11:26 -04:00
|
|
|
# Amazon recommends setting this to the highest possible value for a good EBS
|
2020-03-15 19:18:51 -04:00
|
|
|
# experience, which prior to 4.15 was 255.
|
2018-05-02 11:13:13 -04:00
|
|
|
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html#timeout-nvme-ebs-volumes
|
2020-03-15 19:18:51 -04:00
|
|
|
config.boot.kernelParams =
|
|
|
|
let
|
|
|
|
timeout =
|
2024-07-15 21:51:36 -07:00
|
|
|
if versionAtLeast config.boot.kernelPackages.kernel.version "4.15" then "4294967295" else "255";
|
2020-03-15 19:18:51 -04:00
|
|
|
in
|
|
|
|
[ "nvme_core.io_timeout=${timeout}" ];
|
2018-05-02 11:13:13 -04:00
|
|
|
|
2017-02-22 23:47:24 +00:00
|
|
|
options.amazonImage = {
|
|
|
|
contents = mkOption {
|
2021-10-03 18:06:03 +02:00
|
|
|
example = literalExpression ''
|
2017-02-22 23:47:24 +00:00
|
|
|
[ { source = pkgs.memtest86 + "/memtest.bin";
|
|
|
|
target = "boot/memtest.bin";
|
|
|
|
}
|
|
|
|
]
|
|
|
|
'';
|
|
|
|
default = [ ];
|
2022-08-29 19:33:50 +02:00
|
|
|
description = ''
|
2017-02-22 23:47:24 +00:00
|
|
|
This option lists files to be copied to fixed locations in the
|
|
|
|
generated image. Glob patterns work.
|
|
|
|
'';
|
|
|
|
};
|
2017-08-10 21:40:21 +00:00
|
|
|
|
|
|
|
format = mkOption {
|
2017-08-10 22:57:26 +00:00
|
|
|
type = types.enum [
|
|
|
|
"raw"
|
|
|
|
"qcow2"
|
|
|
|
"vpc"
|
|
|
|
];
|
2019-06-01 02:21:24 +09:00
|
|
|
default = "vpc";
|
2022-08-29 19:33:50 +02:00
|
|
|
description = "The image format to output";
|
2017-08-10 21:40:21 +00:00
|
|
|
};
|
2017-02-22 23:47:24 +00:00
|
|
|
};
|
|
|
|
|
2024-09-10 19:14:43 +02:00
|
|
|
# Use a priority just below mkOptionDefault (1500) instead of lib.mkDefault
|
|
|
|
# to avoid breaking existing configs using that.
|
2025-05-19 12:46:01 +02:00
|
|
|
config.virtualisation.diskSize = lib.mkOverride 1490 (4 * 1024);
|
2024-09-10 19:14:43 +02:00
|
|
|
config.virtualisation.diskSizeAutoSupported = !config.ec2.zfs.enable;
|
|
|
|
|
2024-11-15 01:10:33 +01:00
|
|
|
config.system.nixos.tags = [ "amazon" ];
|
|
|
|
config.system.build.image = config.system.build.amazonImage;
|
|
|
|
config.image.extension = cfg.format;
|
|
|
|
|
2021-08-25 09:38:46 -04:00
|
|
|
config.system.build.amazonImage =
|
|
|
|
let
|
2015-09-27 21:01:43 +02:00
|
|
|
configFile = pkgs.writeText "configuration.nix" ''
|
2020-08-08 08:48:28 +01:00
|
|
|
{ modulesPath, ... }: {
|
|
|
|
imports = [ "''${modulesPath}/virtualisation/amazon-image.nix" ];
|
2019-05-25 18:53:15 +09:00
|
|
|
${optionalString config.ec2.efi ''
|
|
|
|
ec2.efi = true;
|
|
|
|
''}
|
2021-08-25 09:38:46 -04: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 09:38:46 -04:00
|
|
|
|
2022-02-16 13:56:51 -05:00
|
|
|
zfsBuilder = import ../../../lib/make-multi-disk-zfs-image.nix {
|
2021-08-25 09:38:46 -04:00
|
|
|
inherit
|
|
|
|
lib
|
|
|
|
config
|
|
|
|
configFile
|
|
|
|
pkgs
|
|
|
|
;
|
2024-11-15 01:10:33 +01:00
|
|
|
inherit (cfg) contents format;
|
|
|
|
name = config.image.baseName;
|
2024-10-17 18:27:37 +02:00
|
|
|
|
2021-08-25 09:38:55 -04:00
|
|
|
includeChannel = true;
|
2024-10-17 18:27:37 +02:00
|
|
|
|
2021-08-25 09:38:55 -04:00
|
|
|
bootSize = 1000; # 1G is the minimum EBS volume
|
2024-10-17 18:27:37 +02:00
|
|
|
|
2024-09-10 19:14:43 +02:00
|
|
|
rootSize = config.virtualisation.diskSize;
|
2021-11-10 14:12:01 -07:00
|
|
|
rootPoolProperties = {
|
2021-08-25 09:38:55 -04:00
|
|
|
ashift = 12;
|
|
|
|
autoexpand = "on";
|
2024-10-17 18:27:37 +02:00
|
|
|
};
|
|
|
|
|
2021-08-25 09:38:46 -04:00
|
|
|
datasets = config.ec2.zfs.datasets;
|
2024-10-17 18:27:37 +02:00
|
|
|
|
2021-08-25 09:38:46 -04:00
|
|
|
postVM = ''
|
|
|
|
extension=''${rootDiskImage##*.}
|
2024-11-15 01:10:33 +01:00
|
|
|
friendlyName=$out/${config.image.baseName}
|
2021-08-25 09:38:46 -04:00
|
|
|
rootDisk="$friendlyName.root.$extension"
|
|
|
|
bootDisk="$friendlyName.boot.$extension"
|
|
|
|
mv "$rootDiskImage" "$rootDisk"
|
|
|
|
mv "$bootDiskImage" "$bootDisk"
|
2024-10-17 18:27:37 +02:00
|
|
|
|
2021-08-25 09:38:46 -04:00
|
|
|
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
|
2024-10-17 18:27:37 +02:00
|
|
|
|
2021-08-25 09:38:46 -04:00
|
|
|
${pkgs.jq}/bin/jq -n \
|
2025-05-19 12:37:31 +02:00
|
|
|
--arg system_version ${lib.escapeShellArg config.system.nixos.version} \
|
2021-08-25 09:38:46 -04:00
|
|
|
--arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
|
|
|
|
--arg root_logical_bytes "$(${pkgs.qemu_kvm}/bin/qemu-img info --output json "$rootDisk" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
|
2023-04-28 23:51:07 +02:00
|
|
|
--arg boot_logical_bytes "$(${pkgs.qemu_kvm}/bin/qemu-img info --output json "$bootDisk" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
|
2021-11-10 14:12:01 -07:00
|
|
|
--arg boot_mode "${amiBootMode}" \
|
2023-04-28 23:51:07 +02:00
|
|
|
--arg root "$rootDisk" \
|
|
|
|
--arg boot "$bootDisk" \
|
2024-10-17 18:27:37 +02:00
|
|
|
'{}
|
2025-05-19 12:37:31 +02:00
|
|
|
| .label = $system_version
|
2021-11-10 14:12:01 -07:00
|
|
|
| .boot_mode = $boot_mode
|
2021-08-25 09:38:55 -04:00
|
|
|
| .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
|
2024-10-17 18:27:37 +02:00
|
|
|
'';
|
2021-08-25 09:38:46 -04:00
|
|
|
};
|
2024-09-10 03:12:55 +02:00
|
|
|
|
2021-08-25 09:38:46 -04:00
|
|
|
extBuilder = import ../../../lib/make-disk-image.nix {
|
|
|
|
inherit
|
2024-10-17 18:27:37 +02:00
|
|
|
lib
|
2021-08-25 09:38:46 -04:00
|
|
|
config
|
|
|
|
configFile
|
|
|
|
pkgs
|
2024-10-17 18:27:37 +02:00
|
|
|
;
|
|
|
|
|
2024-11-15 01:10:33 +01:00
|
|
|
inherit (cfg) contents format;
|
|
|
|
inherit (config.image) baseName;
|
|
|
|
name = config.image.baseName;
|
2024-10-17 18:27:37 +02:00
|
|
|
|
2021-08-25 09:38:46 -04:00
|
|
|
fsType = "ext4";
|
2021-08-25 09:38:55 -04:00
|
|
|
partitionTableType = if config.ec2.efi then "efi" else "legacy+gpt";
|
2024-10-17 18:27:37 +02:00
|
|
|
|
2024-09-10 19:14:43 +02:00
|
|
|
inherit (config.virtualisation) diskSize;
|
2024-10-17 18:27:37 +02:00
|
|
|
|
2021-08-25 09:38:46 -04:00
|
|
|
postVM = ''
|
2021-08-25 09:38:55 -04:00
|
|
|
mkdir -p $out/nix-support
|
|
|
|
echo "file ${cfg.format} $diskImage" >> $out/nix-support/hydra-build-products
|
2024-10-17 18:27:37 +02:00
|
|
|
|
2024-09-02 13:02:31 +02:00
|
|
|
${pkgs.jq}/bin/jq -n \
|
2025-05-19 12:37:31 +02:00
|
|
|
--arg system_version ${lib.escapeShellArg config.system.nixos.version} \
|
2021-08-25 09:38:46 -04:00
|
|
|
--arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
|
|
|
|
--arg logical_bytes "$(${pkgs.qemu_kvm}/bin/qemu-img info --output json "$diskImage" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
|
2021-11-10 14:12:01 -07:00
|
|
|
--arg boot_mode "${amiBootMode}" \
|
2021-08-25 09:38:46 -04:00
|
|
|
--arg file "$diskImage" \
|
2024-09-10 03:12:55 +02:00
|
|
|
'{}
|
2025-05-19 12:37:31 +02:00
|
|
|
| .label = $system_version
|
2021-11-10 14:12:01 -07:00
|
|
|
| .boot_mode = $boot_mode
|
2021-08-25 09:38:55 -04:00
|
|
|
| .system = $system
|
|
|
|
| .logical_bytes = $logical_bytes
|
|
|
|
| .file = $file
|
|
|
|
| .disks.root.logical_bytes = $logical_bytes
|
|
|
|
| .disks.root.file = $file
|
|
|
|
' > $out/nix-support/image-info.json
|
2024-09-10 03:12:55 +02:00
|
|
|
'';
|
|
|
|
};
|
2021-08-25 09:38:46 -04:00
|
|
|
in
|
|
|
|
if config.ec2.zfs.enable then zfsBuilder else extBuilder;
|
2024-02-09 18:01:17 +01:00
|
|
|
|
2024-07-15 21:51:36 -07:00
|
|
|
meta.maintainers = with lib.maintainers; [ arianvp ];
|
2015-09-27 21:01:43 +02:00
|
|
|
}
|