1 { config, lib, pkgs, ... }:
4 inherit (lib) mkOption optionalString types versionAtLeast;
5 inherit (lib.options) literalExpression;
6 cfg = config.amazonImage;
7 amiBootMode = if config.ec2.efi then "uefi" else "legacy-bios";
11 imports = [ ../../../modules/virtualisation/amazon-image.nix ];
13 # Amazon recommends setting this to the highest possible value for a good EBS
14 # experience, which prior to 4.15 was 255.
15 # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html#timeout-nvme-ebs-volumes
16 config.boot.kernelParams =
18 if versionAtLeast config.boot.kernelPackages.kernel.version "4.15"
21 in [ "nvme_core.io_timeout=${timeout}" ];
23 options.amazonImage = {
26 description = "The name of the generated derivation";
27 default = "nixos-amazon-image-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
31 example = literalExpression ''
32 [ { source = pkgs.memtest86 + "/memtest.bin";
33 target = "boot/memtest.bin";
39 This option lists files to be copied to fixed locations in the
40 generated image. Glob patterns work.
45 type = with types; either (enum [ "auto" ]) int;
48 description = "The size in MB of the image";
52 type = types.enum [ "raw" "qcow2" "vpc" ];
54 description = "The image format to output";
58 config.system.build.amazonImage = let
59 configFile = pkgs.writeText "configuration.nix"
61 { modulesPath, ... }: {
62 imports = [ "''${modulesPath}/virtualisation/amazon-image.nix" ];
63 ${optionalString config.ec2.efi ''
66 ${optionalString config.ec2.zfs.enable ''
67 ec2.zfs.enable = true;
68 networking.hostId = "${config.networking.hostId}";
73 zfsBuilder = import ../../../lib/make-multi-disk-zfs-image.nix {
74 inherit lib config configFile pkgs;
75 inherit (cfg) contents format name;
77 includeChannel = true;
79 bootSize = 1000; # 1G is the minimum EBS volume
81 rootSize = cfg.sizeMB;
82 rootPoolProperties = {
87 datasets = config.ec2.zfs.datasets;
90 extension=''${rootDiskImage##*.}
91 friendlyName=$out/${cfg.name}
92 rootDisk="$friendlyName.root.$extension"
93 bootDisk="$friendlyName.boot.$extension"
94 mv "$rootDiskImage" "$rootDisk"
95 mv "$bootDiskImage" "$bootDisk"
97 mkdir -p $out/nix-support
98 echo "file ${cfg.format} $bootDisk" >> $out/nix-support/hydra-build-products
99 echo "file ${cfg.format} $rootDisk" >> $out/nix-support/hydra-build-products
101 ${pkgs.jq}/bin/jq -n \
102 --arg system_label ${lib.escapeShellArg config.system.nixos.label} \
103 --arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
104 --arg root_logical_bytes "$(${pkgs.qemu_kvm}/bin/qemu-img info --output json "$rootDisk" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
105 --arg boot_logical_bytes "$(${pkgs.qemu_kvm}/bin/qemu-img info --output json "$bootDisk" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
106 --arg boot_mode "${amiBootMode}" \
107 --arg root "$rootDisk" \
108 --arg boot "$bootDisk" \
110 | .label = $system_label
111 | .boot_mode = $boot_mode
113 | .disks.boot.logical_bytes = $boot_logical_bytes
114 | .disks.boot.file = $boot
115 | .disks.root.logical_bytes = $root_logical_bytes
116 | .disks.root.file = $root
117 ' > $out/nix-support/image-info.json
121 extBuilder = import ../../../lib/make-disk-image.nix {
122 inherit lib config configFile pkgs;
124 inherit (cfg) contents format name;
127 partitionTableType = if config.ec2.efi then "efi" else "legacy+gpt";
129 diskSize = cfg.sizeMB;
132 extension=''${diskImage##*.}
133 friendlyName=$out/${cfg.name}.$extension
134 mv "$diskImage" "$friendlyName"
135 diskImage=$friendlyName
137 mkdir -p $out/nix-support
138 echo "file ${cfg.format} $diskImage" >> $out/nix-support/hydra-build-products
140 ${pkgs.jq}/bin/jq -n \
141 --arg system_label ${lib.escapeShellArg config.system.nixos.label} \
142 --arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
143 --arg logical_bytes "$(${pkgs.qemu_kvm}/bin/qemu-img info --output json "$diskImage" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
144 --arg boot_mode "${amiBootMode}" \
145 --arg file "$diskImage" \
147 | .label = $system_label
148 | .boot_mode = $boot_mode
150 | .logical_bytes = $logical_bytes
152 | .disks.root.logical_bytes = $logical_bytes
153 | .disks.root.file = $file
154 ' > $out/nix-support/image-info.json
157 in if config.ec2.zfs.enable then zfsBuilder else extBuilder;
159 meta.maintainers = with lib.maintainers; [ arianvp ];