Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

virtualisation: Use system.build.image, normalize file names #359339

Merged
merged 16 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,6 @@ ce21e97a1f20dee15da85c084f9d1148d84f853b

# treewide: migrate packages to pkgs/by-name, take 1
571c71e6f73af34a229414f51585738894211408

# format files with nixfmt (#347275)
adb9714bd909df283c66bbd641bd631ff50a4260
16 changes: 15 additions & 1 deletion nixos/modules/image/images.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@
let
inherit (lib) types;

imageModules = { };
imageModules = {
azure = [ ../virtualisation/azure-image.nix ];
arianvp marked this conversation as resolved.
Show resolved Hide resolved
digital-ocean = [ ../virtualisation/digital-ocean-image.nix ];
google-compute = [ ../virtualisation/google-compute-image.nix ];
hyperv = [ ../virtualisation/hyperv-image.nix ];
linode = [ ../virtualisation/linode-image.nix ];
lxc = [ ../virtualisation/lxc-container.nix ];
lxc-metadata = [ ../virtualisation/lxc-image-metadata.nix ];
oci = [ ../virtualisation/oci-image.nix ];
proxmox = [ ../virtualisation/proxmox-image.nix ];
kubevirt = [ ../virtualisation/kubevirt.nix ];
vagrant-virtualbox = [ ../virtualisation/vagrant-virtualbox-image.nix ];
virtualbox = [ ../virtualisation/virtualbox-image.nix ];
vmware = [ ../virtualisation/vmware-image.nix ];
};
imageConfigs = lib.mapAttrs (
name: modules:
extendModules {
Expand Down
7 changes: 6 additions & 1 deletion nixos/modules/virtualisation/azure-image.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ in
imports = [
./azure-common.nix
./disk-size-option.nix
../image/file-options.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
Expand Down Expand Up @@ -61,10 +62,14 @@ in
};

config = {
image.extension = "vhd";
system.nixos.tags = [ "azure" ];
system.build.image = config.system.build.azureImage;
system.build.azureImage = import ../../lib/make-disk-image.nix {
name = "azure-image";
inherit (config.image) baseName;
postVM = ''
${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -o subformat=fixed,force_size -O vpc $diskImage $out/disk.vhd
${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -o subformat=fixed,force_size -O vpc $diskImage $out/${config.image.fileName}
rm $diskImage
'';
configFile = ./azure-config-user.nix;
Expand Down
70 changes: 46 additions & 24 deletions nixos/modules/virtualisation/digital-ocean-image.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ in
imports = [
./digital-ocean-config.nix
./disk-size-option.nix
../image/file-options.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
Expand Down Expand Up @@ -57,32 +58,53 @@ in
};

#### implementation
config = {
system.build.digitalOceanImage = import ../../lib/make-disk-image.nix {
name = "digital-ocean-image";
config =
let
format = "qcow2";
postVM =
let
compress =
{
"gzip" = "${pkgs.gzip}/bin/gzip";
"bzip2" = "${pkgs.bzip2}/bin/bzip2";
}
.${cfg.compressionMethod};
in
''
${compress} $diskImage
'';
configFile =
if cfg.configFile == null then
config.virtualisation.digitalOcean.defaultConfigFile
else
cfg.configFile;
inherit (config.virtualisation) diskSize;
inherit config lib pkgs;
};
in
{
image.extension = lib.concatStringsSep "." [
format
(
{
"gzip" = "gz";
"bzip2" = "bz2";
}
.${cfg.compressionMethod}
)
];
system.nixos.tags = [ "digital-ocean" ];
system.build.image = config.system.build.digitalOceanImage;
system.build.digitalOceanImage = import ../../lib/make-disk-image.nix {
name = "digital-ocean-image";
inherit (config.image) baseName;
inherit (config.virtualisation) diskSize;
inherit
config
lib
pkgs
format
;
postVM =
let
compress =
{
"gzip" = "${pkgs.gzip}/bin/gzip";
"bzip2" = "${pkgs.bzip2}/bin/bzip2";
}
.${cfg.compressionMethod};
in
''
${compress} $diskImage
'';
configFile =
if cfg.configFile == null then
config.virtualisation.digitalOcean.defaultConfigFile
else
cfg.configFile;
};

};
};

meta.maintainers = with maintainers; [
arianvp
Expand Down
12 changes: 8 additions & 4 deletions nixos/modules/virtualisation/google-compute-image.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ in
imports = [
./google-compute-config.nix
./disk-size-option.nix
../image/file-options.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
Expand Down Expand Up @@ -72,8 +73,12 @@ in
fsType = "vfat";
};

system.nixos.tags = [ "google-compute" ];
image.extension = "raw.tar.gz";
system.build.image = config.system.build.googleComputeImage;
system.build.googleComputeImage = import ../../lib/make-disk-image.nix {
name = "google-compute-image";
inherit (config.image) baseName;
postVM = ''
PATH=$PATH:${
with pkgs;
Expand All @@ -83,10 +88,9 @@ in
]
}
pushd $out
mv $diskImage disk.raw
tar -Sc disk.raw | gzip -${toString cfg.compressionLevel} > \
nixos-image-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.raw.tar.gz
rm $out/disk.raw
tar -Sc $diskImage | gzip -${toString cfg.compressionLevel} > \
${config.image.fileName}
rm $diskImage
popd
'';
format = "raw";
Expand Down
26 changes: 18 additions & 8 deletions nixos/modules/virtualisation/hyperv-image.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ in

imports = [
./disk-size-option.nix
../image/file-options.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
Expand All @@ -25,6 +26,18 @@ in
"diskSize"
];
})
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2505;
from = [
"virtualisation"
"hyperv"
"vmFileName"
];
to = [
"image"
"fileName"
];
})
];

options = {
Expand All @@ -36,13 +49,6 @@ in
The name of the derivation for the hyper-v appliance.
'';
};
vmFileName = mkOption {
type = types.str;
default = "nixos-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.vhdx";
description = ''
The file name of the hyper-v appliance.
'';
};
};
};

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

