1# nix-build '<nixpkgs/nixos>' -A config.system.build.openstackImage --arg configuration "{ imports = [ ./nixos/maintainers/scripts/openstack/openstack-image.nix ]; }"
2
3{
4 config,
5 lib,
6 pkgs,
7 ...
8}:
9let
10 inherit (lib) mkOption types;
11 copyChannel = true;
12 cfg = config.openstackImage;
13 imageBootMode = if config.openstack.efi then "uefi" else "legacy-bios";
14in
15{
16 imports = [
17 ../../../modules/virtualisation/openstack-config.nix
18 ../../../modules/virtualisation/disk-size-option.nix
19 ../../../modules/image/file-options.nix
20 (lib.mkRenamedOptionModuleWith {
21 sinceRelease = 2411;
22 from = [
23 "openstackImage"
24 "sizeMB"
25 ];
26 to = [
27 "virtualisation"
28 "diskSize"
29 ];
30 })
31 (lib.mkRenamedOptionModuleWith {
32 sinceRelease = 2505;
33 from = [
34 "openstackImage"
35 "name"
36 ];
37 to = [
38 "image"
39 "baseName"
40 ];
41 })
42
43 ]
44 ++ (lib.optional copyChannel ../../../modules/installer/cd-dvd/channel.nix);
45
46 options.openstackImage = {
47 ramMB = mkOption {
48 type = types.int;
49 default = (3 * 1024);
50 description = "RAM allocation for build VM";
51 };
52
53 format = mkOption {
54 type = types.enum [
55 "raw"
56 "qcow2"
57 ];
58 default = "qcow2";
59 description = "The image format to output";
60 };
61 };
62
63 config = {
64 documentation.enable = copyChannel;
65 openstack = {
66 efi = true;
67 zfs = {
68 enable = true;
69 datasets = {
70 "tank/system/root".mount = "/";
71 "tank/system/var".mount = "/var";
72 "tank/local/nix".mount = "/nix";
73 "tank/user/home".mount = "/home";
74 };
75 };
76 };
77
78 # Use a priority just below mkOptionDefault (1500) instead of lib.mkDefault
79 # to avoid breaking existing configs using that.
80 virtualisation.diskSize = lib.mkOverride 1490 (8 * 1024);
81 virtualisation.diskSizeAutoSupported = false;
82
83 image.extension = cfg.format;
84 system.nixos.tags = [
85 "openstack"
86 "zfs"
87 ];
88 system.build.image = config.system.build.openstackImage;
89 system.build.openstackImage = import ../../../lib/make-single-disk-zfs-image.nix {
90 inherit lib config;
91 inherit (cfg) contents format;
92 name = config.image.baseName;
93 pkgs = import ../../../.. { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package
94
95 configFile = pkgs.writeText "configuration.nix" ''
96 { modulesPath, ... }: {
97 imports = [ "''${modulesPath}/virtualisation/openstack-config.nix" ];
98 openstack.zfs.enable = true;
99 }
100 '';
101
102 includeChannel = copyChannel;
103
104 bootSize = 1000;
105 memSize = cfg.ramMB;
106 rootSize = config.virtualisation.diskSize;
107 rootPoolProperties = {
108 ashift = 12;
109 autoexpand = "on";
110 };
111
112 datasets = config.openstack.zfs.datasets;
113
114 postVM = ''
115 extension=''${rootDiskImage##*.}
116 friendlyName=$out/${config.image.baseName}
117 rootDisk="$friendlyName.$extension"
118 mv "$rootDiskImage" "$rootDisk"
119
120 mkdir -p $out/nix-support
121 echo "file ${cfg.format} $rootDisk" >> $out/nix-support/hydra-build-products
122
123 ${pkgs.jq}/bin/jq -n \
124 --arg system_label ${lib.escapeShellArg config.system.nixos.label} \
125 --arg system ${lib.escapeShellArg pkgs.stdenv.hostPlatform.system} \
126 --arg root_logical_bytes "$(${pkgs.qemu_kvm}/bin/qemu-img info --output json "$rootDisk" | ${pkgs.jq}/bin/jq '."virtual-size"')" \
127 --arg boot_mode "${imageBootMode}" \
128 --arg root "$rootDisk" \
129 '{}
130 | .label = $system_label
131 | .boot_mode = $boot_mode
132 | .system = $system
133 | .disks.root.logical_bytes = $root_logical_bytes
134 | .disks.root.file = $root
135 ' > $out/nix-support/image-info.json
136 '';
137 };
138 };
139}