15 inherit (lib.options) literalExpression;
16 cfg = config.amazonImage;
17 amiBootMode = if config.ec2.efi then "uefi" else "legacy-bios";
21 ../../../modules/virtualisation/amazon-image.nix
22 ../../../modules/virtualisation/disk-size-option.nix
23 ../../../modules/image/file-options.nix
24 (lib.mkRenamedOptionModuleWith {
35 (lib.mkRenamedOptionModuleWith {
48 # Amazon recommends setting this to the highest possible value for a good EBS
49 # experience, which prior to 4.15 was 255.
50 # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html#timeout-nvme-ebs-volumes
51 config.boot.kernelParams =
54 if versionAtLeast config.boot.kernelPackages.kernel.version "4.15" then "4294967295" else "255";
56 [ "nvme_core.io_timeout=${timeout}" ];
58 options.amazonImage = {
60 example = literalExpression ''
61 [ { source = pkgs.memtest86 + "/memtest.bin";
62 target = "boot/memtest.bin";
68 This option lists files to be copied to fixed locations in the
69 generated image. Glob patterns work.
80 description = "The image format to output";
84 # Use a priority just below mkOptionDefault (1500) instead of lib.mkDefault
85 # to avoid breaking existing configs using that.
86 config.virtualisation.diskSize = lib.mkOverride 1490 (3 * 1024);
87 config.virtualisation.diskSizeAutoSupported = !config.ec2.zfs.enable;
89 config.system.nixos.tags = [ "amazon" ];
90 config.system.build.image = config.system.build.amazonImage;
91 config.image.extension = cfg.format;
93 config.system.build.amazonImage =
95 configFile = pkgs.writeText "configuration.nix" ''
96 { modulesPath, ... }: {
97 imports = [ "''${modulesPath}/virtualisation/amazon-image.nix" ];
98 ${optionalString config.ec2.efi ''
101 ${optionalString config.ec2.zfs.enable ''
102 ec2.zfs.enable = true;
103 networking.hostId = "${config.networking.hostId}";
108 zfsBuilder = import ../../../lib/make-multi-disk-zfs-image.nix {
115 inherit (cfg) contents format;
116 name = config.image.baseName;
118 includeChannel = true;
120 bootSize = 1000; # 1G is the minimum EBS volume
122 rootSize = config.virtualisation.diskSize;
123 rootPoolProperties = {
128 datasets = config.ec2.zfs.datasets;
131 extension=''${rootDiskImage##*.}
132 friendlyName=$out/${config.image.baseName}
133 rootDisk="$friendlyName.root.$extension"
134 bootDisk="$friendlyName.boot.$extension"
135 mv "$rootDiskImage" "$rootDisk"
136 mv "$bootDiskImage" "$bootDisk"
138 mkdir -p $out/nix-support
139 echo "file ${cfg.format} $bootDisk" >> $out/nix-support/hydra-build-products
140 echo "file ${cfg.format} $rootDisk" >> $out/nix-support/hydra-build-products
142 ${pkgs.jq}/bin/jq -n \
143 --arg system_label ${lib.escapeShellArg config.system.nixos.label} \
144 --arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
145 --arg root_logical_bytes "$(${pkgs.qemu_kvm}/bin/qemu-img info --output json "$rootDisk" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
146 --arg boot_logical_bytes "$(${pkgs.qemu_kvm}/bin/qemu-img info --output json "$bootDisk" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
147 --arg boot_mode "${amiBootMode}" \
148 --arg root "$rootDisk" \
149 --arg boot "$bootDisk" \
151 | .label = $system_label
152 | .boot_mode = $boot_mode
154 | .disks.boot.logical_bytes = $boot_logical_bytes
155 | .disks.boot.file = $boot
156 | .disks.root.logical_bytes = $root_logical_bytes
157 | .disks.root.file = $root
158 ' > $out/nix-support/image-info.json
162 extBuilder = import ../../../lib/make-disk-image.nix {
170 inherit (cfg) contents format;
171 inherit (config.image) baseName;
172 name = config.image.baseName;
175 partitionTableType = if config.ec2.efi then "efi" else "legacy+gpt";
177 inherit (config.virtualisation) diskSize;
180 mkdir -p $out/nix-support
181 echo "file ${cfg.format} $diskImage" >> $out/nix-support/hydra-build-products
183 ${pkgs.jq}/bin/jq -n \
184 --arg system_label ${lib.escapeShellArg config.system.nixos.label} \
185 --arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
186 --arg logical_bytes "$(${pkgs.qemu_kvm}/bin/qemu-img info --output json "$diskImage" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
187 --arg boot_mode "${amiBootMode}" \
188 --arg file "$diskImage" \
190 | .label = $system_label
191 | .boot_mode = $boot_mode
193 | .logical_bytes = $logical_bytes
195 | .disks.root.logical_bytes = $logical_bytes
196 | .disks.root.file = $file
197 ' > $out/nix-support/image-info.json
201 if config.ec2.zfs.enable then zfsBuilder else extBuilder;
203 meta.maintainers = with lib.maintainers; [ arianvp ];