1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 defaultUserGroup = "usbmux";
8 apple = "05ac";
9
10 cfg = config.services.usbmuxd;
11
12in
13
14{
15 options.services.usbmuxd = {
16
17 enable = mkOption {
18 type = types.bool;
19 default = false;
20 description = lib.mdDoc ''
21 Enable the usbmuxd ("USB multiplexing daemon") service. This daemon is
22 in charge of multiplexing connections over USB to an iOS device. This is
23 needed for transferring data from and to iOS devices (see ifuse). Also
24 this may enable plug-n-play tethering for iPhones.
25 '';
26 };
27
28 user = mkOption {
29 type = types.str;
30 default = defaultUserGroup;
31 description = lib.mdDoc ''
32 The user usbmuxd should use to run after startup.
33 '';
34 };
35
36 group = mkOption {
37 type = types.str;
38 default = defaultUserGroup;
39 description = lib.mdDoc ''
40 The group usbmuxd should use to run after startup.
41 '';
42 };
43
44 package = mkOption {
45 type = types.package;
46 default = pkgs.usbmuxd;
47 defaultText = literalExpression "pkgs.usbmuxd";
48 description = lib.mdDoc "Which package to use for the usbmuxd daemon.";
49 relatedPackages = [ "usbmuxd" "usbmuxd2" ];
50 };
51
52 };
53
54 config = mkIf cfg.enable {
55
56 users.users = optionalAttrs (cfg.user == defaultUserGroup) {
57 ${cfg.user} = {
58 description = "usbmuxd user";
59 group = cfg.group;
60 isSystemUser = true;
61 };
62 };
63
64 users.groups = optionalAttrs (cfg.group == defaultUserGroup) {
65 ${cfg.group} = { };
66 };
67
68 # Give usbmuxd permission for Apple devices
69 services.udev.extraRules = ''
70 SUBSYSTEM=="usb", ATTR{idVendor}=="${apple}", GROUP="${cfg.group}"
71 '';
72
73 systemd.services.usbmuxd = {
74 description = "usbmuxd";
75 wantedBy = [ "multi-user.target" ];
76 unitConfig.Documentation = "man:usbmuxd(8)";
77 serviceConfig = {
78 # Trigger the udev rule manually. This doesn't require replugging the
79 # device when first enabling the option to get it to work
80 ExecStartPre = "${pkgs.udev}/bin/udevadm trigger -s usb -a idVendor=${apple}";
81 ExecStart = "${cfg.package}/bin/usbmuxd -U ${cfg.user} -v";
82 };
83 };
84
85 };
86}