summary refs log tree commit diff
path: root/nixos/maintainers/scripts/ec2/amazon-image.nix
blob: 31e15537179a91fa53290f165697c23491f98ad0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.amazonImage;
in {

  imports = [ ../../../modules/virtualisation/amazon-image.nix ];

  # Required to provide good EBS experience,
  # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html#timeout-nvme-ebs-volumes
  # TODO change value to 4294967295 when kernel is updated to 4.15 or later
  config.boot.kernelParams = [ "nvme_core.io_timeout=255" ];

  options.amazonImage = {
    name = mkOption {
      type = types.str;
      description = "The name of the generated derivation";
      default = "nixos-amazon-image-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
    };

    contents = mkOption {
      example = literalExample ''
        [ { 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.
      '';
    };

    sizeMB = mkOption {
      type = types.int;
      default = if config.ec2.hvm then 2048 else 8192;
      description = "The size in MB of the image";
    };

    format = mkOption {
      type = types.enum [ "raw" "qcow2" "vpc" ];
      default = "vpc";
      description = "The image format to output";
    };
  };

  config.system.build.amazonImage = import ../../../lib/make-disk-image.nix {
    inherit lib config;
    inherit (cfg) contents format name;
    pkgs = import ../../../.. { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package
    partitionTableType = if config.ec2.efi then "efi"
                         else if config.ec2.hvm then "legacy"
                         else "none";
    diskSize = cfg.sizeMB;
    fsType = "ext4";
    configFile = pkgs.writeText "configuration.nix"
      ''
        {
          imports = [ <nixpkgs/nixos/modules/virtualisation/amazon-image.nix> ];
          ${optionalString config.ec2.hvm ''
            ec2.hvm = true;
          ''}
          ${optionalString config.ec2.efi ''
            ec2.efi = true;
          ''}
        }
      '';
    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 \
        --arg label ${lib.escapeShellArg config.system.nixos.label} \
        --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" \
        '$ARGS.named' \
        > $out/nix-support/image-info.json
    '';
  };
}