summary refs log tree commit diff
path: root/pkgs/build-support/vm/windows/install/default.nix
blob: 10690bf6b28778b1c4e0aac2dd7346ac1e3e30e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{ stdenv, runCommand, openssh, qemu, controller, mkCygwinImage
, writeText, dosfstools, mtools
}:

{ isoFile
, productKey
}:

let
  bootstrapAfterLogin = runCommand "bootstrap.sh" {} ''
    cat > "$out" <<EOF
    mkdir -p ~/.ssh
    cat > ~/.ssh/authorized_keys <<PUBKEY
    $(cat "${cygwinSshKey}/key.pub")
    PUBKEY
    ssh-host-config -y -c 'binmode ntsec' -w dummy
    cygrunsrv -S sshd
    shutdown -s 5
    EOF
  '';

  cygwinSshKey = stdenv.mkDerivation {
    name = "snakeoil-ssh-cygwin";
    buildCommand = ''
      ensureDir "$out"
      ${openssh}/bin/ssh-keygen -t ecdsa -f "$out/key" -N ""
    '';
  };

  sshKey = "${cygwinSshKey}/key";

  packages = [ "openssh" "shutdown" ];

  floppyCreator = import ./unattended-image.nix {
    inherit stdenv writeText dosfstools mtools;
  };

  instfloppy = floppyCreator {
    cygwinPackages = packages;
    inherit productKey;
  };

  cygiso = mkCygwinImage {
    inherit packages;
    extraContents = stdenv.lib.singleton {
      source = bootstrapAfterLogin;
      target = "bootstrap.sh";
    };
  };

  installController = controller {
    inherit sshKey;
    installMode = true;
    qemuArgs = [
      "-boot order=c,once=d"
      "-drive file=${instfloppy},readonly,index=0,if=floppy"
      "-drive file=winvm.img,index=0,media=disk"
      "-drive file=${isoFile},index=1,media=cdrom"
      "-drive file=${cygiso}/iso/cd.iso,index=2,media=cdrom"
    ];
  };

in stdenv.mkDerivation {
  name = "cygwin-base-vm";
  buildCommand = ''
    ${qemu}/bin/qemu-img create -f qcow2 winvm.img 2G
    ${installController}
    ensureDir "$out"
    cp winvm.img "$out/disk.img"
  '';
  passthru = {
    inherit sshKey;
  };
}