2015-09-27 20:01:43 +01: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.)
|
|
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
2010-01-20 18:10:02 +00:00
|
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
|
with lib;
|
2014-05-21 09:55:34 +01:00
|
|
|
|
|
2019-02-06 11:16:22 +00:00
|
|
|
|
let
|
|
|
|
|
cfg = config.ec2;
|
|
|
|
|
metadataFetcher = import ./ec2-metadata-fetcher.nix {
|
2020-11-18 16:56:15 +00:00
|
|
|
|
inherit (pkgs) curl;
|
2019-02-06 11:16:22 +00:00
|
|
|
|
targetRoot = "$targetRoot/";
|
|
|
|
|
wgetExtraOptions = "-q";
|
|
|
|
|
};
|
|
|
|
|
in
|
2014-05-21 09:55:34 +01:00
|
|
|
|
|
2015-09-27 20:01:43 +01:00
|
|
|
|
{
|
2021-08-20 17:36:54 +01: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
|
|
|
|
|
];
|
2014-05-21 09:55:34 +01:00
|
|
|
|
|
2015-09-27 20:01:43 +01:00
|
|
|
|
config = {
|
2014-05-21 09:55:34 +01:00
|
|
|
|
|
2017-04-04 12:07:15 +01:00
|
|
|
|
assertions = [
|
|
|
|
|
{ assertion = cfg.hvm;
|
|
|
|
|
message = "Paravirtualized EC2 instances are no longer supported.";
|
|
|
|
|
}
|
2019-05-25 10:53:15 +01:00
|
|
|
|
{ assertion = cfg.efi -> cfg.hvm;
|
|
|
|
|
message = "EC2 instances using EFI must be HVM instances.";
|
|
|
|
|
}
|
2022-02-10 07:56:58 +00:00
|
|
|
|
{ assertion = versionOlder config.boot.kernelPackages.kernel.version "5.15";
|
|
|
|
|
message = "ENA driver fails to build with kernel >= 5.15";
|
|
|
|
|
}
|
2017-04-04 12:07:15 +01:00
|
|
|
|
];
|
|
|
|
|
|
2022-02-10 07:56:58 +00:00
|
|
|
|
boot.kernelPackages = pkgs.linuxKernel.packages.linux_5_10;
|
|
|
|
|
|
2018-01-06 13:52:51 +00:00
|
|
|
|
boot.growPartition = cfg.hvm;
|
2016-02-17 12:02:59 +00:00
|
|
|
|
|
2021-08-25 14:38:46 +01:00
|
|
|
|
fileSystems."/" = mkIf (!cfg.zfs.enable) {
|
2015-09-27 20:01:43 +01:00
|
|
|
|
device = "/dev/disk/by-label/nixos";
|
2019-03-14 09:30:20 +00:00
|
|
|
|
fsType = "ext4";
|
2015-09-27 20:01:43 +01:00
|
|
|
|
autoResize = true;
|
|
|
|
|
};
|
2014-05-21 09:55:34 +01:00
|
|
|
|
|
2021-08-25 14:38:46 +01: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.
|
2019-05-25 10:53:15 +01:00
|
|
|
|
device = "/dev/disk/by-label/ESP";
|
|
|
|
|
fsType = "vfat";
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-25 14:38:46 +01:00
|
|
|
|
services.zfs.expandOnBoot = mkIf cfg.zfs.enable "all";
|
|
|
|
|
|
|
|
|
|
boot.zfs.devNodes = mkIf cfg.zfs.enable "/dev/";
|
|
|
|
|
|
2019-08-15 00:58:22 +01:00
|
|
|
|
boot.extraModulePackages = [
|
|
|
|
|
config.boot.kernelPackages.ena
|
|
|
|
|
];
|
2016-07-11 11:15:39 +01:00
|
|
|
|
boot.initrd.kernelModules = [ "xen-blkfront" "xen-netfront" ];
|
2017-11-09 16:47:29 +00:00
|
|
|
|
boot.initrd.availableKernelModules = [ "ixgbevf" "ena" "nvme" ];
|
2021-08-20 16:15:31 +01:00
|
|
|
|
boot.kernelParams = mkIf cfg.hvm [ "console=ttyS0,115200n8" "random.trust_cpu=on" ];
|
2014-05-21 09:55:34 +01:00
|
|
|
|
|
2015-02-17 22:18:11 +00:00
|
|
|
|
# Prevent the nouveau kernel module from being loaded, as it
|
|
|
|
|
# interferes with the nvidia/nvidia-uvm modules needed for CUDA.
|
2015-09-28 20:57:54 +01:00
|
|
|
|
# Also blacklist xen_fbfront to prevent a 30 second delay during
|
|
|
|
|
# boot.
|
|
|
|
|
boot.blacklistedKernelModules = [ "nouveau" "xen_fbfront" ];
|
2015-02-17 22:18:11 +00:00
|
|
|
|
|
2014-05-21 09:55:34 +01:00
|
|
|
|
# Generate a GRUB menu. Amazon's pv-grub uses this to boot our kernel/initrd.
|
2014-06-19 09:56:52 +01:00
|
|
|
|
boot.loader.grub.version = if cfg.hvm then 2 else 1;
|
2019-05-25 10:53:15 +01:00
|
|
|
|
boot.loader.grub.device = if (cfg.hvm && !cfg.efi) then "/dev/xvda" else "nodev";
|
2015-09-28 12:34:19 +01:00
|
|
|
|
boot.loader.grub.extraPerEntryConfig = mkIf (!cfg.hvm) "root (hd0)";
|
2019-05-25 10:53:15 +01:00
|
|
|
|
boot.loader.grub.efiSupport = cfg.efi;
|
|
|
|
|
boot.loader.grub.efiInstallAsRemovable = cfg.efi;
|
2021-08-20 16:20:30 +01:00
|
|
|
|
boot.loader.timeout = 1;
|
2021-08-20 16:22:07 +01:00
|
|
|
|
boot.loader.grub.extraConfig = ''
|
|
|
|
|
serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
|
|
|
|
|
terminal_output console serial
|
|
|
|
|
terminal_input console serial
|
|
|
|
|
'';
|
2014-05-21 09:55:34 +01:00
|
|
|
|
|
2016-02-02 18:03:13 +00:00
|
|
|
|
boot.initrd.network.enable = true;
|
|
|
|
|
|
2014-05-21 09:55:34 +01:00
|
|
|
|
# Mount all formatted ephemeral disks and activate all swap devices.
|
|
|
|
|
# We cannot do this with the ‘fileSystems’ and ‘swapDevices’ options
|
|
|
|
|
# because the set of devices is dependent on the instance type
|
2018-06-03 18:03:34 +01:00
|
|
|
|
# (e.g. "m1.small" has one ephemeral filesystem and one swap device,
|
2014-05-21 09:55:34 +01:00
|
|
|
|
# while "m1.large" has two ephemeral filesystems and no swap
|
|
|
|
|
# devices). Also, put /tmp and /var on /disk0, since it has a lot
|
|
|
|
|
# more space than the root device. Similarly, "move" /nix to /disk0
|
|
|
|
|
# by layering a unionfs-fuse mount on top of it so we have a lot more space for
|
|
|
|
|
# Nix operations.
|
|
|
|
|
boot.initrd.postMountCommands =
|
|
|
|
|
''
|
2019-02-06 11:16:22 +00:00
|
|
|
|
${metadataFetcher}
|
2016-02-04 14:42:49 +00:00
|
|
|
|
|
2014-05-21 09:55:34 +01:00
|
|
|
|
diskNr=0
|
|
|
|
|
diskForUnionfs=
|
|
|
|
|
for device in /dev/xvd[abcde]*; do
|
|
|
|
|
if [ "$device" = /dev/xvda -o "$device" = /dev/xvda1 ]; then continue; fi
|
|
|
|
|
fsType=$(blkid -o value -s TYPE "$device" || true)
|
|
|
|
|
if [ "$fsType" = swap ]; then
|
|
|
|
|
echo "activating swap device $device..."
|
|
|
|
|
swapon "$device" || true
|
|
|
|
|
elif [ "$fsType" = ext3 ]; then
|
|
|
|
|
mp="/disk$diskNr"
|
|
|
|
|
diskNr=$((diskNr + 1))
|
|
|
|
|
if mountFS "$device" "$mp" "" ext3; then
|
2016-02-02 18:10:00 +00:00
|
|
|
|
if [ -z "$diskForUnionfs" ]; then diskForUnionfs="$mp"; fi
|
2014-05-21 09:55:34 +01:00
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
echo "skipping unknown device type $device"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
if [ -n "$diskForUnionfs" ]; then
|
|
|
|
|
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root
|
|
|
|
|
|
|
|
|
|
mkdir -m 1777 -p $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
|
|
|
|
|
mount --bind $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
|
|
|
|
|
|
2016-02-02 15:17:20 +00:00
|
|
|
|
if [ "$(cat "$metaDir/ami-manifest-path")" != "(unknown)" ]; then
|
2014-05-21 09:55:34 +01:00
|
|
|
|
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/var $targetRoot/var
|
|
|
|
|
mount --bind $targetRoot/$diskForUnionfs/root/var $targetRoot/var
|
|
|
|
|
|
|
|
|
|
mkdir -p /unionfs-chroot/ro-nix
|
|
|
|
|
mount --rbind $targetRoot/nix /unionfs-chroot/ro-nix
|
|
|
|
|
|
|
|
|
|
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/nix
|
|
|
|
|
mkdir -p /unionfs-chroot/rw-nix
|
|
|
|
|
mount --rbind $targetRoot/$diskForUnionfs/root/nix /unionfs-chroot/rw-nix
|
|
|
|
|
|
|
|
|
|
unionfs -o allow_other,cow,nonempty,chroot=/unionfs-chroot,max_files=32768 /rw-nix=RW:/ro-nix=RO $targetRoot/nix
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
boot.initrd.extraUtilsCommands =
|
|
|
|
|
''
|
|
|
|
|
# We need swapon in the initrd.
|
2020-11-24 15:29:28 +00:00
|
|
|
|
copy_bin_and_libs ${pkgs.util-linux}/sbin/swapon
|
2014-05-21 09:55:34 +01:00
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
# Allow root logins only using the SSH key that the user specified
|
|
|
|
|
# at instance creation time.
|
|
|
|
|
services.openssh.enable = true;
|
2016-10-01 18:23:56 +01:00
|
|
|
|
services.openssh.permitRootLogin = "prohibit-password";
|
2014-05-21 09:55:34 +01:00
|
|
|
|
|
2021-08-20 17:36:54 +01:00
|
|
|
|
# Enable the serial console on ttyS0
|
|
|
|
|
systemd.services."serial-getty@ttyS0".enable = true;
|
|
|
|
|
|
2019-08-23 13:12:33 +01:00
|
|
|
|
# Creates symlinks for block device names.
|
2022-02-01 21:59:59 +00:00
|
|
|
|
services.udev.packages = [ pkgs.amazon-ec2-utils ];
|
2019-08-23 13:12:33 +01:00
|
|
|
|
|
2014-05-21 09:55:34 +01:00
|
|
|
|
# Force getting the hostname from EC2.
|
|
|
|
|
networking.hostName = mkDefault "";
|
|
|
|
|
|
|
|
|
|
# Always include cryptsetup so that Charon can use it.
|
|
|
|
|
environment.systemPackages = [ pkgs.cryptsetup ];
|
|
|
|
|
|
|
|
|
|
boot.initrd.supportedFilesystems = [ "unionfs-fuse" ];
|
2018-10-15 20:47:51 +01:00
|
|
|
|
|
2017-11-30 03:48:00 +00:00
|
|
|
|
# EC2 has its own NTP server provided by the hypervisor
|
|
|
|
|
networking.timeServers = [ "169.254.169.123" ];
|
2018-10-15 20:47:51 +01:00
|
|
|
|
|
|
|
|
|
# udisks has become too bloated to have in a headless system
|
2019-09-03 23:49:40 +01:00
|
|
|
|
# (e.g. it depends on GTK).
|
2018-10-15 20:47:51 +01:00
|
|
|
|
services.udisks2.enable = false;
|
2014-05-21 09:55:34 +01:00
|
|
|
|
};
|
2010-01-20 18:10:02 +00:00
|
|
|
|
}
|