8 cfg = config.services.jack;
10 pcmPlugin = cfg.jackd.enable && cfg.alsa.enable;
11 loopback = cfg.jackd.enable && cfg.loopback.enable;
13 enable32BitAlsaPlugins =
14 cfg.alsa.support32Bit && pkgs.stdenv.hostPlatform.isx86_64 && pkgs.pkgsi686Linux.alsa-lib != null;
16 umaskNeeded = lib.versionOlder cfg.jackd.package.version "1.9.12";
17 bridgeNeeded = lib.versionAtLeast cfg.jackd.package.version "1.9.12";
23 enable = lib.mkEnableOption ''
24 JACK Audio Connection Kit. You need to add yourself to the "jackaudio" group
28 lib.mkPackageOption pkgs "jack2" {
32 # until jack1 promiscuous mode is fixed
36 extraOptions = lib.mkOption {
37 type = lib.types.listOf lib.types.str;
41 example = lib.literalExpression ''
42 [ "-dalsa" "--device" "hw:1" ];
45 Specifies startup command line arguments to pass to JACK server.
49 session = lib.mkOption {
50 type = lib.types.lines;
52 Commands to run after JACK is started.
59 enable = lib.mkOption {
60 type = lib.types.bool;
63 Route audio to/from generic ALSA-using applications using ALSA JACK PCM plugin.
67 support32Bit = lib.mkOption {
68 type = lib.types.bool;
71 Whether to support sound for 32-bit ALSA applications on 64-bit system.
77 enable = lib.mkOption {
78 type = lib.types.bool;
81 Create ALSA loopback device, instead of using PCM plugin. Has broader
82 application support (things like Steam will work), but may need fine-tuning
83 for concrete hardware.
87 index = lib.mkOption {
91 Index of an ALSA loopback device.
95 config = lib.mkOption {
96 type = lib.types.lines;
98 ALSA config for loopback device.
102 dmixConfig = lib.mkOption {
103 type = lib.types.lines;
110 For music production software that still doesn't support JACK natively you
111 would like to put buffer/period adjustments here
112 to decrease dmix device latency.
116 session = lib.mkOption {
117 type = lib.types.lines;
119 Additional commands to run to setup loopback device.
128 config = lib.mkMerge [
130 (lib.mkIf pcmPlugin {
131 environment.etc."alsa/conf.d/98-jack.conf".text = ''
133 libs.native = ${pkgs.alsa-plugins}/lib/alsa-lib/libasound_module_pcm_jack.so ;
134 ${lib.optionalString enable32BitAlsaPlugins "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 environment.etc."alsa/conf.d/99-jack-loopback.conf".text = cfg.loopback.config;
150 (lib.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 # https://jackaudio.org/faq/linux_rt_config.html
229 security.pam.loginLimits = [
231 domain = "@jackaudio";
237 domain = "@jackaudio";
243 users.groups.jackaudio = { };
246 systemPackages = [ cfg.jackd.package ];
247 etc."alsa/conf.d/50-jack.conf".source = "${pkgs.alsa-plugins}/etc/alsa/conf.d/50-jack.conf";
248 variables.JACK_PROMISCUOUS_SERVER = "jackaudio";
251 services.udev.extraRules = ''
252 ACTION=="add", SUBSYSTEM=="sound", ATTRS{id}!="Loopback", TAG+="systemd", ENV{SYSTEMD_WANTS}="jack.service"
255 systemd.services.jack = {
256 description = "JACK Audio Connection Kit";
260 SupplementaryGroups = lib.optional (
261 config.hardware.pulseaudio.enable && !config.hardware.pulseaudio.systemWide
263 ExecStart = "${cfg.jackd.package}/bin/jackd ${lib.escapeShellArgs cfg.jackd.extraOptions}";
265 LimitMEMLOCK = "infinity";
267 // lib.optionalAttrs umaskNeeded {
270 path = [ cfg.jackd.package ];
272 JACK_PROMISCUOUS_SERVER = "jackaudio";
273 JACK_NO_AUDIO_RESERVATION = "1";
275 restartIfChanged = false;
277 systemd.services.jack-session = {
278 description = "JACK session";
280 ${pkgs.jack-example-tools}/bin/jack_wait -w
282 ${lib.optionalString cfg.loopback.enable cfg.loopback.session}
285 RemainAfterExit = true;
287 StateDirectory = "jack";
289 LimitMEMLOCK = "infinity";
291 path = [ cfg.jackd.package ];
293 JACK_PROMISCUOUS_SERVER = "jackaudio";
294 HOME = "/var/lib/jack";
296 wantedBy = [ "jack.service" ];
297 partOf = [ "jack.service" ];
298 after = [ "jack.service" ];
299 restartIfChanged = false;
305 meta.maintainers = [ ];