Move let bindings to the top

This commit is contained in:
Manuel Thalmann 2024-05-01 02:07:12 +02:00
parent 8bb98177cf
commit 1ea8734a9c

View file

@ -1,4 +1,53 @@
{ config, lib, options, pkgs, ... }: { { config, lib, options, pkgs, ... }:
let
packageName = "custom-nixos-vm";
mergedSystemOption =
with options.system;
lib.mergeDefinitions
build.loc
build.type
(lib.lists.forEach
(
builtins.filter
(item:
!(lib.path.hasPrefix ./. (/. + item.file)))
build.definitionsWithLocations)
(item: { inherit (item) file value; }));
vanillaVM = mergedSystemOption.mergedValue.vm;
createVMWrapper =
vm:
if (vm.name == packageName)
then
vm
else
let
prefix =
lib.concatStringsSep " " (
lib.optionals config.virtualisation.runAsRoot (
["sudo"] ++
lib.optional config.virtualisation.qemu.runInBackground "-b"));
suffix =
lib.concatStringsSep " " (
lib.optional
(!config.virtualisation.runAsRoot && config.virtualisation.qemu.runInBackground)
"&");
wrapped = pkgs.writeShellApplication {
name = "run-${config.system.name}-vm";
text = ''
${prefix} ${vm}/bin/run-${config.system.name}-vm ${suffix}
'';
};
in
pkgs.symlinkJoin {
name = packageName;
paths = [ wrapped ];
};
in {
options = options =
let let
vmVariantOptions = { vmVariantOptions = {
@ -57,54 +106,7 @@
# Replace native `qemu` with `remote-viewer` # Replace native `qemu` with `remote-viewer`
system.build = system.build =
{ {
vm = vm = lib.mkForce (createVMWrapper vanillaVM);
let
packageName = "custom-nixos-vm";
mergedSystem =
with options.system;
lib.mergeDefinitions
build.loc
build.type
(lib.lists.forEach
(
builtins.filter
(item:
!(lib.path.hasPrefix ./. (/. + item.file)))
build.definitionsWithLocations)
(item: { inherit (item) file value; }));
in
lib.mkForce (
with { inherit (mergedSystem.mergedValue) vm; };
if (vm.name == packageName)
then
vm
else
let
prefix =
lib.concatStringsSep " " (
lib.optionals config.virtualisation.runAsRoot (
["sudo"] ++
lib.optional config.virtualisation.qemu.runInBackground "-b"));
suffix =
lib.concatStringsSep " " (
lib.optional
(!config.virtualisation.runAsRoot && config.virtualisation.qemu.runInBackground)
"&");
wrapped = pkgs.writeShellApplication {
name = "run-${config.system.name}-vm";
text = ''
${prefix} ${vm}/bin/run-${config.system.name}-vm ${suffix}
'';
};
in
# Rename package to `nixos-vm`
pkgs.symlinkJoin {
name = packageName;
paths = [ wrapped ];
});
}; };
}; };
} }