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.
wfvm
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; ) services;
# If we are running in impure mode we can also enable networked services sshSetupCommands = let
impureSetupCommands = let
makeDirs = lib.mapAttrsToList (n: v: ''mkdir C:\Users\${n}\.ssh'') users; makeDirs = lib.mapAttrsToList (n: v: ''mkdir C:\Users\${n}\.ssh'') users;
writeKeys = lib.flatten (lib.mapAttrsToList (n: v: builtins.map (key: let writeKeys = lib.flatten (lib.mapAttrsToList (n: v: builtins.map (key: let
commands = [ 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); in lib.concatStringsSep "\n" commands) (v.sshKeys or [])) users);
mkDirsDesc = builtins.map (c: {Path = c; Description = "Make SSH key dir";}) makeDirs; mkDirsDesc = builtins.map (c: {Path = c; Description = "Make SSH key dir";}) makeDirs;
writeKeysDesc = builtins.map (c: {Path = c; Description = "Add SSH key";}) writeKeys; writeKeysDesc = builtins.map (c: {Path = c; Description = "Add SSH key";}) writeKeys;
in mkDirsDesc ++ writeKeysDesc ++ [ 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"; Description = "Local powershell repo import";
} }
{ {
@ -81,7 +78,12 @@ let
} }
] ]
++ setupCommands ++ setupCommands
++ impureSetupCommands ++ [
{
Path = ''powershell.exe F:\ssh-setup.ps1'';
Description = "Setup SSH and keys";
}
]
++ serviceCommands ++ serviceCommands
++ impureShellCommands ++ impureShellCommands
); );
@ -290,8 +292,23 @@ let
</unattend> </unattend>
''; '';
in in {
# Lint and format as a sanity check # Lint and format as a sanity check
pkgs.runCommandNoCC "autounattend.xml" {} '' autounattendXML = pkgs.runCommandNoCC "autounattend.xml" {} ''
${pkgs.libxml2}/bin/xmllint --format ${autounattendXML} > $out ${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"; name = "windows-test-runner";
# Dummy sources # Dummy sources
src = pkgs.runCommandNoCC "" {} "touch $out"; src = pkgs.runCommandNoCC "dummy" {} "touch $out";
dontUnpack = true;
propagatedBuildInputs = qemu.inputs; propagatedBuildInputs = qemu.inputs;
dontBuild = true; dontBuild = true;

View File

@ -81,6 +81,10 @@ let
mkdir pkgs/files mkdir pkgs/files
cp ${bundleInstaller} pkgs/"$(stripHash "${bundleInstaller}")" 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 ${nuget} pkgs/bootstrap/00_"$(stripHash "${nuget}")"
cp ${opensshutils} pkgs/bootstrap/01_"$(stripHash "${opensshutils}")" cp ${opensshutils} pkgs/bootstrap/01_"$(stripHash "${opensshutils}")"
cp ${anaconda} pkgs/bootstrap/02_"$(stripHash "${anaconda}")" cp ${anaconda} pkgs/bootstrap/02_"$(stripHash "${anaconda}")"
@ -133,7 +137,9 @@ let
mkdir -p win mkdir -p win
mkdir -p win/nix-win mkdir -p win/nix-win
7z x -y ${windowsIso} -owin 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 virt-make-fs --partition --type=fat win/ usbimage.img
rm -rf win rm -rf win