1 # Configuration for Amazon EC2 instances. (Note that this file is a
2 # misnomer - it should be "amazon-config.nix" or so, not
3 # "amazon-image.nix", since it's used not only to build images but
4 # also to reconfigure instances. However, we can't rename it because
5 # existing "configuration.nix" files on EC2 instances refer to it.)
7 { config, lib, pkgs, ... }:
10 inherit (lib) mkDefault mkIf;
16 ../profiles/headless.nix
17 # Note: While we do use the headless profile, we also explicitly
18 # turn on the serial console on ttyS0 below. This is because
19 # AWS does support accessing the serial console:
20 # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configure-access-to-serial-console.html
29 boot.growPartition = true;
31 fileSystems."/" = mkIf (!cfg.zfs.enable) {
32 device = "/dev/disk/by-label/nixos";
37 fileSystems."/boot" = mkIf (cfg.efi || cfg.zfs.enable) {
38 # The ZFS image uses a partition labeled ESP whether or not we're
40 device = "/dev/disk/by-label/ESP";
44 services.zfs.expandOnBoot = mkIf cfg.zfs.enable "all";
46 boot.zfs.devNodes = mkIf cfg.zfs.enable "/dev/";
48 boot.extraModulePackages = [
49 config.boot.kernelPackages.ena
51 boot.initrd.kernelModules = [ "xen-blkfront" ];
52 boot.initrd.availableKernelModules = [ "nvme" ];
53 boot.kernelParams = [ "console=ttyS0,115200n8" "random.trust_cpu=on" ];
55 # Prevent the nouveau kernel module from being loaded, as it
56 # interferes with the nvidia/nvidia-uvm modules needed for CUDA.
57 # Also blacklist xen_fbfront to prevent a 30 second delay during
59 boot.blacklistedKernelModules = [ "nouveau" "xen_fbfront" ];
61 boot.loader.grub.device = if cfg.efi then "nodev" else "/dev/xvda";
62 boot.loader.grub.efiSupport = cfg.efi;
63 boot.loader.grub.efiInstallAsRemovable = cfg.efi;
64 boot.loader.timeout = 1;
65 boot.loader.grub.extraConfig = ''
66 serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
67 terminal_output console serial
68 terminal_input console serial
71 systemd.services.fetch-ec2-metadata = {
72 wantedBy = [ "multi-user.target" ];
73 wants = [ "network-online.target" ];
74 after = ["network-online.target"];
76 script = builtins.readFile ./ec2-metadata-fetcher.sh;
77 serviceConfig.Type = "oneshot";
78 serviceConfig.StandardOutput = "journal+console";
81 # Amazon-issued AMIs include the SSM Agent by default, so we do the same.
82 # https://docs.aws.amazon.com/systems-manager/latest/userguide/ami-preinstalled-agent.html
83 services.amazon-ssm-agent.enable = true;
85 # Allow root logins only using the SSH key that the user specified
86 # at instance creation time.
87 services.openssh.enable = true;
88 services.openssh.settings.PermitRootLogin = "prohibit-password";
90 # Enable the serial console on ttyS0
91 systemd.services."serial-getty@ttyS0".enable = true;
93 # Creates symlinks for block device names.
94 services.udev.packages = [ pkgs.amazon-ec2-utils ];
96 # Force getting the hostname from EC2.
97 networking.hostName = mkDefault "";
99 # Always include cryptsetup so that Charon can use it.
100 environment.systemPackages = [ pkgs.cryptsetup ];
102 # EC2 has its own NTP server provided by the hypervisor
103 networking.timeServers = [ "169.254.169.123" ];
105 # udisks has become too bloated to have in a headless system
106 # (e.g. it depends on GTK).
107 services.udisks2.enable = false;
109 meta.maintainers = with lib.maintainers; [ arianvp ];