2020-04-24 23:36:52 +00:00
|
|
|
# Configuration for Amazon EC2 instances. (Note that this file is a
|
|
|
|
# misnomer - it should be "amazon-config.nix" or so, not
|
|
|
|
# "amazon-image.nix", since it's used not only to build images but
|
|
|
|
# also to reconfigure instances. However, we can't rename it because
|
|
|
|
# existing "configuration.nix" files on EC2 instances refer to it.)
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.ec2;
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2021-08-22 07:53:02 +00:00
|
|
|
imports = [
|
|
|
|
../profiles/headless.nix
|
|
|
|
# Note: While we do use the headless profile, we also explicitly
|
|
|
|
# turn on the serial console on ttyS0 below. This is because
|
|
|
|
# AWS does support accessing the serial console:
|
|
|
|
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configure-access-to-serial-console.html
|
|
|
|
./ec2-data.nix
|
|
|
|
./amazon-init.nix
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
config = {
|
|
|
|
|
|
|
|
assertions = [
|
2022-05-18 14:49:53 +00:00
|
|
|
{ assertion = versionOlder config.boot.kernelPackages.kernel.version "5.17";
|
|
|
|
message = "ENA driver fails to build with kernel >= 5.17";
|
2022-03-05 16:20:37 +00:00
|
|
|
}
|
2020-04-24 23:36:52 +00:00
|
|
|
];
|
|
|
|
|
2022-11-27 09:42:12 +00:00
|
|
|
boot.growPartition = true;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-08-27 14:25:00 +00:00
|
|
|
fileSystems."/" = mkIf (!cfg.zfs.enable) {
|
2020-04-24 23:36:52 +00:00
|
|
|
device = "/dev/disk/by-label/nixos";
|
|
|
|
fsType = "ext4";
|
|
|
|
autoResize = true;
|
|
|
|
};
|
|
|
|
|
2021-08-27 14:25:00 +00:00
|
|
|
fileSystems."/boot" = mkIf (cfg.efi || cfg.zfs.enable) {
|
|
|
|
# The ZFS image uses a partition labeled ESP whether or not we're
|
|
|
|
# booting with EFI.
|
2020-04-24 23:36:52 +00:00
|
|
|
device = "/dev/disk/by-label/ESP";
|
|
|
|
fsType = "vfat";
|
|
|
|
};
|
|
|
|
|
2021-08-27 14:25:00 +00:00
|
|
|
services.zfs.expandOnBoot = mkIf cfg.zfs.enable "all";
|
|
|
|
|
|
|
|
boot.zfs.devNodes = mkIf cfg.zfs.enable "/dev/";
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
boot.extraModulePackages = [
|
|
|
|
config.boot.kernelPackages.ena
|
|
|
|
];
|
2022-11-27 09:42:12 +00:00
|
|
|
boot.initrd.kernelModules = [ "xen-blkfront" ];
|
|
|
|
boot.initrd.availableKernelModules = [ "nvme" ];
|
|
|
|
boot.kernelParams = [ "console=ttyS0,115200n8" "random.trust_cpu=on" ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Prevent the nouveau kernel module from being loaded, as it
|
|
|
|
# interferes with the nvidia/nvidia-uvm modules needed for CUDA.
|
|
|
|
# Also blacklist xen_fbfront to prevent a 30 second delay during
|
|
|
|
# boot.
|
|
|
|
boot.blacklistedKernelModules = [ "nouveau" "xen_fbfront" ];
|
|
|
|
|
2022-11-27 09:42:12 +00:00
|
|
|
boot.loader.grub.device = if cfg.efi then "nodev" else "/dev/xvda";
|
2020-04-24 23:36:52 +00:00
|
|
|
boot.loader.grub.efiSupport = cfg.efi;
|
|
|
|
boot.loader.grub.efiInstallAsRemovable = cfg.efi;
|
2021-08-22 07:53:02 +00:00
|
|
|
boot.loader.timeout = 1;
|
|
|
|
boot.loader.grub.extraConfig = ''
|
|
|
|
serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
|
|
|
|
terminal_output console serial
|
|
|
|
terminal_input console serial
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-11-27 09:42:12 +00:00
|
|
|
systemd.services.fetch-ec2-metadata = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = ["network-online.target"];
|
|
|
|
path = [ pkgs.curl ];
|
|
|
|
script = builtins.readFile ./ec2-metadata-fetcher.sh;
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
serviceConfig.StandardOutput = "journal+console";
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Allow root logins only using the SSH key that the user specified
|
|
|
|
# at instance creation time.
|
|
|
|
services.openssh.enable = true;
|
2023-01-20 10:41:00 +00:00
|
|
|
services.openssh.settings.PermitRootLogin = "prohibit-password";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-08-22 07:53:02 +00:00
|
|
|
# Enable the serial console on ttyS0
|
|
|
|
systemd.services."serial-getty@ttyS0".enable = true;
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# Creates symlinks for block device names.
|
2022-02-10 20:34:41 +00:00
|
|
|
services.udev.packages = [ pkgs.amazon-ec2-utils ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Force getting the hostname from EC2.
|
|
|
|
networking.hostName = mkDefault "";
|
|
|
|
|
|
|
|
# Always include cryptsetup so that Charon can use it.
|
|
|
|
environment.systemPackages = [ pkgs.cryptsetup ];
|
|
|
|
|
|
|
|
# EC2 has its own NTP server provided by the hypervisor
|
|
|
|
networking.timeServers = [ "169.254.169.123" ];
|
|
|
|
|
|
|
|
# udisks has become too bloated to have in a headless system
|
|
|
|
# (e.g. it depends on GTK).
|
|
|
|
services.udisks2.enable = false;
|
|
|
|
};
|
|
|
|
}
|