2024-10-17 18:27:37 +02:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
2014-09-06 21:37:46 +02:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
2019-11-18 12:39:18 -08:00
|
|
|
cfg = config.virtualisation.azureImage;
|
2014-09-06 21:37:46 +02:00
|
|
|
in
|
|
|
|
{
|
2024-09-10 19:14:43 +02:00
|
|
|
imports = [
|
|
|
|
./azure-common.nix
|
|
|
|
./disk-size-option.nix
|
2024-11-15 00:35:48 +01:00
|
|
|
../image/file-options.nix
|
2024-09-10 19:14:43 +02:00
|
|
|
(lib.mkRenamedOptionModuleWith {
|
|
|
|
sinceRelease = 2411;
|
|
|
|
from = [
|
|
|
|
"virtualisation"
|
|
|
|
"azureImage"
|
|
|
|
"diskSize"
|
|
|
|
];
|
|
|
|
to = [
|
|
|
|
"virtualisation"
|
|
|
|
"diskSize"
|
|
|
|
];
|
|
|
|
})
|
|
|
|
];
|
2020-08-07 14:43:58 +01:00
|
|
|
|
2024-08-09 14:47:30 +08:00
|
|
|
options.virtualisation.azureImage = {
|
|
|
|
bootSize = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 256;
|
|
|
|
description = ''
|
|
|
|
ESP partition size. Unit is MB.
|
|
|
|
Only effective when vmGeneration is `v2`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
contents = mkOption {
|
2023-10-08 19:35:14 +02:00
|
|
|
type = with types; listOf attrs;
|
|
|
|
default = [ ];
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2023-10-08 19:35:14 +02:00
|
|
|
Extra contents to add to the image.
|
|
|
|
'';
|
|
|
|
};
|
2024-08-09 14:47:30 +08:00
|
|
|
|
2024-11-27 21:23:30 +08:00
|
|
|
label = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "nixos";
|
|
|
|
description = ''
|
|
|
|
NixOS partition label.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-09 14:47:30 +08:00
|
|
|
vmGeneration = mkOption {
|
2024-10-17 18:27:37 +02:00
|
|
|
type =
|
|
|
|
with types;
|
|
|
|
enum [
|
|
|
|
"v1"
|
|
|
|
"v2"
|
|
|
|
];
|
2024-08-09 14:47:30 +08:00
|
|
|
default = "v1";
|
|
|
|
description = ''
|
|
|
|
VM Generation to use.
|
|
|
|
For v2, secure boot needs to be turned off during creation.
|
|
|
|
'';
|
|
|
|
};
|
2019-11-18 12:39:18 -08:00
|
|
|
};
|
2024-08-09 14:47:30 +08:00
|
|
|
|
2019-11-18 12:39:18 -08:00
|
|
|
config = {
|
2024-11-15 00:35:48 +01:00
|
|
|
image.extension = "vhd";
|
|
|
|
system.nixos.tags = [ "azure" ];
|
|
|
|
system.build.image = config.system.build.azureImage;
|
2019-11-18 12:39:18 -08:00
|
|
|
system.build.azureImage = import ../../lib/make-disk-image.nix {
|
|
|
|
name = "azure-image";
|
2024-11-15 00:35:48 +01:00
|
|
|
inherit (config.image) baseName;
|
2024-11-29 10:18:18 +08:00
|
|
|
|
|
|
|
# Azure expects vhd format with fixed size,
|
|
|
|
# generating raw format and convert with subformat args afterwards
|
|
|
|
format = "raw";
|
2019-11-18 12:39:18 -08:00
|
|
|
postVM = ''
|
2024-11-15 00:35:48 +01:00
|
|
|
${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -o subformat=fixed,force_size -O vpc $diskImage $out/${config.image.fileName}
|
2020-03-26 06:11:54 +00:00
|
|
|
rm $diskImage
|
2019-11-18 12:39:18 -08:00
|
|
|
'';
|
|
|
|
configFile = ./azure-config-user.nix;
|
2024-08-09 14:47:30 +08:00
|
|
|
|
|
|
|
bootSize = "${toString cfg.bootSize}M";
|
2024-11-27 21:23:30 +08:00
|
|
|
partitionTableType = if (cfg.vmGeneration == "v2") then "efi" else "legacy";
|
2024-08-09 14:47:30 +08:00
|
|
|
|
2024-11-27 21:23:30 +08:00
|
|
|
inherit (cfg) contents label;
|
2024-09-10 19:14:43 +02:00
|
|
|
inherit (config.virtualisation) diskSize;
|
2019-11-18 12:39:18 -08:00
|
|
|
inherit config lib pkgs;
|
|
|
|
};
|
2024-11-27 21:23:30 +08:00
|
|
|
|
|
|
|
boot.growPartition = true;
|
|
|
|
boot.loader.grub = rec {
|
|
|
|
efiSupport = (cfg.vmGeneration == "v2");
|
|
|
|
device = if efiSupport then "nodev" else "/dev/sda";
|
|
|
|
efiInstallAsRemovable = efiSupport;
|
2024-11-29 10:18:18 +08:00
|
|
|
# Force grub to run in text mode and output to console
|
|
|
|
# by disabling font and splash image
|
2024-11-28 01:05:58 +08:00
|
|
|
font = null;
|
|
|
|
splashImage = null;
|
2024-11-29 10:18:18 +08:00
|
|
|
# For Gen 1 VM, configurate grub output to serial_com0.
|
|
|
|
# Not needed for Gen 2 VM wbere serial_com0 does not exist,
|
2025-06-02 15:54:57 +02:00
|
|
|
# and outputting to console is enough to make Azure Serial Console working
|
2024-11-28 01:05:58 +08:00
|
|
|
extraConfig = lib.mkIf (!efiSupport) ''
|
2024-11-27 21:23:30 +08:00
|
|
|
serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
|
|
|
|
terminal_input --append serial
|
|
|
|
terminal_output --append serial
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2025-04-29 08:41:26 +05:30
|
|
|
fileSystems = {
|
2024-11-27 21:23:30 +08:00
|
|
|
"/" = {
|
|
|
|
device = "/dev/disk/by-label/${cfg.label}";
|
2024-11-29 10:18:18 +08:00
|
|
|
inherit (cfg) label;
|
2024-11-27 21:23:30 +08:00
|
|
|
fsType = "ext4";
|
|
|
|
autoResize = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
"/boot" = lib.mkIf (cfg.vmGeneration == "v2") {
|
|
|
|
device = "/dev/disk/by-label/ESP";
|
|
|
|
fsType = "vfat";
|
|
|
|
};
|
|
|
|
};
|
2019-11-18 12:39:18 -08:00
|
|
|
};
|
2014-09-06 21:37:46 +02:00
|
|
|
}
|