1 configuration_file: { pkgs, myEnv, nixpkgs, environment, pkgs-no-overlay, ... }:
3 config = (import (nixpkgs + "/nixos/lib/eval-config.nix") {
6 _module.args.environment = environment;
9 (nixpkgs + "/nixos/modules/profiles/qemu-guest.nix")
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";
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
24 ${pkgs.e2fsprogs}/bin/resize2fs /dev/vda1
29 in pkgs-no-overlay.vmTools.runInLinuxVM (
30 pkgs.runCommand "nixos-base-image"
37 ${pkgs-no-overlay.vmTools.qemu}/bin/qemu-img create -f qcow2 $diskImage 2G
42 echo compressing VM image...
43 ${pkgs-no-overlay.vmTools.qemu}/bin/qemu-img convert -c $diskImage -O qcow2 $out/nixos.qcow2
45 buildInputs = [ pkgs.utillinux pkgs.perl pkgs.parted pkgs.e2fsprogs ];
46 exportReferencesGraph =
47 [ "closure" config.system.build.toplevel ];
50 # Create the partition
51 parted /dev/vda mklabel msdos
52 parted /dev/vda -- mkpart primary ext4 1M -1s
54 # Format the partition
55 mkfs.ext4 -L nixos /dev/vda1
59 for dir in dev proc sys; do
61 mount --bind /$dir /mnt/$dir
64 storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure)
65 echo filling Nix store...
66 mkdir -p /mnt/nix/store
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
73 echo 'build-users-group = ' > /mnt/etc/nix/nix.conf
75 # Register the paths in the Nix database.
77 printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \
78 chroot /mnt ${config.nix.package.out}/bin/nix-store --load-db
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}
84 # `nixos-rebuild' requires an /etc/NIXOS.
85 mkdir -p /mnt/etc/nixos
88 # `switch-to-configuration' requires a /bin/sh
90 ln -s ${config.system.build.binsh}/bin/sh /mnt/bin/sh
92 # Generate the GRUB menu.
93 chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot
95 umount /mnt/{proc,dev,sys}