summaryrefslogtreecommitdiffstats
path: root/nixos/modules/virtualisation/hyperv-guest.nix
blob: 9161cacdd400b3a6318b7acb3c7fb41fb47c4cf5 (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
{
  config,
  lib,
  pkgs,
  ...
}:

with lib;

let
  cfg = config.virtualisation.hypervGuest;

in
{
  imports = [
    (mkRemovedOptionModule [
      "virtualisation"
      "hypervGuest"
      "videoMode"
    ] "The video mode can now be configured via standard tools, or in Hyper-V VM settings.")
  ];

  options = {
    virtualisation.hypervGuest = {
      enable = mkEnableOption "Hyper-V Guest Support";
    };
  };

  config = mkIf cfg.enable {
    boot = {
      initrd.kernelModules = [
        "hv_balloon"
        "hv_netvsc"
        "hv_storvsc"
        "hv_utils"
        "hv_vmbus"
      ];

      initrd.availableKernelModules = [ "hyperv_keyboard" ];

      kernelParams = [
        "elevator=noop"
      ];
    };

    environment.systemPackages = [ config.boot.kernelPackages.hyperv-daemons.bin ];

    # enable hotadding cpu/memory
    services.udev.packages = lib.singleton (
      pkgs.writeTextFile {
        name = "hyperv-cpu-and-memory-hotadd-udev-rules";
        destination = "/etc/udev/rules.d/99-hyperv-cpu-and-memory-hotadd.rules";
        text = ''
          # Memory hotadd
          SUBSYSTEM=="memory", ACTION=="add", DEVPATH=="/devices/system/memory/memory[0-9]*", TEST=="state", ATTR{state}="online"

          # CPU hotadd
          SUBSYSTEM=="cpu", ACTION=="add", DEVPATH=="/devices/system/cpu/cpu[0-9]*", TEST=="online", ATTR{online}="1"
        '';
      }
    );

    systemd = {
      packages = [ config.boot.kernelPackages.hyperv-daemons.lib ];

      targets.hyperv-daemons = {
        wantedBy = [ "multi-user.target" ];
      };
    };
  };
}