diff options
Diffstat (limited to 'systems/dilion/vms/base_image.nix')
-rw-r--r-- | systems/dilion/vms/base_image.nix | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/systems/dilion/vms/base_image.nix b/systems/dilion/vms/base_image.nix new file mode 100644 index 0000000..46b90eb --- /dev/null +++ b/systems/dilion/vms/base_image.nix | |||
@@ -0,0 +1,98 @@ | |||
1 | configuration_file: { pkgs, myEnv, nixpkgs, environment, pkgs-no-overlay, ... }: | ||
2 | let | ||
3 | config = (import (nixpkgs + "/nixos/lib/eval-config.nix") { | ||
4 | inherit (pkgs) system; | ||
5 | modules = [ { | ||
6 | _module.args.environment = environment; | ||
7 | myEnv = myEnv; | ||
8 | imports = [ | ||
9 | (nixpkgs + "/nixos/modules/profiles/qemu-guest.nix") | ||
10 | configuration_file | ||
11 | ]; | ||
12 | |||
13 | # We want our template image to be as small as possible, but the deployed image should be able to be | ||
14 | # of any size. Hence we resize on the first boot. | ||
15 | systemd.services.resize-main-fs = { | ||
16 | wantedBy = [ "multi-user.target" ]; | ||
17 | serviceConfig.Type = "oneshot"; | ||
18 | script = | ||
19 | '' | ||
20 | # Resize main partition to fill whole disk | ||
21 | echo ", +" | ${pkgs.utillinux}/bin/sfdisk /dev/vda --no-reread -N 1 | ||
22 | ${pkgs.parted}/bin/partprobe | ||
23 | # Resize filesystem | ||
24 | ${pkgs.e2fsprogs}/bin/resize2fs /dev/vda1 | ||
25 | ''; | ||
26 | }; | ||
27 | } ]; | ||
28 | }).config; | ||
29 | in pkgs-no-overlay.vmTools.runInLinuxVM ( | ||
30 | pkgs.runCommand "nixos-base-image" | ||
31 | { | ||
32 | memSize = 768; | ||
33 | preVM = | ||
34 | '' | ||
35 | mkdir $out | ||
36 | diskImage=image.qcow2 | ||
37 | ${pkgs-no-overlay.vmTools.qemu}/bin/qemu-img create -f qcow2 $diskImage 2G | ||
38 | mv closure xchg/ | ||
39 | ''; | ||
40 | postVM = | ||
41 | '' | ||
42 | echo compressing VM image... | ||
43 | ${pkgs-no-overlay.vmTools.qemu}/bin/qemu-img convert -c $diskImage -O qcow2 $out/nixos.qcow2 | ||
44 | ''; | ||
45 | buildInputs = [ pkgs.utillinux pkgs.perl pkgs.parted pkgs.e2fsprogs ]; | ||
46 | exportReferencesGraph = | ||
47 | [ "closure" config.system.build.toplevel ]; | ||
48 | } | ||
49 | '' | ||
50 | # Create the partition | ||
51 | parted /dev/vda mklabel msdos | ||
52 | parted /dev/vda -- mkpart primary ext4 1M -1s | ||
53 | |||
54 | # Format the partition | ||
55 | mkfs.ext4 -L nixos /dev/vda1 | ||
56 | mkdir /mnt | ||
57 | mount /dev/vda1 /mnt | ||
58 | |||
59 | for dir in dev proc sys; do | ||
60 | mkdir /mnt/$dir | ||
61 | mount --bind /$dir /mnt/$dir | ||
62 | done | ||
63 | |||
64 | storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure) | ||
65 | echo filling Nix store... | ||
66 | mkdir -p /mnt/nix/store | ||
67 | set -f | ||
68 | cp -prd $storePaths /mnt/nix/store | ||
69 | # The permissions will be set up incorrectly if the host machine is not running NixOS | ||
70 | chown -R 0:30000 /mnt/nix/store | ||
71 | |||
72 | mkdir -p /mnt/etc/nix | ||
73 | echo 'build-users-group = ' > /mnt/etc/nix/nix.conf | ||
74 | |||
75 | # Register the paths in the Nix database. | ||
76 | export USER=root | ||
77 | printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \ | ||
78 | chroot /mnt ${config.nix.package.out}/bin/nix-store --load-db | ||
79 | |||
80 | # Create the system profile to allow nixos-rebuild to work. | ||
81 | chroot /mnt ${config.nix.package.out}/bin/nix-env \ | ||
82 | -p /nix/var/nix/profiles/system --set ${config.system.build.toplevel} | ||
83 | |||
84 | # `nixos-rebuild' requires an /etc/NIXOS. | ||
85 | mkdir -p /mnt/etc/nixos | ||
86 | touch /mnt/etc/NIXOS | ||
87 | |||
88 | # `switch-to-configuration' requires a /bin/sh | ||
89 | mkdir -p /mnt/bin | ||
90 | ln -s ${config.system.build.binsh}/bin/sh /mnt/bin/sh | ||
91 | |||
92 | # Generate the GRUB menu. | ||
93 | chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot | ||
94 | |||
95 | umount /mnt/{proc,dev,sys} | ||
96 | umount /mnt | ||
97 | '' | ||
98 | ) | ||