allow calling customizeImage without a name

This commit is contained in:
Sagar Ch 2024-05-25 02:04:28 +00:00
parent 82513c0714
commit 5e3c958428

View file

@ -1,7 +1,7 @@
# wrapper function around virt-customize to create custom OS image from an original OS image # wrapper function around virt-customize to create custom OS image from an original OS image
{ pkgs, lib, ... }: { pkgs, lib, ... }:
originalImage: { originalImage: {
name, name ? "",
hostname ? "", hostname ? "",
nameToHostname ? true, nameToHostname ? true,
diskSize ? "", diskSize ? "",
@ -14,18 +14,22 @@
}: }:
let let
originalImageName = lib.strings.removeSuffix "-vmix" (lib.strings.removeSuffix ".qcow2" originalImage.name); originalImageName = lib.strings.removeSuffix "-vmix" (lib.strings.removeSuffix ".qcow2" originalImage.name);
customImageName = (if name != "" then name else "custom") + "-${originalImageName}-vmix.qcow2";
resultImg = "./disk.qcow2"; resultImg = "./disk.qcow2";
qemuWrapperScript = (pkgs.writeShellScript "qemu-wrapper-script" '' qemuWrapperScript = (pkgs.writeShellScript "qemu-wrapper-script" ''
export PATH="${pkgs.qemu}/bin:$PATH" export PATH="${pkgs.qemu}/bin:$PATH"
exec qemu-kvm -nic user,model=virtio-net-pci "$@" exec qemu-kvm -nic user,model=virtio-net-pci "$@"
''); '');
setHostname = if hostname != "" then hostname else if nameToHostname then name else ""; setHostname = if hostname != "" then hostname else if nameToHostname then name else "";
virtCustomizeArgsHostname = if setHostname != "" then "--hostname '${setHostname}'" else ""; virtCustomizeArgsHostname = if setHostname != "" then "--hostname '${setHostname}'" else "";
virtCustomizeArgsInstall = if install != [] then "--install '${lib.strings.concatStringsSep "," install }'" else ""; virtCustomizeArgsInstall = if install != [] then "--install '${lib.strings.concatStringsSep "," install }'" else "";
virtCustomizeArgsCommandsFile = if commands != "" then ("--commands-from-file " + pkgs.writeText "${name}-vmix-virt-customize-commands-file" commands) else ""; virtCustomizeArgsCommandsFile = if commands != "" then ("--commands-from-file " + pkgs.writeText "${name}-virt-customize-commands-file" commands) else "";
virtCustomizeArgsRun = if run != "" then ("--run " + pkgs.writeScript "${name}-vmix-virt-customize-run-script" "${run}") else ""; virtCustomizeArgsRun = if run != "" then ("--run " + pkgs.writeScript "${name}-virt-customize-run-script" "${run}") else "";
in
pkgs.runCommand "${name}-${originalImageName}-vmix.qcow2" { __noChroot = true; } '' builderCommand = ''
export PATH="${pkgs.qemu}/bin:${pkgs.curl}/bin:$PATH" export PATH="${pkgs.qemu}/bin:${pkgs.curl}/bin:$PATH"
# create resulting image backed by original image # create resulting image backed by original image
@ -46,4 +50,6 @@
${virtCustomizeArgsRun} ${virtCustomizeArgsRun}
mv ${resultImg} $out mv ${resultImg} $out
'' '';
in
pkgs.runCommand customImageName { __noChroot = true; } builderCommand