1 { config, lib, pkgs, ... }:
6 cfg = config.services.jack;
8 pcmPlugin = cfg.jackd.enable && cfg.alsa.enable;
9 loopback = cfg.jackd.enable && cfg.loopback.enable;
11 enable32BitAlsaPlugins = cfg.alsa.support32Bit && pkgs.stdenv.isx86_64 && pkgs.pkgsi686Linux.alsa-lib != null;
13 umaskNeeded = versionOlder cfg.jackd.package.version "1.9.12";
14 bridgeNeeded = versionAtLeast cfg.jackd.package.version "1.9.12";
19 enable = mkEnableOption (lib.mdDoc ''
20 JACK Audio Connection Kit. You need to add yourself to the "jackaudio" group
24 # until jack1 promiscuous mode is fixed
28 defaultText = literalExpression "pkgs.jack2";
29 example = literalExpression "pkgs.jack1";
30 description = lib.mdDoc ''
31 The JACK package to use.
35 extraOptions = mkOption {
36 type = types.listOf types.str;
40 example = literalExpression ''
41 [ "-dalsa" "--device" "hw:1" ];
43 description = lib.mdDoc ''
44 Specifies startup command line arguments to pass to JACK server.
50 description = lib.mdDoc ''
51 Commands to run after JACK is started.
61 description = lib.mdDoc ''
62 Route audio to/from generic ALSA-using applications using ALSA JACK PCM plugin.
66 support32Bit = mkOption {
69 description = lib.mdDoc ''
70 Whether to support sound for 32-bit ALSA applications on 64-bit system.
79 description = lib.mdDoc ''
80 Create ALSA loopback device, instead of using PCM plugin. Has broader
81 application support (things like Steam will work), but may need fine-tuning
82 for concrete hardware.
89 description = lib.mdDoc ''
90 Index of an ALSA loopback device.
96 description = lib.mdDoc ''
97 ALSA config for loopback device.
101 dmixConfig = mkOption {
108 description = lib.mdDoc ''
109 For music production software that still doesn't support JACK natively you
110 would like to put buffer/period adjustments here
111 to decrease dmix device latency.
117 description = lib.mdDoc ''
118 Additional commands to run to setup loopback device.
130 sound.extraConfig = ''
132 libs.native = ${pkgs.alsa-plugins}/lib/alsa-lib/libasound_module_pcm_jack.so ;
133 ${lib.optionalString enable32BitAlsaPlugins
134 "libs.32Bit = ${pkgs.pkgsi686Linux.alsa-plugins}/lib/alsa-lib/libasound_module_pcm_jack.so ;"}
145 boot.kernelModules = [ "snd-aloop" ];
146 boot.kernelParams = [ "snd-aloop.index=${toString cfg.loopback.index}" ];
147 sound.extraConfig = cfg.loopback.config;
150 (mkIf cfg.jackd.enable {
151 services.jack.jackd.session = ''
152 ${lib.optionalString bridgeNeeded "${pkgs.a2jmidid}/bin/a2jmidid -e &"}
154 # https://alsa.opensrc.org/Jack_and_Loopback_device_as_Alsa-to-Jack_bridge#id06
155 services.jack.loopback.config = ''
158 card ${toString cfg.loopback.index}
167 ${cfg.loopback.dmixConfig}
173 control { name Master }
177 card ${toString cfg.loopback.index}
184 card ${toString cfg.loopback.index}
190 card ${toString cfg.loopback.index}
197 playback.pcm "asoftvol"
198 capture.pcm "loophw01"
205 services.jack.loopback.session = ''
206 alsa_in -j cloop -dcloop &
207 alsa_out -j ploop -dploop &
208 while [ "$(jack_lsp cloop)" == "" ] || [ "$(jack_lsp ploop)" == "" ]; do sleep 1; done
209 jack_connect cloop:capture_1 system:playback_1
210 jack_connect cloop:capture_2 system:playback_2
211 jack_connect system:capture_1 ploop:playback_1
212 jack_connect system:capture_2 ploop:playback_2
217 assertion = !(cfg.alsa.enable && cfg.loopback.enable);
218 message = "For JACK both alsa and loopback options shouldn't be used at the same time.";
222 users.users.jackaudio = {
224 extraGroups = [ "audio" ];
225 description = "JACK Audio system service user";
228 # http://jackaudio.org/faq/linux_rt_config.html
229 security.pam.loginLimits = [
230 { domain = "@jackaudio"; type = "-"; item = "rtprio"; value = "99"; }
231 { domain = "@jackaudio"; type = "-"; item = "memlock"; value = "unlimited"; }
233 users.groups.jackaudio = {};
236 systemPackages = [ cfg.jackd.package ];
237 etc."alsa/conf.d/50-jack.conf".source = "${pkgs.alsa-plugins}/etc/alsa/conf.d/50-jack.conf";
238 variables.JACK_PROMISCUOUS_SERVER = "jackaudio";
241 services.udev.extraRules = ''
242 ACTION=="add", SUBSYSTEM=="sound", ATTRS{id}!="Loopback", TAG+="systemd", ENV{SYSTEMD_WANTS}="jack.service"
245 systemd.services.jack = {
246 description = "JACK Audio Connection Kit";
249 SupplementaryGroups = lib.optional
250 (config.hardware.pulseaudio.enable
251 && !config.hardware.pulseaudio.systemWide) "users";
252 ExecStart = "${cfg.jackd.package}/bin/jackd ${lib.escapeShellArgs cfg.jackd.extraOptions}";
254 LimitMEMLOCK = "infinity";
255 } // optionalAttrs umaskNeeded {
258 path = [ cfg.jackd.package ];
260 JACK_PROMISCUOUS_SERVER = "jackaudio";
261 JACK_NO_AUDIO_RESERVATION = "1";
263 restartIfChanged = false;
265 systemd.services.jack-session = {
266 description = "JACK session";
270 ${lib.optionalString cfg.loopback.enable cfg.loopback.session}
273 RemainAfterExit = true;
275 StateDirectory = "jack";
277 LimitMEMLOCK = "infinity";
279 path = [ cfg.jackd.package ];
281 JACK_PROMISCUOUS_SERVER = "jackaudio";
282 HOME = "/var/lib/jack";
284 wantedBy = [ "jack.service" ];
285 partOf = [ "jack.service" ];
286 after = [ "jack.service" ];
287 restartIfChanged = false;
293 meta.maintainers = [ ];