2010-01-20 19:10:02 +01:00
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
with pkgs.lib;
|
|
|
|
|
|
|
|
|
|
{
|
2011-11-02 17:10:30 +01:00
|
|
|
|
require = [ ../profiles/headless.nix ./ec2-data.nix ];
|
2011-04-06 17:09:34 +02:00
|
|
|
|
|
2010-01-21 09:03:58 +01:00
|
|
|
|
system.build.amazonImage =
|
2010-01-20 19:10:02 +01:00
|
|
|
|
pkgs.vmTools.runInLinuxVM (
|
|
|
|
|
pkgs.runCommand "amazon-image"
|
|
|
|
|
{ preVM =
|
|
|
|
|
''
|
|
|
|
|
mkdir $out
|
|
|
|
|
diskImage=$out/nixos.img
|
2010-07-13 22:47:31 +02:00
|
|
|
|
${pkgs.vmTools.kvm}/bin/qemu-img create -f raw $diskImage "4G"
|
2011-10-22 01:39:05 +02:00
|
|
|
|
mv closure xchg/
|
2010-01-20 19:10:02 +01:00
|
|
|
|
'';
|
2010-01-21 09:03:58 +01:00
|
|
|
|
buildInputs = [ pkgs.utillinux pkgs.perl ];
|
2011-09-14 20:20:50 +02:00
|
|
|
|
exportReferencesGraph =
|
2010-01-20 19:10:02 +01:00
|
|
|
|
[ "closure" config.system.build.toplevel ];
|
|
|
|
|
}
|
|
|
|
|
''
|
2010-08-27 17:31:04 +02:00
|
|
|
|
# Create an empty filesystem and mount it.
|
2010-01-22 18:31:23 +01:00
|
|
|
|
${pkgs.e2fsprogs}/sbin/mkfs.ext3 -L nixos /dev/vda
|
|
|
|
|
${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda
|
2010-01-20 19:10:02 +01:00
|
|
|
|
mkdir /mnt
|
|
|
|
|
mount /dev/vda /mnt
|
|
|
|
|
|
2010-01-22 21:22:40 +01:00
|
|
|
|
# The initrd expects these directories to exist.
|
|
|
|
|
mkdir /mnt/dev /mnt/proc /mnt/sys
|
2011-09-14 20:20:50 +02:00
|
|
|
|
|
2011-07-29 16:55:42 +02:00
|
|
|
|
mount -o bind /proc /mnt/proc
|
2010-01-22 21:22:40 +01:00
|
|
|
|
|
2010-01-20 19:10:02 +01:00
|
|
|
|
# Copy all paths in the closure to the filesystem.
|
2011-10-22 01:39:05 +02:00
|
|
|
|
storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure)
|
2010-01-20 19:10:02 +01:00
|
|
|
|
|
2011-10-30 16:19:58 +01:00
|
|
|
|
mkdir -p /mnt/nix/store
|
2011-10-22 01:39:05 +02:00
|
|
|
|
echo "copying everything (will take a while)..."
|
2011-10-30 16:19:58 +01:00
|
|
|
|
cp -prd $storePaths /mnt/nix/store/
|
2010-01-20 19:10:02 +01:00
|
|
|
|
|
2010-01-22 18:31:23 +01:00
|
|
|
|
# Register the paths in the Nix database.
|
2011-10-22 01:39:05 +02:00
|
|
|
|
printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \
|
2010-01-22 18:31:23 +01: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 16:19:58 +01:00
|
|
|
|
-p /nix/var/nix/profiles/system --set ${config.system.build.toplevel}
|
2010-01-22 18:31:23 +01:00
|
|
|
|
|
|
|
|
|
# `nixos-rebuild' requires an /etc/NIXOS.
|
|
|
|
|
mkdir -p /mnt/etc
|
|
|
|
|
touch /mnt/etc/NIXOS
|
2010-07-15 13:09:30 +02:00
|
|
|
|
|
|
|
|
|
# Install a configuration.nix.
|
|
|
|
|
mkdir -p /mnt/etc/nixos
|
|
|
|
|
cp ${./amazon-config.nix} /mnt/etc/nixos/configuration.nix
|
2010-07-20 15:53:14 +02:00
|
|
|
|
|
2010-07-22 16:40:29 +02:00
|
|
|
|
# Generate the GRUB menu.
|
|
|
|
|
chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot
|
2010-01-20 19:10:02 +01:00
|
|
|
|
|
2011-07-29 16:55:42 +02:00
|
|
|
|
umount /mnt/proc
|
2010-01-20 19:10:02 +01:00
|
|
|
|
umount /mnt
|
|
|
|
|
''
|
|
|
|
|
);
|
|
|
|
|
|
2010-01-22 18:31:23 +01:00
|
|
|
|
fileSystems =
|
|
|
|
|
[ { mountPoint = "/";
|
|
|
|
|
device = "/dev/disk/by-label/nixos";
|
|
|
|
|
}
|
|
|
|
|
];
|
2010-01-21 09:03:58 +01:00
|
|
|
|
|
2010-08-30 00:29:10 +02:00
|
|
|
|
boot.initrd.kernelModules = [ "xen-blkfront" "aufs" ];
|
2010-07-20 15:53:14 +02:00
|
|
|
|
boot.kernelModules = [ "xen-netfront" ];
|
2010-01-21 09:03:58 +01:00
|
|
|
|
|
2011-11-08 16:12:11 +01:00
|
|
|
|
boot.extraModulePackages = [ config.boot.kernelPackages.aufs ];
|
2010-08-30 00:29:10 +02:00
|
|
|
|
|
2010-07-22 16:40:29 +02: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)";
|
|
|
|
|
|
2011-11-04 19:49:59 +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
|
|
|
|
|
# (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-30 00:09:15 +02:00
|
|
|
|
boot.initrd.postMountCommands =
|
|
|
|
|
''
|
2011-11-04 19:49:59 +01:00
|
|
|
|
diskNr=0
|
|
|
|
|
diskForAufs=
|
|
|
|
|
for device in /dev/xvd*; do
|
|
|
|
|
if [ "$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))
|
|
|
|
|
echo "mounting $device om $mp..."
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
mkdir -m 755 -p $targetRoot/$diskForAufs/root/var $targetRoot/var
|
|
|
|
|
mount --bind $targetRoot/$diskForAufs/root/var $targetRoot/var
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
'';
|
2010-08-30 00:29:10 +02:00
|
|
|
|
|
2011-11-04 19:49:59 +01:00
|
|
|
|
boot.initrd.extraUtilsCommands =
|
|
|
|
|
''
|
|
|
|
|
# We need swapon in the initrd.
|
|
|
|
|
cp ${pkgs.utillinux}/sbin/swapon $out/bin
|
2010-08-30 00:09:15 +02:00
|
|
|
|
'';
|
|
|
|
|
|
2010-01-21 09:03:58 +01:00
|
|
|
|
# Allow root logins only using the SSH key that the user specified
|
|
|
|
|
# at instance creation time.
|
2010-03-11 18:02:53 +01:00
|
|
|
|
services.openssh.enable = true;
|
|
|
|
|
services.openssh.permitRootLogin = "without-password";
|
2011-11-04 21:06:59 +01:00
|
|
|
|
|
|
|
|
|
# Force getting the hostname from EC2.
|
|
|
|
|
networking.hostName = mkDefault "";
|
2010-01-20 19:10:02 +01:00
|
|
|
|
}
|