1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.amazonImage;
7in {
8
9 imports = [ ../../../modules/virtualisation/amazon-image.nix ];
10
11 # Amazon recomments setting this to the highest possible value for a good EBS
12 # experience, which prior to 4.15 was 255.
13 # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html#timeout-nvme-ebs-volumes
14 config.boot.kernelParams =
15 let timeout =
16 if pkgs.lib.versionAtLeast config.boot.kernelPackages.kernel.version "4.15"
17 then "4294967295"
18 else "255";
19 in [ "nvme_core.io_timeout=${timeout}" ];
20
21 options.amazonImage = {
22 name = mkOption {
23 type = types.str;
24 description = "The name of the generated derivation";
25 default = "nixos-amazon-image-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
26 };
27
28 contents = mkOption {
29 example = literalExample ''
30 [ { source = pkgs.memtest86 + "/memtest.bin";
31 target = "boot/memtest.bin";
32 }
33 ]
34 '';
35 default = [];
36 description = ''
37 This option lists files to be copied to fixed locations in the
38 generated image. Glob patterns work.
39 '';
40 };
41
42 sizeMB = mkOption {
43 type = with types; either (enum [ "auto" ]) int;
44 default = if config.ec2.hvm then 2048 else 8192;
45 example = 8192;
46 description = "The size in MB of the image";
47 };
48
49 format = mkOption {
50 type = types.enum [ "raw" "qcow2" "vpc" ];
51 default = "vpc";
52 description = "The image format to output";
53 };
54 };
55
56 config.system.build.amazonImage = import ../../../lib/make-disk-image.nix {
57 inherit lib config;
58 inherit (cfg) contents format name;
59 pkgs = import ../../../.. { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package
60 partitionTableType = if config.ec2.efi then "efi"
61 else if config.ec2.hvm then "legacy+gpt"
62 else "none";
63 diskSize = cfg.sizeMB;
64 fsType = "ext4";
65 configFile = pkgs.writeText "configuration.nix"
66 ''
67 { modulesPath, ... }: {
68 imports = [ "''${modulesPath}/virtualisation/amazon-image.nix" ];
69 ${optionalString config.ec2.hvm ''
70 ec2.hvm = true;
71 ''}
72 ${optionalString config.ec2.efi ''
73 ec2.efi = true;
74 ''}
75 }
76 '';
77 postVM = ''
78 extension=''${diskImage##*.}
79 friendlyName=$out/${cfg.name}.$extension
80 mv "$diskImage" "$friendlyName"
81 diskImage=$friendlyName
82
83 mkdir -p $out/nix-support
84 echo "file ${cfg.format} $diskImage" >> $out/nix-support/hydra-build-products
85
86 ${pkgs.jq}/bin/jq -n \
87 --arg label ${lib.escapeShellArg config.system.nixos.label} \
88 --arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
89 --arg logical_bytes "$(${pkgs.qemu}/bin/qemu-img info --output json "$diskImage" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
90 --arg file "$diskImage" \
91 '$ARGS.named' \
92 > $out/nix-support/image-info.json
93 '';
94 };
95}