From 1ea8734a9c498a255d7d62358fe9ce244c06d68e Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 1 May 2024 02:07:12 +0200 Subject: [PATCH] Move `let` bindings to the top --- lib/config/custom-build-vm.nix | 100 +++++++++++++++++---------------- 1 file changed, 51 insertions(+), 49 deletions(-) diff --git a/lib/config/custom-build-vm.nix b/lib/config/custom-build-vm.nix index ebc0cbe..9acddbf 100644 --- a/lib/config/custom-build-vm.nix +++ b/lib/config/custom-build-vm.nix @@ -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 = let vmVariantOptions = { @@ -57,54 +106,7 @@ # Replace native `qemu` with `remote-viewer` system.build = { - vm = - 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 ]; - }); + vm = lib.mkForce (createVMWrapper vanillaVM); }; }; }