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
|
|
|
|
|
2012-12-18 19:44:47 +01: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 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
|
|
|
|
|
''
|
|
|
|
|
);
|
|
|
|
|
|
2012-11-02 18:02:12 +01:00
|
|
|
|
fileSystems."/".device = "/dev/disk/by-label/nixos";
|
2010-01-21 09:03:58 +01:00
|
|
|
|
|
2012-12-16 19:16:17 +01:00
|
|
|
|
boot.initrd.kernelModules = [ "xen-blkfront" ];
|
2010-07-20 15:53:14 +02:00
|
|
|
|
boot.kernelModules = [ "xen-netfront" ];
|
2010-01-21 09:03:58 +01: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)";
|
|
|
|
|
|
2012-04-12 20:01:19 +02: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 01:02:34 +02:00
|
|
|
|
kill -9 -1
|
2012-04-12 20:01:19 +02:00
|
|
|
|
'';
|
|
|
|
|
|
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
|
2012-12-16 19:16:17 +01:00
|
|
|
|
# by layering a unionfs-fuse mount on top of it so we have a lot more space for
|
2011-11-04 19:49:59 +01:00
|
|
|
|
# Nix operations.
|
2010-08-30 00:09:15 +02:00
|
|
|
|
boot.initrd.postMountCommands =
|
|
|
|
|
''
|
2011-11-04 19:49:59 +01:00
|
|
|
|
diskNr=0
|
2012-12-16 19:16:17 +01:00
|
|
|
|
diskForUnionfs=
|
2012-04-12 18:48:01 +02:00
|
|
|
|
for device in /dev/xvd[abcde]*; do
|
2012-04-06 14:23:12 +02:00
|
|
|
|
if [ "$device" = /dev/xvda -o "$device" = /dev/xvda1 ]; then continue; fi
|
2011-11-04 19:49:59 +01: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 01:12:04 +01:00
|
|
|
|
echo "mounting $device on $mp..."
|
2011-11-04 19:49:59 +01:00
|
|
|
|
if mountFS "$device" "$mp" "" ext3; then
|
2012-12-16 19:16:17 +01:00
|
|
|
|
if [ -z "$diskForUnionfs" ]; then diskForUnionfs="$mp"; fi
|
2011-11-04 19:49:59 +01:00
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
echo "skipping unknown device type $device"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
2012-12-16 19:16:17 +01:00
|
|
|
|
if [ -n "$diskForUnionfs" ]; then
|
|
|
|
|
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root
|
2011-11-04 19:49:59 +01:00
|
|
|
|
|
2012-12-16 19:16:17 +01:00
|
|
|
|
mkdir -m 1777 -p $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
|
|
|
|
|
mount --bind $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
|
2011-11-04 19:49:59 +01:00
|
|
|
|
|
2012-04-10 13:11:05 +02:00
|
|
|
|
if [ ! -e $targetRoot/.ebs ]; then
|
2012-12-16 19:16:17 +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
|
2011-11-04 19:49:59 +01:00
|
|
|
|
|
2012-12-16 19:16:17 +01: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 13:31:08 +01:00
|
|
|
|
unionfs -o allow_other,cow,nonempty,chroot=/unionfs-chroot,max_files=32768 /rw-nix=RW:/ro-nix=RO $targetRoot/nix
|
2012-04-10 13:11:05 +02:00
|
|
|
|
fi
|
2011-11-04 19:49:59 +01:00
|
|
|
|
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
|
|
|
|
'';
|
|
|
|
|
|
2012-07-25 22:38:05 +02: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 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 "";
|
2012-05-15 01:43:38 +02:00
|
|
|
|
|
|
|
|
|
# Always include cryptsetup so that Charon can use it.
|
|
|
|
|
environment.systemPackages = [ pkgs.cryptsetup ];
|
2012-12-16 19:16:17 +01:00
|
|
|
|
|
|
|
|
|
boot.initrd.supportedFilesystems = [ "unionfs-fuse" ];
|
2010-01-20 19:10:02 +01:00
|
|
|
|
}
|