2019-04-14 05:33:31 +08:00
|
|
|
{ pkgs,
|
|
|
|
diskImage,
|
|
|
|
qemuMem,
|
|
|
|
sshUser ? "user",
|
|
|
|
sshPassword ? "user",
|
|
|
|
}:
|
|
|
|
|
|
|
|
with pkgs;
|
|
|
|
|
|
|
|
let
|
2019-04-18 15:19:42 +08:00
|
|
|
qemu-img = "${qemu_kvm}/bin/qemu-img";
|
2019-04-18 23:34:09 +08:00
|
|
|
runQemu = isolateNetwork: forwardedPorts: extraArgs:
|
2019-04-14 05:33:31 +08:00
|
|
|
let
|
2019-04-15 02:52:09 +08:00
|
|
|
restrict =
|
|
|
|
if isolateNetwork
|
|
|
|
then "on"
|
|
|
|
else "off";
|
2019-04-24 03:13:55 +08:00
|
|
|
# use socat instead of `tcp:…` to allow multiple connections
|
2019-04-18 23:34:09 +08:00
|
|
|
guestfwds =
|
|
|
|
builtins.concatStringsSep ""
|
|
|
|
(map ({ listenAddr, targetAddr, port }:
|
2019-12-18 11:10:21 +08:00
|
|
|
",guestfwd=tcp:${listenAddr}:${toString port}-cmd:${socat}/bin/socat\\ -\\ tcp:${targetAddr}:${toString port}"
|
2019-04-18 23:34:09 +08:00
|
|
|
) forwardedPorts);
|
2019-04-14 05:33:31 +08:00
|
|
|
args = [
|
|
|
|
"-enable-kvm"
|
|
|
|
"-m" qemuMem
|
|
|
|
"-bios" "${OVMF.fd}/FV/OVMF.fd"
|
2019-04-18 23:34:09 +08:00
|
|
|
"-netdev" "user,id=n1,net=192.168.1.0/24,restrict=${restrict},hostfwd=tcp::2022-:22${guestfwds}"
|
2019-04-15 02:52:09 +08:00
|
|
|
"-device" "e1000,netdev=n1"
|
2019-04-14 05:33:31 +08:00
|
|
|
];
|
|
|
|
argStr = builtins.concatStringsSep " " (args ++ extraArgs);
|
2019-04-18 21:55:02 +08:00
|
|
|
in "${qemu_kvm}/bin/qemu-system-x86_64 ${argStr}";
|
2019-04-14 05:33:31 +08:00
|
|
|
|
2019-04-24 04:33:06 +08:00
|
|
|
# Pass empty config file to prevent ssh from failing to create ~/.ssh
|
2019-04-24 04:40:19 +08:00
|
|
|
sshOpts = "-F /dev/null -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile=\$TMP/known_hosts";
|
2019-11-14 07:13:03 +08:00
|
|
|
sshWithQuotes = quotes: cmd: ''
|
|
|
|
echo ssh windows ${quotes}${cmd}${quotes}
|
2019-04-14 05:33:31 +08:00
|
|
|
${sshpass}/bin/sshpass -p${sshPassword} -- \
|
|
|
|
${openssh}/bin/ssh -np 2022 ${sshOpts} \
|
|
|
|
${sshUser}@localhost \
|
2019-11-14 07:13:03 +08:00
|
|
|
${quotes}${cmd}${quotes}
|
2019-04-14 05:33:31 +08:00
|
|
|
'';
|
2019-11-14 07:13:03 +08:00
|
|
|
ssh = sshWithQuotes "'";
|
2019-04-14 05:33:31 +08:00
|
|
|
scp = src: target: ''
|
|
|
|
echo "Copy ${src} to ${target}"
|
|
|
|
${sshpass}/bin/sshpass -p${sshPassword} -- \
|
|
|
|
${openssh}/bin/scp -P 2022 ${sshOpts} \
|
|
|
|
"${src}" "${sshUser}@localhost:${target}"
|
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
{
|
2019-11-14 07:13:03 +08:00
|
|
|
inherit qemu-img runQemu ssh sshWithQuotes scp;
|
2019-04-18 15:19:42 +08:00
|
|
|
inputs = [ qemu_kvm openssh sshpass ];
|
2019-04-14 05:33:31 +08:00
|
|
|
}
|