2010-01-20 18:10:02 +00:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
|
|
|
|
|
|
|
{
|
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
|
|
|
|
qemu-img create -f raw $diskImage "1024M"
|
|
|
|
'';
|
2010-01-21 08:03:58 +00:00
|
|
|
buildInputs = [ pkgs.utillinux pkgs.perl ];
|
2010-01-20 18:10:02 +00:00
|
|
|
exportReferencesGraph =
|
|
|
|
[ "closure" config.system.build.toplevel ];
|
|
|
|
}
|
|
|
|
''
|
|
|
|
# Create an empty filesysten 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
|
|
|
|
|
2010-01-20 18:10:02 +00: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 08:03:58 +00:00
|
|
|
cp -prvd $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.
|
|
|
|
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-01-22 20:22:40 +00:00
|
|
|
# Amazon assumes that there is a /sbin/init, so create one.
|
|
|
|
# Note that simply creating /sbin/init as a symlink breaks
|
|
|
|
# some EC2 initrds (like Ubuntu's) because they do a "test
|
|
|
|
# -x $mountPoint/sbin/init".
|
2010-01-20 18:10:02 +00:00
|
|
|
mkdir -p /mnt/sbin
|
2010-01-22 20:22:40 +00:00
|
|
|
echo "#! /nix/var/nix/profiles/system/init" > /mnt/sbin/init
|
|
|
|
chmod +x /mnt/sbin/init
|
2010-01-20 18:10:02 +00:00
|
|
|
|
|
|
|
umount /mnt
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
2010-01-21 08:03:58 +00:00
|
|
|
# On EC2 we don't get to supply our own kernel, so we can't load any
|
|
|
|
# modules. However, dhclient fails if the ipv6 module isn't loaded,
|
|
|
|
# unless it's compiled without IPv6 support. So do that.
|
2010-01-22 17:31:23 +00:00
|
|
|
nixpkgs.config.packageOverrides = pkgsOld:
|
|
|
|
{ dhcp = pkgs.lib.overrideDerivation pkgsOld.dhcp (oldAttrs:
|
|
|
|
{ configureFlags = "--disable-dhcpv6";
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
fileSystems =
|
|
|
|
[ { mountPoint = "/";
|
|
|
|
device = "/dev/disk/by-label/nixos";
|
|
|
|
}
|
|
|
|
{ mountPoint = "/mnt";
|
|
|
|
device = "/dev/sda2";
|
|
|
|
}
|
|
|
|
];
|
2010-01-21 08:03:58 +00:00
|
|
|
|
|
|
|
swapDevices =
|
2010-01-22 17:31:23 +00:00
|
|
|
[ { device = "/dev/sda3"; } ];
|
2010-01-21 08:03:58 +00: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.
|
|
|
|
services.sshd.enable = true;
|
2010-01-22 17:31:23 +00:00
|
|
|
services.sshd.permitRootLogin = "without-password";
|
2010-01-21 08:03:58 +00:00
|
|
|
|
2010-01-22 17:31:23 +00:00
|
|
|
# Obtain the SSH key and host name at startup time.
|
|
|
|
jobs.fetchEC2Data =
|
|
|
|
{ name = "fetch-ec2-data";
|
2010-01-21 08:03:58 +00: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 17:31:23 +00: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 08:03:58 +00:00
|
|
|
'';
|
|
|
|
};
|
2010-01-20 18:10:02 +00:00
|
|
|
}
|