1{ stdenv, lib, python2, python3, kernel, makeWrapper, writeText
2, gawk, iproute2 }:
3
4let
5 libexec = "libexec/hypervkvpd";
6
7 daemons = stdenv.mkDerivation rec {
8 pname = "hyperv-daemons-bin";
9 inherit (kernel) src version;
10
11 nativeBuildInputs = [ makeWrapper ];
12 buildInputs = [ (if lib.versionOlder version "4.19" then python2 else python3) ];
13
14 # as of 4.9 compilation will fail due to -Werror=format-security
15 hardeningDisable = [ "format" ];
16
17 postPatch = ''
18 cd tools/hv
19 substituteInPlace hv_kvp_daemon.c \
20 --replace /usr/libexec/hypervkvpd/ $out/${libexec}/
21 '';
22
23 # We don't actually need the hv_get_{dhcp,dns}_info scripts on NixOS in
24 # their current incarnation but with them in place, we stop the spam of
25 # errors in the log.
26 installPhase = ''
27 runHook preInstall
28
29 for f in fcopy kvp vss ; do
30 install -Dm755 hv_''${f}_daemon -t $out/bin
31 done
32
33 install -Dm755 lsvmbus $out/bin/lsvmbus
34 install -Dm755 hv_get_dhcp_info.sh $out/${libexec}/hv_get_dhcp_info
35 install -Dm755 hv_get_dns_info.sh $out/${libexec}/hv_get_dns_info
36
37 runHook postInstall
38 '';
39
40 postFixup = ''
41 wrapProgram $out/bin/hv_kvp_daemon \
42 --prefix PATH : $out/bin:${lib.makeBinPath [ gawk iproute2 ]}
43 '';
44 };
45
46 service = bin: title: check:
47 writeText "hv-${bin}.service" ''
48 [Unit]
49 Description=Hyper-V ${title} daemon
50 ConditionVirtualization=microsoft
51 ${lib.optionalString (check != "") ''
52 ConditionPathExists=/dev/vmbus/${check}
53 ''}
54 [Service]
55 ExecStart=@out@/hv_${bin}_daemon -n
56 Restart=on-failure
57 PrivateTmp=true
58 Slice=hyperv.slice
59
60 [Install]
61 WantedBy=hyperv-daemons.target
62 '';
63
64in stdenv.mkDerivation {
65 pname = "hyperv-daemons";
66 inherit (kernel) version;
67
68 # we just stick the bins into out as well as it requires "out"
69 outputs = [ "bin" "lib" "out" ];
70
71 buildInputs = [ daemons ];
72
73 buildCommand = ''
74 system=$lib/lib/systemd/system
75
76 install -Dm444 ${service "fcopy" "file copy (FCOPY)" "hv_fcopy" } $system/hv-fcopy.service
77 install -Dm444 ${service "kvp" "key-value pair (KVP)" "hv_kvp" } $system/hv-kvp.service
78 install -Dm444 ${service "vss" "volume shadow copy (VSS)" "hv_vss" } $system/hv-vss.service
79
80 cat > $system/hyperv-daemons.target <<EOF
81 [Unit]
82 Description=Hyper-V Daemons
83 Wants=hv-fcopy.service hv-kvp.service hv-vss.service
84 EOF
85
86 for f in $lib/lib/systemd/system/*.service ; do
87 substituteInPlace $f --replace @out@ ${daemons}/bin
88 done
89
90 # we need to do both $out and $bin as $out is required
91 for d in $out/bin $bin/bin ; do
92 # make user binaries available
93 mkdir -p $d
94 ln -s ${daemons}/bin/lsvmbus $d/lsvmbus
95 done
96 '';
97
98 meta = with lib; {
99 description = "Integration Services for running NixOS under HyperV";
100 longDescription = ''
101 This packages contains the daemons that are used by the Hyper-V hypervisor
102 on the host.
103
104 Microsoft calls their guest agents "Integration Services" which is why
105 we use that name here.
106 '';
107 homepage = "https://kernel.org";
108 maintainers = with maintainers; [ peterhoeg ];
109 platforms = kernel.meta.platforms;
110 };
111}