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;
|
2010-01-20 18:10:02 +00:00
|
|
|
|
|
|
|
|
|
{
|
2013-09-04 12:05:09 +01:00
|
|
|
|
imports = [ ../profiles/headless.nix ./ec2-data.nix ];
|
2011-04-06 16:09:34 +01:00
|
|
|
|
|
2010-01-21 08:03:58 +00:00
|
|
|
|
system.build.amazonImage =
|
2010-01-20 18:10:02 +00:00
|
|
|
|
pkgs.vmTools.runInLinuxVM (
|
|
|
|
|
pkgs.runCommand "amazon-image"
|
|
|
|
|
{ preVM =
|
|
|
|
|
''
|
|
|
|
|
mkdir $out
|
|
|
|
|
diskImage=$out/nixos.img
|
2013-07-31 13:53:27 +01:00
|
|
|
|
${pkgs.vmTools.qemu}/bin/qemu-img create -f raw $diskImage "4G"
|
2011-10-22 00:39:05 +01:00
|
|
|
|
mv closure xchg/
|
2010-01-20 18:10:02 +00:00
|
|
|
|
'';
|
2010-01-21 08:03:58 +00:00
|
|
|
|
buildInputs = [ pkgs.utillinux pkgs.perl ];
|
2011-09-14 19:20:50 +01:00
|
|
|
|
exportReferencesGraph =
|
2010-01-20 18:10:02 +00:00
|
|
|
|
[ "closure" config.system.build.toplevel ];
|
|
|
|
|
}
|
|
|
|
|
''
|
2010-08-27 16:31:04 +01:00
|
|
|
|
# Create an empty filesystem and mount it.
|
2013-01-24 17:14:13 +00:00
|
|
|
|
${pkgs.e2fsprogs}/sbin/mkfs.ext4 -L nixos /dev/vda
|
2010-01-22 17:31:23 +00:00
|
|
|
|
${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda
|
2010-01-20 18:10:02 +00:00
|
|
|
|
mkdir /mnt
|
|
|
|
|
mount /dev/vda /mnt
|
|
|
|
|
|
2010-01-22 20:22:40 +00:00
|
|
|
|
# The initrd expects these directories to exist.
|
|
|
|
|
mkdir /mnt/dev /mnt/proc /mnt/sys
|
2011-09-14 19:20:50 +01:00
|
|
|
|
|
2011-07-29 15:55:42 +01:00
|
|
|
|
mount -o bind /proc /mnt/proc
|
2010-01-22 20:22:40 +00:00
|
|
|
|
|
2010-01-20 18:10:02 +00:00
|
|
|
|
# Copy all paths in the closure to the filesystem.
|
2011-10-22 00:39:05 +01:00
|
|
|
|
storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure)
|
2010-01-20 18:10:02 +00:00
|
|
|
|
|
2011-10-30 15:19:58 +00:00
|
|
|
|
mkdir -p /mnt/nix/store
|
2011-10-22 00:39:05 +01:00
|
|
|
|
echo "copying everything (will take a while)..."
|
2011-10-30 15:19:58 +00:00
|
|
|
|
cp -prd $storePaths /mnt/nix/store/
|
2010-01-20 18:10:02 +00:00
|
|
|
|
|
2010-01-22 17:31:23 +00:00
|
|
|
|
# Register the paths in the Nix database.
|
2011-10-22 00:39:05 +01:00
|
|
|
|
printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \
|
2013-10-28 15:28:04 +00:00
|
|
|
|
chroot /mnt ${config.nix.package}/bin/nix-store --load-db
|
2010-01-22 17:31:23 +00:00
|
|
|
|
|
|
|
|
|
# Create the system profile to allow nixos-rebuild to work.
|
2013-10-28 15:28:04 +00:00
|
|
|
|
chroot /mnt ${config.nix.package}/bin/nix-env \
|
2011-10-30 15:19:58 +00:00
|
|
|
|
-p /nix/var/nix/profiles/system --set ${config.system.build.toplevel}
|
2010-01-22 17:31:23 +00:00
|
|
|
|
|
|
|
|
|
# `nixos-rebuild' requires an /etc/NIXOS.
|
|
|
|
|
mkdir -p /mnt/etc
|
|
|
|
|
touch /mnt/etc/NIXOS
|
2010-07-15 12:09:30 +01:00
|
|
|
|
|
2012-12-18 18:44:47 +00:00
|
|
|
|
# `switch-to-configuration' requires a /bin/sh
|
|
|
|
|
mkdir -p /mnt/bin
|
|
|
|
|
ln -s ${config.system.build.binsh}/bin/sh /mnt/bin/sh
|
|
|
|
|
|
2010-07-15 12:09:30 +01:00
|
|
|
|
# Install a configuration.nix.
|
|
|
|
|
mkdir -p /mnt/etc/nixos
|
|
|
|
|
cp ${./amazon-config.nix} /mnt/etc/nixos/configuration.nix
|
2010-07-20 14:53:14 +01:00
|
|
|
|
|
2010-07-22 15:40:29 +01:00
|
|
|
|
# Generate the GRUB menu.
|
|
|
|
|
chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot
|
2010-01-20 18:10:02 +00:00
|
|
|
|
|
2011-07-29 15:55:42 +01:00
|
|
|
|
umount /mnt/proc
|
2010-01-20 18:10:02 +00:00
|
|
|
|
umount /mnt
|
|
|
|
|
''
|
|
|
|
|
);
|
|
|
|
|
|
2012-11-02 17:02:12 +00:00
|
|
|
|
fileSystems."/".device = "/dev/disk/by-label/nixos";
|
2010-01-21 08:03:58 +00:00
|
|
|
|
|
2012-12-16 18:16:17 +00:00
|
|
|
|
boot.initrd.kernelModules = [ "xen-blkfront" ];
|
2010-07-20 14:53:14 +01:00
|
|
|
|
boot.kernelModules = [ "xen-netfront" ];
|
2010-01-21 08:03:58 +00:00
|
|
|
|
|
2010-07-22 15:40:29 +01:00
|
|
|
|
# Generate a GRUB menu. Amazon's pv-grub uses this to boot our kernel/initrd.
|
2013-10-07 10:13:58 +01:00
|
|
|
|
boot.loader.grub.version = 1;
|
2010-07-22 15:40:29 +01:00
|
|
|
|
boot.loader.grub.device = "nodev";
|
|
|
|
|
boot.loader.grub.timeout = 0;
|
|
|
|
|
boot.loader.grub.extraPerEntryConfig = "root (hd0)";
|
|
|
|
|
|
2012-04-12 19:01:19 +01:00
|
|
|
|
boot.initrd.postDeviceCommands =
|
|
|
|
|
''
|
|
|
|
|
# Force udev to exit to prevent random "Device or resource busy
|
|
|
|
|
# while trying to open /dev/xvda" errors from fsck.
|
|
|
|
|
udevadm control --exit || true
|
2012-06-25 00:02:34 +01:00
|
|
|
|
kill -9 -1
|
2012-04-12 19:01:19 +01:00
|
|
|
|
'';
|
2013-09-04 12:05:09 +01:00
|
|
|
|
|
2011-11-04 18:49:59 +00: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
|
|
|
|
|
# (e.g. "m1.large" has one ephemeral filesystem and one swap device,
|
|
|
|
|
# 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
|
2012-12-16 18:16:17 +00:00
|
|
|
|
# by layering a unionfs-fuse mount on top of it so we have a lot more space for
|
2011-11-04 18:49:59 +00:00
|
|
|
|
# Nix operations.
|
2010-08-29 23:09:15 +01:00
|
|
|
|
boot.initrd.postMountCommands =
|
|
|
|
|
''
|
2011-11-04 18:49:59 +00:00
|
|
|
|
diskNr=0
|
2012-12-16 18:16:17 +00:00
|
|
|
|
diskForUnionfs=
|
2012-04-12 17:48:01 +01:00
|
|
|
|
for device in /dev/xvd[abcde]*; do
|
2012-04-06 13:23:12 +01:00
|
|
|
|
if [ "$device" = /dev/xvda -o "$device" = /dev/xvda1 ]; then continue; fi
|
2011-11-04 18:49:59 +00:00
|
|
|
|
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))
|
2012-02-26 00:12:04 +00:00
|
|
|
|
echo "mounting $device on $mp..."
|
2011-11-04 18:49:59 +00:00
|
|
|
|
if mountFS "$device" "$mp" "" ext3; then
|
2012-12-16 18:16:17 +00:00
|
|
|
|
if [ -z "$diskForUnionfs" ]; then diskForUnionfs="$mp"; fi
|
2011-11-04 18:49:59 +00:00
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
echo "skipping unknown device type $device"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
2012-12-16 18:16:17 +00:00
|
|
|
|
if [ -n "$diskForUnionfs" ]; then
|
|
|
|
|
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root
|
2011-11-04 18:49:59 +00:00
|
|
|
|
|
2012-12-16 18:16:17 +00:00
|
|
|
|
mkdir -m 1777 -p $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
|
|
|
|
|
mount --bind $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
|
2011-11-04 18:49:59 +00:00
|
|
|
|
|
2012-04-10 12:11:05 +01:00
|
|
|
|
if [ ! -e $targetRoot/.ebs ]; then
|
2012-12-16 18:16:17 +00: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
|
2011-11-04 18:49:59 +00:00
|
|
|
|
|
2012-12-16 18:16:17 +00:00
|
|
|
|
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/nix
|
|
|
|
|
mkdir -p /unionfs-chroot/rw-nix
|
|
|
|
|
mount --rbind $targetRoot/$diskForUnionfs/root/nix /unionfs-chroot/rw-nix
|
|
|
|
|
|
2013-01-22 12:31:08 +00:00
|
|
|
|
unionfs -o allow_other,cow,nonempty,chroot=/unionfs-chroot,max_files=32768 /rw-nix=RW:/ro-nix=RO $targetRoot/nix
|
2012-04-10 12:11:05 +01:00
|
|
|
|
fi
|
2011-11-04 18:49:59 +00:00
|
|
|
|
fi
|
|
|
|
|
'';
|
2010-08-29 23:29:10 +01:00
|
|
|
|
|
2011-11-04 18:49:59 +00:00
|
|
|
|
boot.initrd.extraUtilsCommands =
|
|
|
|
|
''
|
|
|
|
|
# We need swapon in the initrd.
|
|
|
|
|
cp ${pkgs.utillinux}/sbin/swapon $out/bin
|
2010-08-29 23:09:15 +01:00
|
|
|
|
'';
|
|
|
|
|
|
2012-07-25 21:38:05 +01:00
|
|
|
|
# Don't put old configurations in the GRUB menu. The user has no
|
|
|
|
|
# way to select them anyway.
|
|
|
|
|
boot.loader.grub.configurationLimit = 0;
|
|
|
|
|
|
2010-01-21 08:03:58 +00:00
|
|
|
|
# Allow root logins only using the SSH key that the user specified
|
|
|
|
|
# at instance creation time.
|
2010-03-11 17:02:53 +00:00
|
|
|
|
services.openssh.enable = true;
|
|
|
|
|
services.openssh.permitRootLogin = "without-password";
|
2011-11-04 20:06:59 +00:00
|
|
|
|
|
|
|
|
|
# Force getting the hostname from EC2.
|
|
|
|
|
networking.hostName = mkDefault "";
|
2012-05-15 00:43:38 +01:00
|
|
|
|
|
|
|
|
|
# Always include cryptsetup so that Charon can use it.
|
|
|
|
|
environment.systemPackages = [ pkgs.cryptsetup ];
|
2012-12-16 18:16:17 +00:00
|
|
|
|
|
|
|
|
|
boot.initrd.supportedFilesystems = [ "unionfs-fuse" ];
|
2013-11-01 13:45:56 +00:00
|
|
|
|
|
|
|
|
|
# Prevent logging in as root without a password. This doesn't really matter,
|
|
|
|
|
# since the only PAM services that allow logging in with a null
|
|
|
|
|
# password are local ones that are inaccessible on EC2 machines.
|
2014-02-24 17:05:26 +00:00
|
|
|
|
security.initialRootPassword = mkDefault "!";
|
2010-01-20 18:10:02 +00:00
|
|
|
|
}
|