nix-scripts/artiq-fast/windows/win.nix

151 lines
4.6 KiB
Nix
Raw Normal View History

{ pkgs ? import <nixpkgs> {}
, lib ? pkgs.lib
, diskImageSize ? "22G"
, qemuMem ? "4G"
, windowsImage ? null
, autoUnattendParams ? {}
, packages ? []
, impureMode ? false
, ...
}@attrs:
let
# qemu_test is a smaller closure only building for a single system arch
qemu = pkgs.qemu_test;
libguestfs = pkgs.libguestfs-with-appliance.override {
inherit qemu;
};
runQemuCommand = name: command: (
pkgs.runCommandNoCC name { buildInputs = [ pkgs.p7zip qemu libguestfs ]; }
(
''
if ! test -f; then
echo "KVM not available, bailing out" >> /dev/stderr
exit 1
fi
'' + command
)
);
windowsIso = if windowsImage != null then windowsImage else pkgs.fetchurl {
url = "https://software-download.microsoft.com/download/sg/17763.107.101029-1455.rs5_release_svc_refresh_CLIENT_LTSC_EVAL_x64FRE_en-us.iso";
sha256 = "668fe1af70c2f7416328aee3a0bb066b12dc6bbd2576f40f812b95741e18bc3a";
};
autounattend = import ./autounattend.nix (
attrs // {
inherit pkgs;
}
);
bundleInstaller = pkgs.callPackage ./bundle {};
# Packages required to drive installation of other packages
bootstrapPkgs = let
winPkgs = import ./pkgs.nix { inherit pkgs; };
2020-01-20 08:43:44 +08:00
opensshutils = pkgs.fetchurl {
name = "opensshutils.nupkg";
url = "https://www.powershellgallery.com/api/v2/package/OpenSSHUtils/1.0.0.1";
sha256 = "11iw0wyi11v684if0nsjgaglxsqnhdn94siq27pd8h34z64l6fq3";
};
nuget = winPkgs.makePkg {
name = "nuget-dll";
src = ./nuget/Microsoft.PackageManagement.NuGetProvider.dll;
installScript = ''
mkdir -f "C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208"
cp "Microsoft.PackageManagement.NuGetProvider.dll" "C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208"
2020-01-20 08:43:44 +08:00
Import-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201
'';
};
anaconda = winPkgs.makePkg {
name = "Anaconda3";
src = pkgs.fetchurl {
name = "Anaconda3.exe";
url = "https://repo.anaconda.com/archive/Anaconda3-2019.03-Windows-x86_64.exe";
sha256 = "1f9icm5rwab6l1f23a70dw0qixzrl62wbglimip82h4zhxlh3jfj";
};
installScript = ''
Anaconda3.exe /InstallationType=AllUsers /RegisterPython=0 /S /D="C:\Program Files\\Anaconda3"
'';
};
in
runQemuCommand "bootstrap-win-pkgs.img" ''
mkdir pkgs
mkdir pkgs/bootstrap
mkdir pkgs/user
2020-01-20 08:43:44 +08:00
mkdir pkgs/files
cp ${bundleInstaller} pkgs/"$(stripHash "${bundleInstaller}")"
cp ${nuget} pkgs/bootstrap/00_"$(stripHash "${nuget}")"
cp ${opensshutils} pkgs/bootstrap/01_"$(stripHash "${opensshutils}")"
cp ${anaconda} pkgs/bootstrap/02_"$(stripHash "${anaconda}")"
${lib.concatStringsSep "\n" (builtins.map (x: ''cp ${x} pkgs/bootstrap/"$(stripHash "${x}")"'') packages)}
virt-make-fs --partition --type=fat pkgs/ $out
'';
installScript = pkgs.writeScript "windows-install-script" (
let
qemuParams = [
"-enable-kvm"
"-cpu"
"host"
"-smp"
"$NIX_BUILD_CORES"
"-m"
"${qemuMem}"
"-bios"
"${pkgs.OVMF.fd}/FV/OVMF.fd"
"-vga"
"virtio"
"-device"
"piix3-usb-uhci" # USB root hub
# USB boot
"-drive"
"id=win-install,file=usbimage.img,if=none,format=raw,readonly=on"
"-device"
"usb-storage,drive=win-install"
# Output image
"-drive"
"file=c.img,index=0,media=disk,cache=unsafe"
# "CD" drive with bootstrap pkgs
"-drive"
"id=virtio-win,file=${bootstrapPkgs},if=none,format=raw,readonly=on"
"-device"
"usb-storage,drive=virtio-win"
] ++ lib.optional (!impureMode) "-nographic";
in
''
#!${pkgs.runtimeShell}
set -exuo pipefail
export PATH=${lib.makeBinPath [ pkgs.p7zip qemu libguestfs ]}:$PATH
# Create a bootable "USB" image
# Booting in USB mode circumvents the "press any key to boot from cdrom" prompt
#
# Also embed the autounattend answer file in this image
mkdir -p win
mkdir -p win/nix-win
7z x -y ${windowsIso} -owin
cp ${autounattend} win/autounattend.xml
virt-make-fs --partition --type=fat win/ usbimage.img
rm -rf win
# Qemu requires files to be rw
qemu-img create -f qcow2 c.img ${diskImageSize}
env NIX_BUILD_CORES="''${NIX_BUILD_CORES:4}" qemu-system-x86_64 ${lib.concatStringsSep " " qemuParams}
''
);
in
if impureMode then installScript else pkgs.runCommandNoCC "windows.img" {} ''
${installScript}
mv c.img $out
''