1{ config, lib, pkgs, ... }:
2
3with lib;
4
5{
6 imports = [ ../profiles/qemu-guest.nix ../profiles/headless.nix ./ec2-data.nix ];
7
8 system.build.novaImage =
9 pkgs.vmTools.runInLinuxVM (
10 pkgs.runCommand "nova-image"
11 { preVM =
12 ''
13 mkdir $out
14 diskImage=$out/image
15 ${pkgs.vmTools.qemu}/bin/qemu-img create -f raw $diskImage "4G"
16 mv closure xchg/
17 '';
18 buildInputs = [ pkgs.utillinux pkgs.perl ];
19 exportReferencesGraph =
20 [ "closure" config.system.build.toplevel ];
21 }
22 ''
23 # Create a single / partition.
24 ${pkgs.parted}/sbin/parted /dev/vda mklabel msdos
25 ${pkgs.parted}/sbin/parted /dev/vda -- mkpart primary ext2 1M -1s
26 . /sys/class/block/vda1/uevent
27 mknod /dev/vda1 b $MAJOR $MINOR
28
29 # Create an empty filesystem and mount it.
30 ${pkgs.e2fsprogs}/sbin/mkfs.ext3 -L nixos /dev/vda1
31 ${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda1
32 mkdir /mnt
33 mount /dev/vda1 /mnt
34
35 # The initrd expects these directories to exist.
36 mkdir /mnt/dev /mnt/proc /mnt/sys
37 mount --bind /proc /mnt/proc
38 mount --bind /dev /mnt/dev
39 mount --bind /sys /mnt/sys
40
41 # Copy all paths in the closure to the filesystem.
42 storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure)
43
44 mkdir -p /mnt/nix/store
45 ${pkgs.rsync}/bin/rsync -av $storePaths /mnt/nix/store/
46
47 # Register the paths in the Nix database.
48 printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \
49 chroot /mnt ${config.nix.package}/bin/nix-store --load-db --option build-users-group ""
50
51 # Create the system profile to allow nixos-rebuild to work.
52 chroot /mnt ${config.nix.package}/bin/nix-env --option build-users-group "" \
53 -p /nix/var/nix/profiles/system --set ${config.system.build.toplevel}
54
55 # `nixos-rebuild' requires an /etc/NIXOS.
56 mkdir -p /mnt/etc
57 touch /mnt/etc/NIXOS
58
59 # `switch-to-configuration' requires a /bin/sh
60 mkdir -p /mnt/bin
61 ln -s ${config.system.build.binsh}/bin/sh /mnt/bin/sh
62
63 # Install a configuration.nix.
64 mkdir -p /mnt/etc/nixos
65 cp ${./nova-config.nix} /mnt/etc/nixos/configuration.nix
66
67 # Generate the GRUB menu.
68 chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot
69
70 umount /mnt/proc /mnt/dev /mnt/sys
71 umount /mnt
72 ''
73 );
74
75 fileSystems."/".device = "/dev/disk/by-label/nixos";
76
77 boot.kernelParams = [ "console=ttyS0" ];
78
79 boot.loader.grub.version = 2;
80 boot.loader.grub.device = "/dev/vda";
81 boot.loader.grub.timeout = 0;
82
83 # Put /tmp and /var on /ephemeral0, which has a lot more space.
84 # Unfortunately we can't do this with the `fileSystems' option
85 # because it has no support for creating the source of a bind
86 # mount. Also, "move" /nix to /ephemeral0 by layering a unionfs-fuse
87 # mount on top of it so we have a lot more space for Nix operations.
88 /*
89 boot.initrd.postMountCommands =
90 ''
91 mkdir -m 1777 -p $targetRoot/ephemeral0/tmp
92 mkdir -m 1777 -p $targetRoot/tmp
93 mount --bind $targetRoot/ephemeral0/tmp $targetRoot/tmp
94
95 mkdir -m 755 -p $targetRoot/ephemeral0/var
96 mkdir -m 755 -p $targetRoot/var
97 mount --bind $targetRoot/ephemeral0/var $targetRoot/var
98
99 mkdir -p /unionfs-chroot/ro-nix
100 mount --rbind $targetRoot/nix /unionfs-chroot/ro-nix
101
102 mkdir -p /unionfs-chroot/rw-nix
103 mkdir -m 755 -p $targetRoot/ephemeral0/nix
104 mount --rbind $targetRoot/ephemeral0/nix /unionfs-chroot/rw-nix
105 unionfs -o allow_other,cow,nonempty,chroot=/unionfs-chroot,max_files=32768 /rw-nix=RW:/ro-nix=RO $targetRoot/nix
106 '';
107
108 boot.initrd.supportedFilesystems = [ "unionfs-fuse" ];
109 */
110
111 # Allow root logins only using the SSH key that the user specified
112 # at instance creation time.
113 services.openssh.enable = true;
114 services.openssh.permitRootLogin = "without-password";
115}