home

My NixOS systems configurations.
Log | Files | Refs | LICENSE

audio.nix (3647B)


      1 { config, lib, pkgs, ... }:
      2 let
      3   inherit (lib) mkEnableOption mkIf mkMerge mkOption types versionOlder;
      4   cfg = config.modules.hardware.audio;
      5   stable = versionOlder config.system.nixos.release "24.05";
      6 in
      7 {
      8   options.modules.hardware.audio = {
      9     enable = mkEnableOption "enable audio";
     10     pulseaudio = {
     11       enable = mkEnableOption "enable pulseaudio";
     12       tcp = mkOption {
     13         default = false;
     14         description = "enable pulseaudio tcp";
     15         type = types.bool;
     16       };
     17     };
     18     pipewire = {
     19       enable = mkEnableOption "enable pipewire";
     20     };
     21   };
     22   config = mkIf cfg.enable (mkMerge [
     23     {
     24       # Enable sound (alsa)
     25       # sound.enable = true;
     26       # FIXME is it needed
     27       security.pam.loginLimits = [
     28         { domain = "@audio"; item = "memlock"; type = "-"; value = "unlimited"; }
     29         { domain = "@audio"; item = "rtprio"; type = "-"; value = "99"; }
     30         { domain = "@audio"; item = "nofile"; type = "-"; value = "99999"; }
     31       ];
     32     }
     33     (mkIf cfg.pipewire.enable {
     34       security.rtkit.enable = true;
     35       services.pipewire = {
     36         enable = true;
     37         alsa.enable = true;
     38         alsa.support32Bit = true;
     39         pulse.enable = true;
     40         wireplumber = {
     41           enable = true;
     42         } // (if stable then { } else {
     43           configPackages = [
     44             (pkgs.writeTextDir "share/wireplumber/bluetooth.lua.d/51-bluez-config.lua" ''
     45               bluez_monitor.properties = {
     46                 ["bluez5.enable-sbc-xq"] = true,
     47                 ["bluez5.enable-msbc"] = true,
     48                 ["bluez5.enable-hw-volume"] = true,
     49                 ["bluez5.headset-roles"] = "[ hsp_hs hsp_ag hfp_hf hfp_ag ]"
     50               }
     51             '')
     52           ];
     53         });
     54       } // (if stable then { } else {
     55         extraConfig = {
     56           pipewire-pulse = {
     57             "50-network-party.conf" = {
     58               # "context.modules" = [
     59               #   { name = "libpipewire-module-protocol-native"; }
     60               #   { name = "libpipewire-module-client-node"; }
     61               #   { name = "libpipewire-module-adapter"; }
     62               #   { name = "libpipewire-module-metadata"; }
     63               # ];
     64               "context.exec" = [
     65                 { path = "pactl"; args = "load-module module-native-protocol-tcp"; }
     66                 { path = "pactl"; args = "load-module module-zeroconf-discover"; }
     67                 { path = "pactl"; args = "load-module module-zeroconf-publish"; }
     68               ];
     69             };
     70           };
     71         };
     72       });
     73       networking.firewall = {
     74         allowedTCPPorts = [ 6001 6002 ];
     75       };
     76     })
     77     (mkIf cfg.pulseaudio.enable {
     78       # Enable and configure pulseaudio
     79       hardware.pulseaudio = {
     80         enable = true;
     81         support32Bit = true;
     82         zeroconf.discovery.enable = true;
     83       };
     84     })
     85     (mkIf (cfg.pulseaudio.enable || cfg.pipewire.enable) {
     86       # Add extra packages
     87       environment.systemPackages = with pkgs; [
     88         apulse # allow alsa application to use pulse
     89         pavucontrol # pulseaudio volume control
     90         pasystray # systray application
     91         pulseaudioFull # pactl, etc..
     92       ];
     93     })
     94     (mkIf (cfg.pulseaudio.enable && cfg.pulseaudio.tcp) {
     95       hardware.pulseaudio = {
     96         zeroconf = {
     97           discovery.enable = cfg.tcp;
     98           publish.enable = cfg.tcp;
     99         };
    100         tcp = {
    101           enable = cfg.tcp;
    102           anonymousClients = {
    103             allowAll = true;
    104             allowedIpRanges = [ "127.0.0.1" "192.168.12.0/24" "10.0.0.0/24" ];
    105           };
    106         };
    107       };
    108       networking.firewall = {
    109         allowedTCPPorts = [ 4713 ];
    110       };
    111     })
    112   ]);
    113 }