summaryrefslogtreecommitdiff
path: root/pkgs/os-specific/linux/hyperv-daemons/default.nix
blob: 4637b5cae272753f82ed464972c46bf4735d6efe (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{
  stdenv,
  lib,
  python3,
  kernel,
  makeWrapper,
  writeText,
  gawk,
  iproute2,
}:

let
  libexec = "libexec/hypervkvpd";

  fcopy_name =
    if lib.versionOlder kernel.version "6.10" then
      "fcopy"
    else
      # The fcopy program is explicitly left out in the Makefile on aarch64
      (if stdenv.hostPlatform.isAarch64 then null else "fcopy_uio");

  daemons = stdenv.mkDerivation {
    pname = "hyperv-daemons-bin";
    inherit (kernel) src version;

    nativeBuildInputs = [ makeWrapper ];
    buildInputs = [ python3 ];

    postPatch = ''
      cd tools/hv
      substituteInPlace hv_kvp_daemon.c \
        --replace /usr/libexec/hypervkvpd/ $out/${libexec}/
    '';

    makeFlags = [
      "ARCH=${stdenv.hostPlatform.parsed.cpu.name}"
      "DESTDIR=$(out)"
      "sbindir=/bin"
      "libexecdir=/libexec"
    ];

    postFixup = ''
      wrapProgram $out/bin/hv_kvp_daemon \
        --prefix PATH : $out/bin:${
          lib.makeBinPath [
            gawk
            iproute2
          ]
        }
    '';
  };

  service =
    bin: title: check:
    writeText "hv-${bin}.service" ''
      [Unit]
      Description=Hyper-V ${title} daemon
      ConditionVirtualization=microsoft
      ${lib.optionalString (check != "") ''
        ConditionPathExists=/dev/vmbus/${check}
      ''}
      [Service]
      ExecStart=@out@/hv_${bin}_daemon -n
      Restart=on-failure
      PrivateTmp=true
      Slice=hyperv.slice

      [Install]
      WantedBy=hyperv-daemons.target
    '';

in
stdenv.mkDerivation {
  pname = "hyperv-daemons";
  inherit (kernel) version;

  # we just stick the bins into out as well as it requires "out"
  outputs = [
    "bin"
    "lib"
    "out"
  ];

  buildInputs = [ daemons ];
  passthru = {
    inherit daemons;
  };

  buildCommand = ''
    system=$lib/lib/systemd/system

    ${lib.optionalString (fcopy_name != null) ''
      install -Dm444 ${
        service fcopy_name "file copy (FCOPY)"
          "/sys/bus/vmbus/devices/eb765408-105f-49b6-b4aa-c123b64d17d4/uio"
      } $system/hv-fcopy.service
    ''}
    install -Dm444 ${service "kvp" "key-value pair (KVP)" "hv_kvp"} $system/hv-kvp.service
    install -Dm444 ${service "vss" "volume shadow copy (VSS)" "hv_vss"} $system/hv-vss.service

    cat > $system/hyperv-daemons.target <<EOF
    [Unit]
    Description=Hyper-V Daemons
    Wants=hv-kvp.service hv-vss.service
    ${lib.optionalString (fcopy_name != null) ''
      Wants=hv-fcopy.service
    ''}
    EOF

    for f in $lib/lib/systemd/system/*.service ; do
      substituteInPlace $f --replace @out@ ${daemons}/bin
    done

    # we need to do both $out and $bin as $out is required
    for d in $out/bin $bin/bin ; do
      # make user binaries available
      mkdir -p $d
      ln -s ${daemons}/bin/lsvmbus $d/lsvmbus
    done
  '';

  meta = {
    description = "Integration Services for running NixOS under HyperV";
    mainProgram = "lsvmbus";
    longDescription = ''
      This packages contains the daemons that are used by the Hyper-V hypervisor
      on the host.

      Microsoft calls their guest agents "Integration Services" which is why
      we use that name here.
    '';
    homepage = "https://kernel.org";
    maintainers = with lib.maintainers; [ peterhoeg ];
    platforms = kernel.meta.platforms;
  };
}