at 16.09-beta 5.9 kB view raw
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.) 6 7{ config, lib, pkgs, ... }: 8 9with lib; 10 11let cfg = config.ec2; in 12 13{ 14 imports = [ ../profiles/headless.nix ./ec2-data.nix ./grow-partition.nix ./amazon-init.nix ]; 15 16 config = { 17 18 virtualisation.growPartition = cfg.hvm; 19 20 fileSystems."/" = { 21 device = "/dev/disk/by-label/nixos"; 22 autoResize = true; 23 }; 24 25 boot.extraModulePackages = 26 [ config.boot.kernelPackages.ixgbevf 27 config.boot.kernelPackages.ena 28 ]; 29 boot.initrd.kernelModules = [ "xen-blkfront" "xen-netfront" ]; 30 boot.initrd.availableKernelModules = [ "ixgbevf" "ena" ]; 31 boot.kernelParams = mkIf cfg.hvm [ "console=ttyS0" ]; 32 33 # Prevent the nouveau kernel module from being loaded, as it 34 # interferes with the nvidia/nvidia-uvm modules needed for CUDA. 35 # Also blacklist xen_fbfront to prevent a 30 second delay during 36 # boot. 37 boot.blacklistedKernelModules = [ "nouveau" "xen_fbfront" ]; 38 39 # Generate a GRUB menu. Amazon's pv-grub uses this to boot our kernel/initrd. 40 boot.loader.grub.version = if cfg.hvm then 2 else 1; 41 boot.loader.grub.device = if cfg.hvm then "/dev/xvda" else "nodev"; 42 boot.loader.grub.extraPerEntryConfig = mkIf (!cfg.hvm) "root (hd0)"; 43 boot.loader.timeout = 0; 44 45 boot.initrd.postDeviceCommands = 46 '' 47 # Force udev to exit to prevent random "Device or resource busy 48 # while trying to open /dev/xvda" errors from fsck. 49 udevadm control --exit || true 50 ''; 51 52 boot.initrd.network.enable = true; 53 54 # Mount all formatted ephemeral disks and activate all swap devices. 55 # We cannot do this with the ‘fileSystems’ and ‘swapDevices’ options 56 # because the set of devices is dependent on the instance type 57 # (e.g. "m1.large" has one ephemeral filesystem and one swap device, 58 # while "m1.large" has two ephemeral filesystems and no swap 59 # devices). Also, put /tmp and /var on /disk0, since it has a lot 60 # more space than the root device. Similarly, "move" /nix to /disk0 61 # by layering a unionfs-fuse mount on top of it so we have a lot more space for 62 # Nix operations. 63 boot.initrd.postMountCommands = 64 '' 65 metaDir=$targetRoot/etc/ec2-metadata 66 mkdir -m 0755 -p "$metaDir" 67 68 echo "getting EC2 instance metadata..." 69 70 if ! [ -e "$metaDir/ami-manifest-path" ]; then 71 wget -q -O "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path 72 fi 73 74 if ! [ -e "$metaDir/user-data" ]; then 75 wget -q -O "$metaDir/user-data" http://169.254.169.254/1.0/user-data && chmod 600 "$metaDir/user-data" 76 fi 77 78 if ! [ -e "$metaDir/hostname" ]; then 79 wget -q -O "$metaDir/hostname" http://169.254.169.254/1.0/meta-data/hostname 80 fi 81 82 if ! [ -e "$metaDir/public-keys-0-openssh-key" ]; then 83 wget -q -O "$metaDir/public-keys-0-openssh-key" http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key 84 fi 85 86 diskNr=0 87 diskForUnionfs= 88 for device in /dev/xvd[abcde]*; do 89 if [ "$device" = /dev/xvda -o "$device" = /dev/xvda1 ]; then continue; fi 90 fsType=$(blkid -o value -s TYPE "$device" || true) 91 if [ "$fsType" = swap ]; then 92 echo "activating swap device $device..." 93 swapon "$device" || true 94 elif [ "$fsType" = ext3 ]; then 95 mp="/disk$diskNr" 96 diskNr=$((diskNr + 1)) 97 echo "mounting $device on $mp..." 98 if mountFS "$device" "$mp" "" ext3; then 99 if [ -z "$diskForUnionfs" ]; then diskForUnionfs="$mp"; fi 100 fi 101 else 102 echo "skipping unknown device type $device" 103 fi 104 done 105 106 if [ -n "$diskForUnionfs" ]; then 107 mkdir -m 755 -p $targetRoot/$diskForUnionfs/root 108 109 mkdir -m 1777 -p $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp 110 mount --bind $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp 111 112 if [ "$(cat "$metaDir/ami-manifest-path")" != "(unknown)" ]; then 113 mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/var $targetRoot/var 114 mount --bind $targetRoot/$diskForUnionfs/root/var $targetRoot/var 115 116 mkdir -p /unionfs-chroot/ro-nix 117 mount --rbind $targetRoot/nix /unionfs-chroot/ro-nix 118 119 mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/nix 120 mkdir -p /unionfs-chroot/rw-nix 121 mount --rbind $targetRoot/$diskForUnionfs/root/nix /unionfs-chroot/rw-nix 122 123 unionfs -o allow_other,cow,nonempty,chroot=/unionfs-chroot,max_files=32768 /rw-nix=RW:/ro-nix=RO $targetRoot/nix 124 fi 125 fi 126 ''; 127 128 boot.initrd.extraUtilsCommands = 129 '' 130 # We need swapon in the initrd. 131 copy_bin_and_libs ${pkgs.utillinux}/sbin/swapon 132 ''; 133 134 # Don't put old configurations in the GRUB menu. The user has no 135 # way to select them anyway. 136 boot.loader.grub.configurationLimit = 0; 137 138 # Allow root logins only using the SSH key that the user specified 139 # at instance creation time. 140 services.openssh.enable = true; 141 services.openssh.permitRootLogin = "without-password"; 142 143 # Force getting the hostname from EC2. 144 networking.hostName = mkDefault ""; 145 146 # Always include cryptsetup so that Charon can use it. 147 environment.systemPackages = [ pkgs.cryptsetup ]; 148 149 boot.initrd.supportedFilesystems = [ "unionfs-fuse" ]; 150 }; 151}