2010-01-20 19:10:02 +01:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
|
|
|
|
|
|
|
{
|
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"
|
2010-01-20 19:10:02 +01:00
|
|
|
'';
|
2010-01-21 09:03:58 +01:00
|
|
|
buildInputs = [ pkgs.utillinux pkgs.perl ];
|
2010-01-20 19:10:02 +01:00
|
|
|
exportReferencesGraph =
|
|
|
|
[ "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
|
|
|
|
|
2010-01-20 19:10:02 +01:00
|
|
|
# Copy all paths in the closure to the filesystem.
|
|
|
|
storePaths=$(perl ${pkgs.pathsFromGraph} $ORIG_TMPDIR/closure)
|
|
|
|
|
|
|
|
mkdir -p /mnt/nix/store
|
2010-01-21 09:03:58 +01:00
|
|
|
cp -prvd $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.
|
|
|
|
printRegistration=1 perl ${pkgs.pathsFromGraph} $ORIG_TMPDIR/closure | \
|
|
|
|
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 \
|
|
|
|
-p /nix/var/nix/profiles/system --set ${config.system.build.toplevel}
|
|
|
|
|
|
|
|
# `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
|
|
|
|
|
|
|
umount /mnt
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
2010-01-22 18:31:23 +01:00
|
|
|
fileSystems =
|
|
|
|
[ { mountPoint = "/";
|
|
|
|
device = "/dev/disk/by-label/nixos";
|
|
|
|
}
|
2010-08-30 00:09:15 +02:00
|
|
|
{ mountPoint = "/ephemeral0";
|
2010-07-20 15:53:14 +02:00
|
|
|
device = "/dev/xvdc";
|
2010-08-30 00:09:15 +02:00
|
|
|
neededForBoot = true;
|
2010-01-22 18:31:23 +01:00
|
|
|
}
|
|
|
|
];
|
2010-01-21 09:03:58 +01:00
|
|
|
|
|
|
|
swapDevices =
|
2010-07-20 15:53:14 +02:00
|
|
|
[ { device = "/dev/xvdb"; } ];
|
|
|
|
|
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
|
|
|
|
2010-08-30 00:29:10 +02:00
|
|
|
boot.extraModulePackages = [ config.boot.kernelPackages.aufs2 ];
|
|
|
|
|
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)";
|
|
|
|
|
2010-08-30 00:09:15 +02:00
|
|
|
# Put /tmp and /var on /ephemeral0, which has a lot more space.
|
|
|
|
# Unfortunately we can't do this with the `fileSystems' option
|
|
|
|
# because it has no support for creating the source of a bind
|
2010-08-30 00:29:10 +02:00
|
|
|
# mount. Also, "move" /nix to /ephemeral0 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 =
|
|
|
|
''
|
|
|
|
mkdir -m 1777 -p $targetRoot/ephemeral0/tmp
|
2010-09-01 13:00:35 +02:00
|
|
|
mkdir -m 1777 -p $targetRoot/tmp
|
2010-08-30 00:09:15 +02:00
|
|
|
mount --bind $targetRoot/ephemeral0/tmp $targetRoot/tmp
|
2010-08-30 00:29:10 +02:00
|
|
|
|
2010-08-30 00:09:15 +02:00
|
|
|
mkdir -m 755 -p $targetRoot/ephemeral0/var
|
2010-09-01 13:00:35 +02:00
|
|
|
mkdir -m 755 -p $targetRoot/var
|
2010-08-30 00:09:15 +02:00
|
|
|
mount --bind $targetRoot/ephemeral0/var $targetRoot/var
|
2010-08-30 00:29:10 +02:00
|
|
|
|
|
|
|
mkdir -m 755 -p $targetRoot/ephemeral0/nix
|
|
|
|
mount -t aufs -o dirs=$targetRoot/ephemeral0/nix=rw:$targetRoot/nix=rr none $targetRoot/nix
|
2010-08-30 00:09:15 +02:00
|
|
|
'';
|
|
|
|
|
2010-01-21 09:03:58 +01:00
|
|
|
# There are no virtual consoles.
|
|
|
|
services.mingetty.ttys = [ ];
|
|
|
|
|
|
|
|
# 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";
|
2010-01-21 09:03:58 +01:00
|
|
|
|
2010-01-22 18:31:23 +01:00
|
|
|
# Obtain the SSH key and host name at startup time.
|
|
|
|
jobs.fetchEC2Data =
|
|
|
|
{ name = "fetch-ec2-data";
|
2010-01-21 09:03:58 +01:00
|
|
|
|
|
|
|
startOn = "ip-up";
|
|
|
|
|
|
|
|
task = true;
|
|
|
|
|
|
|
|
script =
|
|
|
|
''
|
|
|
|
echo "obtaining SSH key..."
|
|
|
|
mkdir -p /root/.ssh
|
|
|
|
${pkgs.curl}/bin/curl --retry 3 --retry-delay 0 --fail \
|
|
|
|
-o /root/key.pub \
|
|
|
|
http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key
|
|
|
|
if [ $? -eq 0 -a -e /root/key.pub ]; then
|
|
|
|
if ! grep -q -f /root/key.pub /root/.ssh/authorized_keys; then
|
|
|
|
cat /root/key.pub >> /root/.ssh/authorized_keys
|
|
|
|
echo "new key added to authorized_keys"
|
|
|
|
fi
|
|
|
|
chmod 600 /root/.ssh/authorized_keys
|
|
|
|
rm -f /root/key.pub
|
|
|
|
fi
|
2010-01-22 18:31:23 +01:00
|
|
|
|
2010-08-29 23:03:22 +02:00
|
|
|
# Print the host public key on the console so that the user
|
|
|
|
# can obtain it securely by parsing the output of
|
|
|
|
# ec2-get-console-output.
|
|
|
|
echo "-----BEGIN SSH HOST KEY FINGERPRINTS-----" > /dev/console
|
|
|
|
${pkgs.openssh}/bin/ssh-keygen -l -f /etc/ssh/ssh_host_dsa_key.pub > /dev/console
|
|
|
|
echo "-----END SSH HOST KEY FINGERPRINTS-----" > /dev/console
|
|
|
|
|
2010-01-22 18:31:23 +01:00
|
|
|
echo "setting host name..."
|
|
|
|
${pkgs.nettools}/bin/hostname $(${pkgs.curl}/bin/curl http://169.254.169.254/1.0/meta-data/hostname)
|
2010-01-21 09:03:58 +01:00
|
|
|
'';
|
|
|
|
};
|
2010-01-20 19:10:02 +01:00
|
|
|
}
|