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

make-disk-image: convert into NixOS module #771

Merged
merged 1 commit into from
Sep 15, 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
7 changes: 0 additions & 7 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ with builtins;
let
outputs = import ../default.nix { inherit lib diskoLib; };
diskoLib = {

# like make-disk-image.nix from nixpkgs, but with disko config
makeDiskImages = args: (import ./make-disk-image.nix ({ inherit diskoLib; } // args)).pure;

# a version of makeDiskImage which runs outside of the store
makeDiskImagesScript = args: (import ./make-disk-image.nix ({ inherit diskoLib; } // args)).impure;

testLib = import ./tests.nix { inherit lib makeTest eval-config; };
# like lib.types.oneOf but instead of a list takes an attrset
# uses the field "type" to find the correct type in the attrset
Expand Down
23 changes: 10 additions & 13 deletions lib/interactive-vm.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# We need to specify extendModules here to ensure that it is available
# in args for makeDiskImages
{ diskoLib, modulesPath, config, pkgs, lib, extendModules, ... }@args:
{ diskoLib, modulesPath, config, pkgs, lib, ... }:

let
vm_disko = (diskoLib.testLib.prepareDiskoConfig config diskoLib.testLib.devices).disko;
Expand All @@ -20,15 +18,6 @@ let
};
}).config;
disks = lib.attrValues cfg_.disko.devices.disk;
diskoImages = diskoLib.makeDiskImages {
nixosConfig = args;
copyNixStore = false;
extraConfig = {
disko.devices = cfg_.disko.devices;
};
testMode = true;
imageFormat = "qcow2";
};
rootDisk = {
name = "root";
file = ''"$tmp"/${(builtins.head disks).name}.qcow2'';
Expand Down Expand Up @@ -58,6 +47,14 @@ in
diskoBasedConfiguration
];

disko.testMode = true;

disko.imageBuilder.copyNixStore = false;
disko.imageBuilder.extraConfig = {
disko.devices = cfg_.disko.devices;
};
disko.imageBuilder.imageFormat = "qcow2";

virtualisation.useEFIBoot = config.disko.tests.efi;
virtualisation.memorySize = config.disko.memSize;
virtualisation.useDefaultFilesystems = false;
Expand All @@ -72,7 +69,7 @@ in
trap 'rm -rf "$tmp"' EXIT
${lib.concatMapStringsSep "\n" (disk: ''
${pkgs.qemu}/bin/qemu-img create -f qcow2 \
-b ${diskoImages}/${disk.name}.qcow2 \
-b ${config.system.build.diskoImages}/${disk.name}.qcow2 \
-F qcow2 "$tmp"/${disk.name}.qcow2
'') disks}
set +f
Expand Down
56 changes: 28 additions & 28 deletions lib/make-disk-image.nix
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
{ nixosConfig
{ config
, diskoLib
, pkgs ? nixosConfig.config.disko.imageBuilderPkgs
, lib ? pkgs.lib
, name ? "${nixosConfig.config.networking.hostName}-disko-images"
, extraPostVM ? nixosConfig.config.disko.extraPostVM
, checked ? false
, copyNixStore ? true
, testMode ? false
, extraConfig ? { }
, imageFormat ? "raw"
, lib
, extendModules
, ...
}:
let
configSupportsZfs = nixosConfig.config.boot.supportedFilesystems.zfs or false;
diskoCfg = config.disko;
cfg = diskoCfg.imageBuilder;
inherit (cfg) pkgs imageFormat;
checked = diskoCfg.checkScripts;

configSupportsZfs = config.boot.supportedFilesystems.zfs or false;
vmTools = pkgs.vmTools.override {
rootModules = [ "9p" "9pnet_virtio" "virtio_pci" "virtio_blk" ]
++ (lib.optional configSupportsZfs "zfs")
++ nixosConfig.config.disko.extraRootModules;
customQemu = nixosConfig.config.disko.imageBuilderQemu;
++ cfg.extraRootModules;
customQemu = cfg.qemu;
kernel = pkgs.aggregateModules
(with nixosConfig.config.disko.imageBuilderKernelPackages; [ kernel ]
++ lib.optional (lib.elem "zfs" nixosConfig.config.disko.extraRootModules || configSupportsZfs) zfs);
(with cfg.kernelPackages; [ kernel ]
++ lib.optional (lib.elem "zfs" cfg.extraRootModules || configSupportsZfs) zfs);
};
cleanedConfig = diskoLib.testLib.prepareDiskoConfig nixosConfig.config diskoLib.testLib.devices;
systemToInstall = nixosConfig.extendModules {
cleanedConfig = diskoLib.testLib.prepareDiskoConfig config diskoLib.testLib.devices;
systemToInstall = extendModules {
modules = [
extraConfig
cfg.extraConfig
{
disko.testMode = true;
disko.devices = lib.mkForce cleanedConfig.disko.devices;
Expand All @@ -41,9 +40,9 @@ let
nix
util-linux
findutils
] ++ nixosConfig.config.disko.extraDependencies;
] ++ cfg.extraDependencies;
preVM = ''
${lib.concatMapStringsSep "\n" (disk: "${pkgs.qemu}/bin/qemu-img create -f ${imageFormat} ${disk.name}.${imageFormat} ${disk.imageSize}") (lib.attrValues nixosConfig.config.disko.devices.disk)}
${lib.concatMapStringsSep "\n" (disk: "${pkgs.qemu}/bin/qemu-img create -f ${imageFormat} ${disk.name}.${imageFormat} ${disk.imageSize}") (lib.attrValues diskoCfg.devices.disk)}
# This makes disko work, when canTouchEfiVariables is set to true.
# Technically these boot entries will no be persisted this way, but
# in most cases this is OK, because we can rely on the standard location for UEFI executables.
Expand All @@ -52,8 +51,8 @@ let
postVM = ''
# shellcheck disable=SC2154
mkdir -p "$out"
${lib.concatMapStringsSep "\n" (disk: "mv ${disk.name}.${imageFormat} \"$out\"/${disk.name}.${imageFormat}") (lib.attrValues nixosConfig.config.disko.devices.disk)}
${extraPostVM}
${lib.concatMapStringsSep "\n" (disk: "mv ${disk.name}.${imageFormat} \"$out\"/${disk.name}.${imageFormat}") (lib.attrValues diskoCfg.devices.disk)}
${cfg.extraPostVM}
'';

closureInfo = pkgs.closureInfo {
Expand All @@ -76,13 +75,13 @@ let
udevadm trigger --action=add
udevadm settle

${lib.optionalString testMode ''
${lib.optionalString diskoCfg.testMode ''
export IN_DISKO_TEST=1
''}
${systemToInstall.config.system.build.diskoScript}
'';

installer = lib.optionalString copyNixStore ''
installer = lib.optionalString cfg.copyNixStore ''
# populate nix db, so nixos-install doesn't complain
export NIX_STATE_DIR=${systemToInstall.config.disko.rootMountPoint}/nix/var/nix
nix-store --load-db < "${closureInfo}/registration"
Expand All @@ -102,17 +101,18 @@ let
(disk:
"-drive file=${disk.name}.${imageFormat},if=virtio,cache=unsafe,werror=report,format=${imageFormat}"
)
(lib.attrValues nixosConfig.config.disko.devices.disk));
(lib.attrValues diskoCfg.devices.disk));
in
{
pure = vmTools.runInLinuxVM (pkgs.runCommand name
system.build.diskoImages = vmTools.runInLinuxVM (pkgs.runCommand cfg.name
{
buildInputs = dependencies;
inherit preVM postVM QEMU_OPTS;
memSize = nixosConfig.config.disko.memSize;
inherit (diskoCfg) memSize;
}
(partitioner + installer));
impure = diskoLib.writeCheckedBash { inherit checked pkgs; } name ''

system.build.diskoImagesScript = diskoLib.writeCheckedBash { inherit checked pkgs; } cfg.name ''
set -efu
export PATH=${lib.makeBinPath dependencies}
showUsage() {
Expand Down
2 changes: 1 addition & 1 deletion lib/types/disk.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
imageSize = lib.mkOption {
type = lib.types.strMatching "[0-9]+[KMGTP]?";
description = ''
size of the image if the makeDiskImages function from diksoLib is used.
size of the image when disko images are created
is used as an argument to "qemu-img create ..."
'';
default = "2G";
Expand Down
Loading