2010-01-20 18:10:02 +00:00
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
with pkgs.lib;
|
|
|
|
|
|
|
|
|
|
{
|
2011-11-02 16:10:30 +00:00
|
|
|
|
require = [ ../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
|
2010-07-13 21:47:31 +01:00
|
|
|
|
${pkgs.vmTools.kvm}/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.
|
2010-01-22 17:31:23 +00:00
|
|
|
|
${pkgs.e2fsprogs}/sbin/mkfs.ext3 -L nixos /dev/vda
|
|
|
|
|
${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 | \
|
2010-01-22 17:31:23 +00:00
|
|
|
|
chroot /mnt ${config.environment.nix}/bin/nix-store --load-db
|
|
|
|
|
|
|
|
|
|
# Create the system profile to allow nixos-rebuild to work.
|
|
|
|
|
chroot /mnt ${config.environment.nix}/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
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
''
|
|
|
|
|
);
|
|
|
|
|
|
2010-01-22 17:31:23 +00:00
|
|
|
|
fileSystems =
|
|
|
|
|
[ { mountPoint = "/";
|
|
|
|
|
device = "/dev/disk/by-label/nixos";
|
|
|
|
|
}
|
|
|
|
|
];
|
2010-01-21 08:03:58 +00:00
|
|
|
|
|
2010-08-29 23:29:10 +01:00
|
|
|
|
boot.initrd.kernelModules = [ "xen-blkfront" "aufs" ];
|
2010-07-20 14:53:14 +01:00
|
|
|
|
boot.kernelModules = [ "xen-netfront" ];
|
2010-01-21 08:03:58 +00:00
|
|
|
|
|
2011-11-08 15:12:11 +00:00
|
|
|
|
boot.extraModulePackages = [ config.boot.kernelPackages.aufs ];
|
2010-08-29 23:29:10 +01:00
|
|
|
|
|
2010-07-22 15:40:29 +01:00
|
|
|
|
# Generate a GRUB menu. Amazon's pv-grub uses this to boot our kernel/initrd.
|
|
|
|
|
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
|
|
|
|
|
kill -9 -- -1
|
|
|
|
|
'';
|
|
|
|
|
|
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
|
|
|
|
|
# by layering an AUFS on top of it so we have a lot more space for
|
|
|
|
|
# Nix operations.
|
2010-08-29 23:09:15 +01:00
|
|
|
|
boot.initrd.postMountCommands =
|
|
|
|
|
''
|
2011-11-04 18:49:59 +00:00
|
|
|
|
diskNr=0
|
|
|
|
|
diskForAufs=
|
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
|
|
|
|
|
if [ -z "$diskForAufs" ]; then diskForAufs="$mp"; fi
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
echo "skipping unknown device type $device"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
if [ -n "$diskForAufs" ]; then
|
|
|
|
|
mkdir -m 755 -p $targetRoot/$diskForAufs/root
|
|
|
|
|
|
|
|
|
|
mkdir -m 1777 -p $targetRoot/$diskForAufs/root/tmp $targetRoot/tmp
|
|
|
|
|
mount --bind $targetRoot/$diskForAufs/root/tmp $targetRoot/tmp
|
|
|
|
|
|
2012-04-10 12:11:05 +01:00
|
|
|
|
if [ ! -e $targetRoot/.ebs ]; then
|
|
|
|
|
mkdir -m 755 -p $targetRoot/$diskForAufs/root/var $targetRoot/var
|
|
|
|
|
mount --bind $targetRoot/$diskForAufs/root/var $targetRoot/var
|
2011-11-04 18:49:59 +00:00
|
|
|
|
|
2012-04-10 12:11:05 +01:00
|
|
|
|
mkdir -m 755 -p $targetRoot/$diskForAufs/root/nix
|
|
|
|
|
mount -t aufs -o dirs=$targetRoot/$diskForAufs/root/nix=rw:$targetRoot/nix=rr none $targetRoot/nix
|
|
|
|
|
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
|
|
|
|
'';
|
|
|
|
|
|
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 ];
|
2010-01-20 18:10:02 +00:00
|
|
|
|
}
|