Skip to content

Commit 223586a

Browse files
authored
virtualisation: Use system.build.image, normalize file names (#359339)
2 parents 61f3a96 + c78003c commit 223586a

16 files changed

+321
-163
lines changed

.git-blame-ignore-revs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,6 @@ ce21e97a1f20dee15da85c084f9d1148d84f853b
209209

210210
# treewide: migrate packages to pkgs/by-name, take 1
211211
571c71e6f73af34a229414f51585738894211408
212+
213+
# format files with nixfmt (#347275)
214+
adb9714bd909df283c66bbd641bd631ff50a4260

nixos/modules/image/images.nix

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,21 @@
88
let
99
inherit (lib) types;
1010

11-
imageModules = { };
11+
imageModules = {
12+
azure = [ ../virtualisation/azure-image.nix ];
13+
digital-ocean = [ ../virtualisation/digital-ocean-image.nix ];
14+
google-compute = [ ../virtualisation/google-compute-image.nix ];
15+
hyperv = [ ../virtualisation/hyperv-image.nix ];
16+
linode = [ ../virtualisation/linode-image.nix ];
17+
lxc = [ ../virtualisation/lxc-container.nix ];
18+
lxc-metadata = [ ../virtualisation/lxc-image-metadata.nix ];
19+
oci = [ ../virtualisation/oci-image.nix ];
20+
proxmox = [ ../virtualisation/proxmox-image.nix ];
21+
kubevirt = [ ../virtualisation/kubevirt.nix ];
22+
vagrant-virtualbox = [ ../virtualisation/vagrant-virtualbox-image.nix ];
23+
virtualbox = [ ../virtualisation/virtualbox-image.nix ];
24+
vmware = [ ../virtualisation/vmware-image.nix ];
25+
};
1226
imageConfigs = lib.mapAttrs (
1327
name: modules:
1428
extendModules {

nixos/modules/virtualisation/azure-image.nix

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ in
1313
imports = [
1414
./azure-common.nix
1515
./disk-size-option.nix
16+
../image/file-options.nix
1617
(lib.mkRenamedOptionModuleWith {
1718
sinceRelease = 2411;
1819
from = [
@@ -61,10 +62,14 @@ in
6162
};
6263

6364
config = {
65+
image.extension = "vhd";
66+
system.nixos.tags = [ "azure" ];
67+
system.build.image = config.system.build.azureImage;
6468
system.build.azureImage = import ../../lib/make-disk-image.nix {
6569
name = "azure-image";
70+
inherit (config.image) baseName;
6671
postVM = ''
67-
${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -o subformat=fixed,force_size -O vpc $diskImage $out/disk.vhd
72+
${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -o subformat=fixed,force_size -O vpc $diskImage $out/${config.image.fileName}
6873
rm $diskImage
6974
'';
7075
configFile = ./azure-config-user.nix;

nixos/modules/virtualisation/digital-ocean-image.nix

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ in
1414
imports = [
1515
./digital-ocean-config.nix
1616
./disk-size-option.nix
17+
../image/file-options.nix
1718
(lib.mkRenamedOptionModuleWith {
1819
sinceRelease = 2411;
1920
from = [
@@ -57,32 +58,53 @@ in
5758
};
5859

5960
#### implementation
60-
config = {
61-
system.build.digitalOceanImage = import ../../lib/make-disk-image.nix {
62-
name = "digital-ocean-image";
61+
config =
62+
let
6363
format = "qcow2";
64-
postVM =
65-
let
66-
compress =
67-
{
68-
"gzip" = "${pkgs.gzip}/bin/gzip";
69-
"bzip2" = "${pkgs.bzip2}/bin/bzip2";
70-
}
71-
.${cfg.compressionMethod};
72-
in
73-
''
74-
${compress} $diskImage
75-
'';
76-
configFile =
77-
if cfg.configFile == null then
78-
config.virtualisation.digitalOcean.defaultConfigFile
79-
else
80-
cfg.configFile;
81-
inherit (config.virtualisation) diskSize;
82-
inherit config lib pkgs;
83-
};
64+
in
65+
{
66+
image.extension = lib.concatStringsSep "." [
67+
format
68+
(
69+
{
70+
"gzip" = "gz";
71+
"bzip2" = "bz2";
72+
}
73+
.${cfg.compressionMethod}
74+
)
75+
];
76+
system.nixos.tags = [ "digital-ocean" ];
77+
system.build.image = config.system.build.digitalOceanImage;
78+
system.build.digitalOceanImage = import ../../lib/make-disk-image.nix {
79+
name = "digital-ocean-image";
80+
inherit (config.image) baseName;
81+
inherit (config.virtualisation) diskSize;
82+
inherit
83+
config
84+
lib
85+
pkgs
86+
format
87+
;
88+
postVM =
89+
let
90+
compress =
91+
{
92+
"gzip" = "${pkgs.gzip}/bin/gzip";
93+
"bzip2" = "${pkgs.bzip2}/bin/bzip2";
94+
}
95+
.${cfg.compressionMethod};
96+
in
97+
''
98+
${compress} $diskImage
99+
'';
100+
configFile =
101+
if cfg.configFile == null then
102+
config.virtualisation.digitalOcean.defaultConfigFile
103+
else
104+
cfg.configFile;
105+
};
84106

85-
};
107+
};
86108

87109
meta.maintainers = with maintainers; [
88110
arianvp

nixos/modules/virtualisation/google-compute-image.nix

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ in
2222
imports = [
2323
./google-compute-config.nix
2424
./disk-size-option.nix
25+
../image/file-options.nix
2526
(lib.mkRenamedOptionModuleWith {
2627
sinceRelease = 2411;
2728
from = [
@@ -72,8 +73,12 @@ in
7273
fsType = "vfat";
7374
};
7475

76+
system.nixos.tags = [ "google-compute" ];
77+
image.extension = "raw.tar.gz";
78+
system.build.image = config.system.build.googleComputeImage;
7579
system.build.googleComputeImage = import ../../lib/make-disk-image.nix {
7680
name = "google-compute-image";
81+
inherit (config.image) baseName;
7782
postVM = ''
7883
PATH=$PATH:${
7984
with pkgs;
@@ -83,10 +88,9 @@ in
8388
]
8489
}
8590
pushd $out
86-
mv $diskImage disk.raw
87-
tar -Sc disk.raw | gzip -${toString cfg.compressionLevel} > \
88-
nixos-image-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.raw.tar.gz
89-
rm $out/disk.raw
91+
tar -Sc $diskImage | gzip -${toString cfg.compressionLevel} > \
92+
${config.image.fileName}
93+
rm $diskImage
9094
popd
9195
'';
9296
format = "raw";

nixos/modules/virtualisation/hyperv-image.nix

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ in
1414

1515
imports = [
1616
./disk-size-option.nix
17+
../image/file-options.nix
1718
(lib.mkRenamedOptionModuleWith {
1819
sinceRelease = 2411;
1920
from = [
@@ -25,6 +26,18 @@ in
2526
"diskSize"
2627
];
2728
})
29+
(lib.mkRenamedOptionModuleWith {
30+
sinceRelease = 2505;
31+
from = [
32+
"virtualisation"
33+
"hyperv"
34+
"vmFileName"
35+
];
36+
to = [
37+
"image"
38+
"fileName"
39+
];
40+
})
2841
];
2942

3043
options = {
@@ -36,13 +49,6 @@ in
3649
The name of the derivation for the hyper-v appliance.
3750
'';
3851
};
39-
vmFileName = mkOption {
40-
type = types.str;
41-
default = "nixos-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.vhdx";
42-
description = ''
43-
The file name of the hyper-v appliance.
44-
'';
45-
};
4652
};
4753
};
4854

@@ -51,10 +57,14 @@ in
5157
# to avoid breaking existing configs using that.
5258
virtualisation.diskSize = lib.mkOverride 1490 (4 * 1024);
5359

60+
system.nixos.tags = [ "hyperv" ];
61+
image.extension = "vhdx";
62+
system.build.image = config.system.build.hypervImage;
5463
system.build.hypervImage = import ../../lib/make-disk-image.nix {
5564
name = cfg.vmDerivationName;
65+
baseName = config.image.baseName;
5666
postVM = ''
57-
${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -o subformat=dynamic -O vhdx $diskImage $out/${cfg.vmFileName}
67+
${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -o subformat=dynamic -O vhdx $diskImage $out/${config.image.fileName}
5868
rm $diskImage
5969
'';
6070
format = "raw";

nixos/modules/virtualisation/kubevirt.nix

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
{ config, lib, pkgs, ... }:
1+
{
2+
config,
3+
lib,
4+
pkgs,
5+
...
6+
}:
27

38
{
49
imports = [
510
../profiles/qemu-guest.nix
11+
../image/file-options.nix
612
];
713

814
config = {
@@ -22,8 +28,12 @@
2228
services.cloud-init.enable = true;
2329
systemd.services."serial-getty@ttyS0".enable = true;
2430

31+
system.nixos.tags = [ "kubevirt" ];
32+
image.extension = "qcow2";
33+
system.build.image = config.system.build.kubevirtImage;
2534
system.build.kubevirtImage = import ../../lib/make-disk-image.nix {
2635
inherit lib config pkgs;
36+
inherit (config.image) baseName;
2737
format = "qcow2";
2838
};
2939
};

nixos/modules/virtualisation/linode-image.nix

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ in
2020
imports = [
2121
./linode-config.nix
2222
./disk-size-option.nix
23+
../image/file-options.nix
2324
(lib.mkRenamedOptionModuleWith {
2425
sinceRelease = 2411;
2526
from = [
@@ -57,13 +58,17 @@ in
5758
};
5859

5960
config = {
61+
system.nixos.tags = [ "linode" ];
62+
image.extension = "img.gz";
63+
system.build.image = config.system.build.linodeImage;
6064
system.build.linodeImage = import ../../lib/make-disk-image.nix {
6165
name = "linode-image";
66+
baseName = config.image.baseName;
6267
# NOTE: Linode specifically requires images to be `gzip`-ed prior to upload
6368
# See: https://www.linode.com/docs/products/tools/images/guides/upload-an-image/#requirements-and-considerations
6469
postVM = ''
6570
${pkgs.gzip}/bin/gzip -${toString cfg.compressionLevel} -c -- $diskImage > \
66-
$out/nixos-image-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.img.gz
71+
$out/${config.image.fileName}
6772
rm $diskImage
6873
'';
6974
format = "raw";

0 commit comments

Comments
 (0)