2018-06-30 19:42:31 +00:00
|
|
|
#!/bin/bash
|
2018-06-30 19:49:12 +00:00
|
|
|
|
|
|
|
APPVM_PATH=$(dirname $(realpath $0))
|
|
|
|
cd ${APPVM_PATH}
|
|
|
|
|
2018-07-01 06:56:16 +00:00
|
|
|
if [ ! -f nix/local.nix ]; then
|
2018-07-01 07:14:12 +00:00
|
|
|
echo "[*] There is no local.nix, creating."
|
2018-07-01 06:56:16 +00:00
|
|
|
echo -e "{\n}" >> nix/local.nix
|
|
|
|
fi
|
|
|
|
|
2018-06-30 19:42:31 +00:00
|
|
|
if [[ "$1" == "build" && "$2" != "" ]]; then
|
2018-07-01 07:14:12 +00:00
|
|
|
if [ -f bin/appvm.${2} ]; then
|
|
|
|
echo "[*] Kill app."
|
|
|
|
pkill -f "$(cat bin/appvm.${2} | grep pgrep | awk '{ print $3 }')"
|
|
|
|
fi
|
|
|
|
if [ -f qemu/qcow2/${2}.qcow2 ]; then
|
|
|
|
echo "[*] Remove old app state."
|
|
|
|
rm qemu/qcow2/${2}.qcow2
|
|
|
|
fi
|
2018-06-30 19:42:31 +00:00
|
|
|
NIX_PATH=$NIX_PATH:. nix-build '<nixpkgs/nixos>' -A config.system.build.vm -I nixos-config=nix/${2}.nix || exit 1
|
|
|
|
NIX_SYSTEM=$(realpath result/system)
|
|
|
|
mkdir -p bin
|
2018-06-30 20:24:44 +00:00
|
|
|
RAND_HASH=$(head /dev/urandom | md5sum | awk '{ print $1 }')
|
|
|
|
VM_BIN_PATH=$(realpath qemu/bin/qemu.${RAND_HASH}.${2})
|
|
|
|
sed "s;NIX_SYSTEM_PLACEHOLDER;${NIX_SYSTEM};" qemu/qemu.template > ${VM_BIN_PATH}
|
|
|
|
sed -i "s;NAME_PLACEHOLDER;${2};" ${VM_BIN_PATH}
|
2018-07-01 07:14:12 +00:00
|
|
|
sed -i "s;HASH_PLACEHOLDER;${RAND_HASH};" ${VM_BIN_PATH}
|
2018-06-30 20:24:44 +00:00
|
|
|
sed -i "s;NIX_DISK_IMAGE_PLACEHOLDER;${APPVM_PATH}/qemu/qcow2/${2}.qcow2;" ${VM_BIN_PATH}
|
2018-06-30 20:01:23 +00:00
|
|
|
RANDOM_PORT=$(/usr/bin/python -c 'import random; print(random.randint(1024,65535))')
|
|
|
|
# TODO Check for port collisions
|
2018-06-30 20:24:44 +00:00
|
|
|
sed -i "s;PORT_PLACEHOLDER;${RANDOM_PORT};" ${VM_BIN_PATH}
|
|
|
|
echo -e "#!/bin/bash\npgrep -f ${RAND_HASH} || {\n\tnohup setsid ${VM_BIN_PATH} >/dev/null 2>&1 &\n\tsleep 1s\n}\nremote-viewer -f spice://127.200.0.1:${RANDOM_PORT}" > bin/appvm.${2}
|
|
|
|
chmod +x ${VM_BIN_PATH}
|
|
|
|
chmod +x bin/appvm.${2}
|
2018-06-30 19:42:31 +00:00
|
|
|
unlink result
|
2018-06-30 21:56:43 +00:00
|
|
|
elif [[ "$1" == "generate-resolution" && "$2" != "" && "$3" != "" ]]; then
|
|
|
|
MONITOR_SIZE="$(xrandr | grep mm | head -n 1 | awk '{ print $(NF-2) " " $(NF) }' | sed 's/mm//g')"
|
|
|
|
CVT="$(cvt ${2} ${3} | grep Modeline)"
|
|
|
|
echo "{"
|
|
|
|
echo " services.xserver.monitorSection = ''"
|
|
|
|
echo " " ${CVT}
|
|
|
|
echo " " Option '"PreferredMode"' $(echo ${CVT} | awk '{ print $2 }')
|
|
|
|
echo " " DisplaySize ${MONITOR_SIZE} # In millimeters
|
|
|
|
echo " '';"
|
|
|
|
echo "}"
|
2018-06-30 19:42:31 +00:00
|
|
|
else
|
2018-06-30 21:56:43 +00:00
|
|
|
echo -e "Usage:\t$0 build APPLICATION"
|
|
|
|
echo -e "or:\t$0 generate-resolution X Y"
|
2018-06-30 19:42:31 +00:00
|
|
|
fi
|