From 0534839f329f479088c4328263814092822c205c Mon Sep 17 00:00:00 2001 From: Jon Sahlberg Date: Thu, 13 Jun 2024 14:14:18 +0300 Subject: [PATCH] AudioVM and client configuration with separate VM audio chennels Signed-off-by: Jon Sahlberg --- modules/common/services/audio.nix | 8 +-- .../microvm/virtualization/microvm/appvm.nix | 70 ++++++++++++++++++- .../microvm/virtualization/microvm/guivm.nix | 1 + modules/reference/appvms/business.nix | 23 +----- modules/reference/appvms/chromium.nix | 22 +----- modules/reference/appvms/element.nix | 21 +----- 6 files changed, 77 insertions(+), 68 deletions(-) diff --git a/modules/common/services/audio.nix b/modules/common/services/audio.nix index fa7a88401..ef7f2bb0a 100644 --- a/modules/common/services/audio.nix +++ b/modules/common/services/audio.nix @@ -43,7 +43,7 @@ in # Enable TCP socket for VMs pulseaudio clients "server.address" = [ { - address = "tcp:4713"; + address = "tcp:${toString cfg.pulseaudioTcpPort}"; "client.access" = "unrestricted"; } ]; @@ -60,12 +60,6 @@ in }; }; - hardware.pulseaudio.extraConfig = '' - # Set sink and source default max volume to about 75% (0-65536) - set-sink-volume @DEFAULT_SINK@ 48000 - set-source-volume @DEFAULT_SOURCE@ 48000 - ''; - # Allow ghaf user to access pulseaudio and pipewire users.extraUsers.ghaf.extraGroups = [ "audio" diff --git a/modules/microvm/virtualization/microvm/appvm.nix b/modules/microvm/virtualization/microvm/appvm.nix index 18acb68e7..28b4854db 100644 --- a/modules/microvm/virtualization/microvm/appvm.nix +++ b/modules/microvm/virtualization/microvm/appvm.nix @@ -93,7 +93,64 @@ let runWaypipe pkgs.tpm2-tools pkgs.opensc - ]; + pkgs.pipewire + ] + ++ (lib.optional ( + config.ghaf.profiles.debug.enable ) + pkgs.pulseaudio ); + + security.rtkit.enable = vm.ghafAudio; + users.extraUsers.ghaf.extraGroups = + lib.optionals vm.ghafAudio ["audio" "video"]; + + services.pipewire = lib.mkIf vm.ghafAudio { + enable = true; + pulse.enable = true; + extraConfig = { + pipewire."10-remote-audio-tunnel-source" = { + "context.modules" = [ + { + name = "libpipewire-module-pulse-tunnel"; + args = { + "tunnel.mode" = "source"; + "pulse.server.address" = "tcp:audio-vm:4713"; + "reconnect.interval.ms" = "1000"; + "stream.props" = '' + "node.name" = "${vm.name}.mic" + "node.description" = "${vm.name} Microphone" + "media.icon" = "${pkgs.icon-pack}/${vm.name}.svg" + "node.autoconnect" = "true" + ''; + }; + } + ]; + }; + pipewire."11-remote-audio-tunnel-sink" = { + "context.modules" = [ + { + name = "libpipewire-module-pulse-tunnel"; + args = { + "tunnel.mode" = "sink"; + "pulse.server.address" = "tcp:audio-vm:4713"; + "reconnect.interval.ms" = "1000"; + "stream.props" = '' + "node.name" = "${vm.name}.speaker" + "node.description" = "${vm.name} Speaker" + "media.icon" = "${pkgs.icon-pack}/${vm.name}.svg" + "node.autoconnect" = "true" + ''; + }; + } + ]; + }; + }; + }; + + hardware.pulseaudio.extraConfig = '' + # Set default sink and source channels per VM + set-default-source "${vm.name}.mic" + set-default-sink "${vm.name}.speaker" + ''; security.tpm2 = { enable = true; @@ -237,6 +294,11 @@ in type = types.nullOr types.str; default = null; }; + ghafAudio = mkOption { + description = "Enable Ghaf VM Audio support"; + type = types.bool; + default = false; + }; vtpm.enable = lib.mkEnableOption "vTPM support in the virtual machine"; }; } @@ -252,6 +314,12 @@ in default = [ ]; }; + ghafAudio = mkOption { + description = "Enable Ghaf VM Audio support"; + type = types.bool; + default = false; + }; + # Base VSOCK CID which is used for auto assigning CIDs for all AppVMs # For example, when it's set to 100, AppVMs will get 100, 101, 102, etc. # It is also possible to override the auto assinged CID using the vms.cid option diff --git a/modules/microvm/virtualization/microvm/guivm.nix b/modules/microvm/virtualization/microvm/guivm.nix index f910bcd13..67f9dcbd1 100644 --- a/modules/microvm/virtualization/microvm/guivm.nix +++ b/modules/microvm/virtualization/microvm/guivm.nix @@ -100,6 +100,7 @@ let (rmDesktopEntries [ pkgs.waypipe pkgs.networkmanagerapplet + pkgs.myxer ]) ++ [ pkgs.nm-launcher diff --git a/modules/reference/appvms/business.nix b/modules/reference/appvms/business.nix index 4acf441e9..89965a1c4 100644 --- a/modules/reference/appvms/business.nix +++ b/modules/reference/appvms/business.nix @@ -32,7 +32,6 @@ in in [ pkgs.chromium - pkgs.pulseaudio pkgs.xdg-utils xdgPdfItem xdgOpenPdf @@ -46,26 +45,7 @@ in cores = 4; extraModules = [ { - imports = [ ../programs/chromium.nix ]; - # Enable pulseaudio for Chromium VM - security.rtkit.enable = true; - users.extraUsers.ghaf.extraGroups = [ - "audio" - "video" - ]; - - hardware.pulseaudio = { - enable = true; - extraConfig = '' - load-module module-tunnel-sink sink_name=chromium-speaker server=audio-vm:4713 format=s16le channels=2 rate=48000 - load-module module-tunnel-source source_name=chromium-mic server=audio-vm:4713 format=s16le channels=1 rate=48000 - - # Set sink and source default max volume to about 90% (0-65536) - set-sink-volume chromium-speaker 60000 - set-source-volume chromium-mic 60000 - ''; - }; - + imports = [../programs/chromium.nix]; time.timeZone = config.time.timeZone; microvm = { @@ -279,5 +259,6 @@ in } ]; borderColor = "#00FF00"; + ghafAudio = true; vtpm.enable = true; } diff --git a/modules/reference/appvms/chromium.nix b/modules/reference/appvms/chromium.nix index f432a555f..d7719df6d 100644 --- a/modules/reference/appvms/chromium.nix +++ b/modules/reference/appvms/chromium.nix @@ -32,7 +32,6 @@ in in [ pkgs.chromium - pkgs.pulseaudio pkgs.xdg-utils xdgPdfItem xdgOpenPdf @@ -43,26 +42,10 @@ in cores = 4; extraModules = [ { - imports = [ ../programs/chromium.nix ]; - # Enable pulseaudio for Chromium VM - security.rtkit.enable = true; - users.extraUsers.ghaf.extraGroups = [ - "audio" - "video" + imports = [ + ../programs/chromium.nix ]; - hardware.pulseaudio = { - enable = true; - extraConfig = '' - load-module module-tunnel-sink sink_name=chromium-speaker server=audio-vm:4713 format=s16le channels=2 rate=48000 - load-module module-tunnel-source source_name=chromium-mic server=audio-vm:4713 format=s16le channels=1 rate=48000 - - # Set sink and source default max volume to about 90% (0-65536) - set-sink-volume chromium-speaker 60000 - set-source-volume chromium-mic 60000 - ''; - }; - time.timeZone = config.time.timeZone; microvm.qemu.extraArgs = optionals ( @@ -83,5 +66,6 @@ in } ]; borderColor = "#630505"; + ghafAudio = true; vtpm.enable = true; } diff --git a/modules/reference/appvms/element.nix b/modules/reference/appvms/element.nix index db6a8f6ee..39b0ff2cc 100644 --- a/modules/reference/appvms/element.nix +++ b/modules/reference/appvms/element.nix @@ -24,32 +24,12 @@ in pkgs.element-gps pkgs.gpsd pkgs.tcpdump - pkgs.pulseaudio ] ++ pkgs.lib.optionals isDendritePineconeEnabled [ dendrite-pinecone ]; macAddress = "02:00:00:03:09:01"; ramMb = 4096; cores = 4; extraModules = [ { - # Enable pulseaudio for user ghaf to access mic - security.rtkit.enable = true; - users.extraUsers.ghaf.extraGroups = [ - "audio" - "video" - ]; - - hardware.pulseaudio = { - enable = true; - extraConfig = '' - load-module module-tunnel-sink sink_name=element-speaker server=audio-vm:4713 format=s16le channels=2 rate=48000 - load-module module-tunnel-source source_name=element-mic server=audio-vm:4713 format=s16le channels=1 rate=48000 - - # Set sink and source default max volume to about 90% (0-65536) - set-sink-volume element-speaker 60000 - set-source-volume element-mic 60000 - ''; - }; - systemd = { services = { element-gps = { @@ -101,4 +81,5 @@ in } ]; borderColor = "#337aff"; + ghafAudio = true; }