windows: Fix stupid quoting issues in autounattend XML

The windows XML parser is shit and bails out extremely late, making
debugging this a 1+ hour ordeal for every iteration.

Let's just write out a powershell script and be done with it.
This commit is contained in:
adisbladis 2020-01-28 23:16:49 +00:00 committed by Stephan Maka
parent d90a2716bd
commit 6b4e6548e5
3 changed files with 37 additions and 13 deletions

View File

@ -34,21 +34,18 @@ let
)
) services;
# If we are running in impure mode we can also enable networked services
impureSetupCommands = let
sshSetupCommands = let
makeDirs = lib.mapAttrsToList (n: v: ''mkdir C:\Users\${n}\.ssh'') users;
writeKeys = lib.flatten (lib.mapAttrsToList (n: v: builtins.map (key: let
commands = [
''powershell.exe "echo ${key} | Out-File -FilePath C:\Users\${n}\.ssh\authorized_keys"''
''powershell.exe Set-Content -Path C:\Users\${n}\.ssh\authorized_keys -Value '${key}' ''
];
in lib.concatStringsSep "\n" commands) (v.sshKeys or [])) users);
mkDirsDesc = builtins.map (c: {Path = c; Description = "Make SSH key dir";}) makeDirs;
writeKeysDesc = builtins.map (c: {Path = c; Description = "Add SSH key";}) writeKeys;
in mkDirsDesc ++ writeKeysDesc ++ [
# TODO: Figure out how to do this step with sandboxing enabled
{
Path = ''Register-PSRepository -Name bootstraprepo -SourceLocation F:\files'';
Path = ''powershell.exe Register-PSRepository -Name bootstraprepo -SourceLocation F:\files'';
Description = "Local powershell repo import";
}
{
@ -81,7 +78,12 @@ let
}
]
++ setupCommands
++ impureSetupCommands
++ [
{
Path = ''powershell.exe F:\ssh-setup.ps1'';
Description = "Setup SSH and keys";
}
]
++ serviceCommands
++ impureShellCommands
);
@ -290,8 +292,23 @@ let
</unattend>
'';
in
in {
# Lint and format as a sanity check
pkgs.runCommandNoCC "autounattend.xml" {} ''
${pkgs.libxml2}/bin/xmllint --format ${autounattendXML} > $out
''
autounattendXML = pkgs.runCommandNoCC "autounattend.xml" {} ''
${pkgs.libxml2}/bin/xmllint --format ${autounattendXML} > $out
'';
# autounattend.xml is _super_ picky about quotes and other things
setupScript = pkgs.writeText "ssh-setup.ps1" (
''
# Setup SSH and keys
'' +
lib.concatStrings (
builtins.map (c: ''
# ${c.Description}
${c.Path}
'') sshSetupCommands
)
);
}

View File

@ -35,7 +35,8 @@ stdenv.mkDerivation {
name = "windows-test-runner";
# Dummy sources
src = pkgs.runCommandNoCC "" {} "touch $out";
src = pkgs.runCommandNoCC "dummy" {} "touch $out";
dontUnpack = true;
propagatedBuildInputs = qemu.inputs;
dontBuild = true;

View File

@ -81,6 +81,10 @@ let
mkdir pkgs/files
cp ${bundleInstaller} pkgs/"$(stripHash "${bundleInstaller}")"
# SSH setup script goes here because windows XML parser sucks
cp ${autounattend.setupScript} pkgs/ssh-setup.ps1
cp ${nuget} pkgs/bootstrap/00_"$(stripHash "${nuget}")"
cp ${opensshutils} pkgs/bootstrap/01_"$(stripHash "${opensshutils}")"
cp ${anaconda} pkgs/bootstrap/02_"$(stripHash "${anaconda}")"
@ -133,7 +137,9 @@ let
mkdir -p win
mkdir -p win/nix-win
7z x -y ${windowsIso} -owin
cp ${autounattend} win/autounattend.xml
cp ${autounattend.autounattendXML} win/autounattend.xml
virt-make-fs --partition --type=fat win/ usbimage.img
rm -rf win