~ruther/nixos-config

ref: 6904cf1761334d033556f106c82d79b48e6657f4 nixos-config/nixos/modules/profiles/virtualisation.nix -rw-r--r-- 2.0 KiB
6904cf17 — Frantisek Bohacek refactor: modularize the configuration 1 year, 1 day ago
                                                                                
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
{ config, lib, pkgs, ... }:

let
  cfg = config.profiles.virtualisation;
in {
  options = {
    profiles.virtualisation = {
      enable = lib.mkEnableOption "virtualisation";

      qemu.enable = lib.mkOption {
        type = lib.types.bool;
        default = true;
      };
      podman.enable = lib.mkOption {
        type = lib.types.bool;
        default = true;
      };
    };
  };

  config = lib.mkIf cfg.enable (lib.mkMerge [
    (lib.mkIf cfg.podman.enable {
      users.groups.podman.members = [ config.nixos-config.defaultUser ];

      virtualisation.podman = {
        enable = true;
        dockerCompat = true;
        defaultNetwork.settings.dns_enabled = true;
      };

      environment.systemPackages = with pkgs; [
        podman-compose
      ];
    })
    (lib.mkIf cfg.qemu.enable {
      users.groups.libvirtd.members = [ "root" config.nixos-config.defaultUser ];
      users.groups.kvm.members = [ "root" config.nixos-config.defaultUser ];

      virtualisation = {
        libvirtd = {
          enable = true;
          qemu = {
            ovmf.enable = true;
            ovmf.packages = [ pkgs.OVMFFull.fd ];
            verbatimConfig = ''
              nvram = [ "${pkgs.OVMF}/FV/OVMF.fd:${pkgs.OVMF}/FV/OVMF_VARS.fd" ]
            '';
            swtpm.enable = true;
          };
        };
        spiceUSBRedirection.enable = true;        # USB passthrough
      };

      environment = {
        etc = {
          "ovmf/edk2-x86_64-secure-code.fd" = {
            source = config.virtualisation.libvirtd.qemu.package + "/share/qemu/edk2-x86_64-secure-code.fd";
          };

          "ovmf/edk2-i386-vars.fd" = {
            source = config.virtualisation.libvirtd.qemu.package + "/share/qemu/edk2-i386-vars.fd";
          };
        };

        systemPackages = with pkgs; [
          virt-manager
          virt-viewer
          qemu
          OVMF
          gvfs # Used for shared folders between Linux and Windows
          swtpm
        ];
      };

      services = { # Enable file sharing between OS
      gvfs.enable = true;
      };
    })
  ]);

}
Do not follow this link