system.nixos.tags = [ "hyperv" ];
image.extension = "vhdx";
system.build.image = config.system.build.hypervImage;
system.build.hypervImage = import ../../lib/make-disk-image.nix {
name = cfg.vmDerivationName;
baseName = config.image.baseName;
postVM = ''
${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -o subformat=dynamic -O vhdx $diskImage $out/${cfg.vmFileName}
${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -o subformat=dynamic -O vhdx $diskImage $out/${config.image.fileName}
rm $diskImage
'';
format = "raw";
Expand Down
12 changes: 11 additions & 1 deletion nixos/modules/virtualisation/kubevirt.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{ config, lib, pkgs, ... }:
{
config,
lib,
pkgs,
...
}:

{
imports = [
../profiles/qemu-guest.nix
../image/file-options.nix
];

config = {
Expand All @@ -22,8 +28,12 @@
services.cloud-init.enable = true;
systemd.services."serial-getty@ttyS0".enable = true;

system.nixos.tags = [ "kubevirt" ];
image.extension = "qcow2";
system.build.image = config.system.build.kubevirtImage;
system.build.kubevirtImage = import ../../lib/make-disk-image.nix {
inherit lib config pkgs;
inherit (config.image) baseName;
format = "qcow2";
};
};
Expand Down
7 changes: 6 additions & 1 deletion nixos/modules/virtualisation/linode-image.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ in
imports = [
./linode-config.nix
./disk-size-option.nix
../image/file-options.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
Expand Down Expand Up @@ -57,13 +58,17 @@ in
};

config = {
system.nixos.tags = [ "linode" ];
image.extension = "img.gz";
system.build.image = config.system.build.linodeImage;
system.build.linodeImage = import ../../lib/make-disk-image.nix {
name = "linode-image";
baseName = config.image.baseName;
# NOTE: Linode specifically requires images to be `gzip`-ed prior to upload
# See: https://www.linode.com/docs/products/tools/images/guides/upload-an-image/#requirements-and-considerations
postVM = ''
${pkgs.gzip}/bin/gzip -${toString cfg.compressionLevel} -c -- $diskImage > \
$out/nixos-image-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.img.gz
$out/${config.image.fileName}
rm $diskImage
'';
format = "raw";
Expand Down
Loading