1# nix-build '<nixpkgs/nixos>' -A config.system.build.openstackImage --arg configuration "{ imports = [ ./nixos/maintainers/scripts/openstack/openstack-image.nix ]; }"
2
3{ config, lib, pkgs, ... }:
4let
5 inherit (lib) mkOption types;
6 copyChannel = true;
7 cfg = config.openstackImage;
8 imageBootMode = if config.openstack.efi then "uefi" else "legacy-bios";
9in
10{
11 imports = [
12 ../../../modules/virtualisation/openstack-config.nix
13 ] ++ (lib.optional copyChannel ../../../modules/installer/cd-dvd/channel.nix);
14
15
16 options.openstackImage = {
17 name = mkOption {
18 type = types.str;
19 description = lib.mdDoc "The name of the generated derivation";
20 default = "nixos-openstack-image-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
21 };
22
23 sizeMB = mkOption {
24 type = types.int;
25 default = 8192;
26 description = lib.mdDoc "The size in MB of the image";
27 };
28
29 format = mkOption {
30 type = types.enum [ "raw" "qcow2" ];
31 default = "qcow2";
32 description = lib.mdDoc "The image format to output";
33 };
34 };
35
36 config = {
37 documentation.enable = copyChannel;
38 openstack = {
39 efi = true;
40 zfs = {
41 enable = true;
42 datasets = {
43 "tank/system/root".mount = "/";
44 "tank/system/var".mount = "/var";
45 "tank/local/nix".mount = "/nix";
46 "tank/user/home".mount = "/home";
47 };
48 };
49 };
50
51 system.build.openstackImage = import ../../../lib/make-single-disk-zfs-image.nix {
52 inherit lib config;
53 inherit (cfg) contents format name;
54 pkgs = import ../../../.. { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package
55
56 configFile = pkgs.writeText "configuration.nix"
57 ''
58 { modulesPath, ... }: {
59 imports = [ "''${modulesPath}/virtualisation/openstack-config.nix" ];
60 openstack.zfs.enable = true;
61 }
62 '';
63
64 includeChannel = copyChannel;
65
66 bootSize = 1000;
67
68 rootSize = cfg.sizeMB;
69 rootPoolProperties = {
70 ashift = 12;
71 autoexpand = "on";
72 };
73
74 datasets = config.openstack.zfs.datasets;
75
76 postVM = ''
77 extension=''${rootDiskImage##*.}
78 friendlyName=$out/${cfg.name}
79 rootDisk="$friendlyName.root.$extension"
80 mv "$rootDiskImage" "$rootDisk"
81
82 mkdir -p $out/nix-support
83 echo "file ${cfg.format} $rootDisk" >> $out/nix-support/hydra-build-products
84
85 ${pkgs.jq}/bin/jq -n \
86 --arg system_label ${lib.escapeShellArg config.system.nixos.label} \
87 --arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
88 --arg root_logical_bytes "$(${pkgs.qemu_kvm}/bin/qemu-img info --output json "$rootDisk" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
89 --arg boot_mode "${imageBootMode}" \
90 --arg root "$rootDisk" \
91 '{}
92 | .label = $system_label
93 | .boot_mode = $boot_mode
94 | .system = $system
95 | .disks.root.logical_bytes = $root_logical_bytes
96 | .disks.root.file = $root
97 ' > $out/nix-support/image-info.json
98 '';
99 };
100 };
101}