Skip to content

Commit

Permalink
nixos-modules/microvm: move microvm-* under microvm.binScripts
Browse files Browse the repository at this point in the history
  • Loading branch information
astro committed Nov 27, 2024
1 parent aae1277 commit 5566068
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions lib/runner.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,39 @@ let
supportsNotifySocket = hypervisorConfig.supportsNotifySocket or false;
preStart = hypervisorConfig.preStart or microvmConfig.preStart;
tapMultiQueue = hypervisorConfig.tapMultiQueue or false;
setBalloonScript = hypervisorConfig.setBalloonScript or null;

execArg = lib.optionalString microvmConfig.prettyProcnames
''-a "microvm@${hostName}"'';

runScriptBin = pkgs.writeShellScriptBin "microvm-run" ''
set -eou pipefail
${preStart}
${createVolumesScript pkgs.buildPackages microvmConfig.volumes}
${lib.optionalString (hypervisorConfig.requiresMacvtapAsFds or false) openMacvtapFds}
exec ${execArg} ${command}
'';

shutdownScriptBin = pkgs.writeShellScriptBin "microvm-shutdown" ''
${shutdownCommand}
'';

balloonScriptBin = pkgs.writeShellScriptBin "microvm-balloon" ''
set -e
if [ -z "$1" ]; then
echo "Usage: $0 <balloon-size-mb>"
exit 1
fi
SIZE=$1
${hypervisorConfig.setBalloonScript}
'';
binScripts = microvmConfig.binScripts // {
microvm-run = ''
set -eou pipefail
${preStart}
${createVolumesScript pkgs.buildPackages microvmConfig.volumes}
${lib.optionalString (hypervisorConfig.requiresMacvtapAsFds or false) openMacvtapFds}
exec ${execArg} ${command}
'';
} // lib.optionalAttrs canShutdown {
microvm-shutdown = shutdownCommand;
} // lib.optionalAttrs (setBalloonScript != null) {
microvm-balloon = ''
set -e
if [ -z "$1" ]; then
echo "Usage: $0 <balloon-size-mb>"
exit 1
fi
SIZE=$1
${setBalloonScript}
'';
};

binScriptPkgs = lib.mapAttrs (scriptName: lines:
pkgs.writeShellScriptBin "microvm-${hostName}-${scriptName}" lines
) microvmConfig.binScripts;
) binScripts;
in

pkgs.buildPackages.runCommand "microvm-${microvmConfig.hypervisor}-${hostName}"
Expand All @@ -67,14 +68,6 @@ pkgs.buildPackages.runCommand "microvm-${microvmConfig.hypervisor}-${hostName}"
} ''
mkdir -p $out/bin
ln -s ${runScriptBin}/bin/microvm-run $out/bin/microvm-run
${if canShutdown
then "ln -s ${shutdownScriptBin}/bin/microvm-shutdown $out/bin/microvm-shutdown"
else ""}
${lib.optionalString ((hypervisorConfig.setBalloonScript or null) != null) ''
ln -s ${balloonScriptBin}/bin/microvm-balloon $out/bin/microvm-balloon
''}
${lib.concatMapStrings (scriptName: ''
ln -s ${lib.getExe binScriptPkgs.${scriptName}} $out/bin/${scriptName}
'') (builtins.attrNames binScriptPkgs)}
Expand Down

0 comments on commit 5566068

Please sign in to comment.