···
{ config, lib, pkgs, ... }:
imports = [ ../profiles/headless.nix ./ec2-data.nix ];
8
-
system.build.amazonImage =
9
-
pkgs.vmTools.runInLinuxVM (
10
-
pkgs.runCommand "amazon-image"
14
-
diskImage=$out/nixos.img
15
-
${pkgs.vmTools.qemu}/bin/qemu-img create -f raw $diskImage "4G"
18
-
buildInputs = [ pkgs.utillinux pkgs.perl ];
19
-
exportReferencesGraph =
20
-
[ "closure" config.system.build.toplevel ];
23
-
# Create an empty filesystem and mount it.
24
-
${pkgs.e2fsprogs}/sbin/mkfs.ext4 -L nixos /dev/vda
25
-
${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda
15
+
Whether the EC2 instance is a HVM instance.
29
-
# The initrd expects these directories to exist.
30
-
mkdir /mnt/dev /mnt/proc /mnt/sys
22
+
system.build.amazonImage =
23
+
pkgs.vmTools.runInLinuxVM (
24
+
pkgs.runCommand "amazon-image"
28
+
diskImage=$out/nixos.img
29
+
${pkgs.vmTools.qemu}/bin/qemu-img create -f raw $diskImage "4G"
32
+
buildInputs = [ pkgs.utillinux pkgs.perl ];
33
+
exportReferencesGraph =
34
+
[ "closure" config.system.build.toplevel ];
37
+
# Create an empty filesystem and mount it.
38
+
${pkgs.e2fsprogs}/sbin/mkfs.ext4 -L nixos /dev/vda
39
+
${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda
43
+
# The initrd expects these directories to exist.
44
+
mkdir /mnt/dev /mnt/proc /mnt/sys
46
+
mount -o bind /proc /mnt/proc
32
-
mount -o bind /proc /mnt/proc
48
+
# Copy all paths in the closure to the filesystem.
49
+
storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure)
34
-
# Copy all paths in the closure to the filesystem.
35
-
storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure)
51
+
mkdir -p /mnt/nix/store
52
+
echo "copying everything (will take a while)..."
53
+
cp -prd $storePaths /mnt/nix/store/
37
-
mkdir -p /mnt/nix/store
38
-
echo "copying everything (will take a while)..."
39
-
cp -prd $storePaths /mnt/nix/store/
55
+
# Register the paths in the Nix database.
56
+
printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \
57
+
chroot /mnt ${config.nix.package}/bin/nix-store --load-db
41
-
# Register the paths in the Nix database.
42
-
printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \
43
-
chroot /mnt ${config.nix.package}/bin/nix-store --load-db
59
+
# Create the system profile to allow nixos-rebuild to work.
60
+
chroot /mnt ${config.nix.package}/bin/nix-env \
61
+
-p /nix/var/nix/profiles/system --set ${config.system.build.toplevel}
45
-
# Create the system profile to allow nixos-rebuild to work.
46
-
chroot /mnt ${config.nix.package}/bin/nix-env \
47
-
-p /nix/var/nix/profiles/system --set ${config.system.build.toplevel}
63
+
# `nixos-rebuild' requires an /etc/NIXOS.
65
+
touch /mnt/etc/NIXOS
49
-
# `nixos-rebuild' requires an /etc/NIXOS.
51
-
touch /mnt/etc/NIXOS
67
+
# `switch-to-configuration' requires a /bin/sh
69
+
ln -s ${config.system.build.binsh}/bin/sh /mnt/bin/sh
53
-
# `switch-to-configuration' requires a /bin/sh
55
-
ln -s ${config.system.build.binsh}/bin/sh /mnt/bin/sh
71
+
# Install a configuration.nix.
72
+
mkdir -p /mnt/etc/nixos
73
+
cp ${./amazon-config.nix} /mnt/etc/nixos/configuration.nix
57
-
# Install a configuration.nix.
58
-
mkdir -p /mnt/etc/nixos
59
-
cp ${./amazon-config.nix} /mnt/etc/nixos/configuration.nix
75
+
# Generate the GRUB menu.
76
+
chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot
61
-
# Generate the GRUB menu.
62
-
chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot
83
+
fileSystems."/".device = "/dev/disk/by-label/nixos";
69
-
fileSystems."/".device = "/dev/disk/by-label/nixos";
85
+
boot.initrd.kernelModules = [ "xen-blkfront" ];
86
+
boot.kernelModules = [ "xen-netfront" ];
71
-
boot.initrd.kernelModules = [ "xen-blkfront" ];
72
-
boot.kernelModules = [ "xen-netfront" ];
73
-
boot.kernelParams = [ "console=ttyS0" ];
88
+
# Generate a GRUB menu. Amazon's pv-grub uses this to boot our kernel/initrd.
89
+
boot.loader.grub.version = 1;
90
+
boot.loader.grub.device = if cfg.hvm then "/dev/xvda" else "nodev";
91
+
boot.loader.grub.timeout = 0;
92
+
boot.loader.grub.extraPerEntryConfig = "root (hd0${lib.optionalString cfg.hvm ",0"})";
75
-
# Generate a GRUB menu. Amazon's pv-grub uses this to boot our kernel/initrd.
76
-
boot.loader.grub.version = 1;
77
-
boot.loader.grub.device = "nodev";
78
-
boot.loader.grub.timeout = 0;
79
-
boot.loader.grub.extraPerEntryConfig = "root (hd0)";
94
+
boot.initrd.postDeviceCommands =
96
+
# Force udev to exit to prevent random "Device or resource busy
97
+
# while trying to open /dev/xvda" errors from fsck.
98
+
udevadm control --exit || true
81
-
boot.initrd.postDeviceCommands =
83
-
# Force udev to exit to prevent random "Device or resource busy
84
-
# while trying to open /dev/xvda" errors from fsck.
85
-
udevadm control --exit || true
102
+
# Mount all formatted ephemeral disks and activate all swap devices.
103
+
# We cannot do this with the ‘fileSystems’ and ‘swapDevices’ options
104
+
# because the set of devices is dependent on the instance type
105
+
# (e.g. "m1.large" has one ephemeral filesystem and one swap device,
106
+
# while "m1.large" has two ephemeral filesystems and no swap
107
+
# devices). Also, put /tmp and /var on /disk0, since it has a lot
108
+
# more space than the root device. Similarly, "move" /nix to /disk0
109
+
# by layering a unionfs-fuse mount on top of it so we have a lot more space for
111
+
boot.initrd.postMountCommands =
115
+
for device in /dev/xvd[abcde]*; do
116
+
if [ "$device" = /dev/xvda -o "$device" = /dev/xvda1 ]; then continue; fi
117
+
fsType=$(blkid -o value -s TYPE "$device" || true)
118
+
if [ "$fsType" = swap ]; then
119
+
echo "activating swap device $device..."
120
+
swapon "$device" || true
121
+
elif [ "$fsType" = ext3 ]; then
123
+
diskNr=$((diskNr + 1))
124
+
echo "mounting $device on $mp..."
125
+
if mountFS "$device" "$mp" "" ext3; then
126
+
if [ -z "$diskForUnionfs" ]; then diskForUnionfs="$mp"; fi
129
+
echo "skipping unknown device type $device"
89
-
# Mount all formatted ephemeral disks and activate all swap devices.
90
-
# We cannot do this with the ‘fileSystems’ and ‘swapDevices’ options
91
-
# because the set of devices is dependent on the instance type
92
-
# (e.g. "m1.large" has one ephemeral filesystem and one swap device,
93
-
# while "m1.large" has two ephemeral filesystems and no swap
94
-
# devices). Also, put /tmp and /var on /disk0, since it has a lot
95
-
# more space than the root device. Similarly, "move" /nix to /disk0
96
-
# by layering a unionfs-fuse mount on top of it so we have a lot more space for
98
-
boot.initrd.postMountCommands =
102
-
for device in /dev/xvd[abcde]*; do
103
-
if [ "$device" = /dev/xvda -o "$device" = /dev/xvda1 ]; then continue; fi
104
-
fsType=$(blkid -o value -s TYPE "$device" || true)
105
-
if [ "$fsType" = swap ]; then
106
-
echo "activating swap device $device..."
107
-
swapon "$device" || true
108
-
elif [ "$fsType" = ext3 ]; then
110
-
diskNr=$((diskNr + 1))
111
-
echo "mounting $device on $mp..."
112
-
if mountFS "$device" "$mp" "" ext3; then
113
-
if [ -z "$diskForUnionfs" ]; then diskForUnionfs="$mp"; fi
116
-
echo "skipping unknown device type $device"
133
+
if [ -n "$diskForUnionfs" ]; then
134
+
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root
120
-
if [ -n "$diskForUnionfs" ]; then
121
-
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root
136
+
mkdir -m 1777 -p $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
137
+
mount --bind $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
123
-
mkdir -m 1777 -p $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
124
-
mount --bind $targetRoot/$diskForUnionfs/root/tmp $targetRoot/tmp
139
+
if [ ! -e $targetRoot/.ebs ]; then
140
+
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/var $targetRoot/var
141
+
mount --bind $targetRoot/$diskForUnionfs/root/var $targetRoot/var
126
-
if [ ! -e $targetRoot/.ebs ]; then
127
-
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/var $targetRoot/var
128
-
mount --bind $targetRoot/$diskForUnionfs/root/var $targetRoot/var
143
+
mkdir -p /unionfs-chroot/ro-nix
144
+
mount --rbind $targetRoot/nix /unionfs-chroot/ro-nix
130
-
mkdir -p /unionfs-chroot/ro-nix
131
-
mount --rbind $targetRoot/nix /unionfs-chroot/ro-nix
146
+
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/nix
147
+
mkdir -p /unionfs-chroot/rw-nix
148
+
mount --rbind $targetRoot/$diskForUnionfs/root/nix /unionfs-chroot/rw-nix
133
-
mkdir -m 755 -p $targetRoot/$diskForUnionfs/root/nix
134
-
mkdir -p /unionfs-chroot/rw-nix
135
-
mount --rbind $targetRoot/$diskForUnionfs/root/nix /unionfs-chroot/rw-nix
150
+
unionfs -o allow_other,cow,nonempty,chroot=/unionfs-chroot,max_files=32768 /rw-nix=RW:/ro-nix=RO $targetRoot/nix
137
-
unionfs -o allow_other,cow,nonempty,chroot=/unionfs-chroot,max_files=32768 /rw-nix=RW:/ro-nix=RO $targetRoot/nix
155
+
boot.initrd.extraUtilsCommands =
157
+
# We need swapon in the initrd.
158
+
cp ${pkgs.utillinux}/sbin/swapon $out/bin
142
-
boot.initrd.extraUtilsCommands =
144
-
# We need swapon in the initrd.
145
-
cp ${pkgs.utillinux}/sbin/swapon $out/bin
161
+
# Don't put old configurations in the GRUB menu. The user has no
162
+
# way to select them anyway.
163
+
boot.loader.grub.configurationLimit = 0;
148
-
# Don't put old configurations in the GRUB menu. The user has no
149
-
# way to select them anyway.
150
-
boot.loader.grub.configurationLimit = 0;
165
+
# Allow root logins only using the SSH key that the user specified
166
+
# at instance creation time.
167
+
services.openssh.enable = true;
168
+
services.openssh.permitRootLogin = "without-password";
152
-
# Allow root logins only using the SSH key that the user specified
153
-
# at instance creation time.
154
-
services.openssh.enable = true;
155
-
services.openssh.permitRootLogin = "without-password";
170
+
# Force getting the hostname from EC2.
171
+
networking.hostName = mkDefault "";
157
-
# Force getting the hostname from EC2.
158
-
networking.hostName = mkDefault "";
173
+
# Always include cryptsetup so that Charon can use it.
174
+
environment.systemPackages = [ pkgs.cryptsetup ];
160
-
# Always include cryptsetup so that Charon can use it.
161
-
environment.systemPackages = [ pkgs.cryptsetup ];
176
+
boot.initrd.supportedFilesystems = [ "unionfs-fuse" ];
163
-
boot.initrd.supportedFilesystems = [ "unionfs-fuse" ];
178
+
# Prevent logging in as root without a password. This doesn't really matter,
179
+
# since the only PAM services that allow logging in with a null
180
+
# password are local ones that are inaccessible on EC2 machines.
181
+
security.initialRootPassword = mkDefault "!";