1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.lirc;
7in {
8
9 ###### interface
10
11 options = {
12 services.lirc = {
13
14 enable = mkEnableOption (lib.mdDoc "LIRC daemon");
15
16 options = mkOption {
17 type = types.lines;
18 example = ''
19 [lircd]
20 nodaemon = False
21 '';
22 description = lib.mdDoc "LIRC default options described in man:lircd(8) ({file}`lirc_options.conf`)";
23 };
24
25 configs = mkOption {
26 type = types.listOf types.lines;
27 description = lib.mdDoc "Configurations for lircd to load, see man:lircd.conf(5) for details ({file}`lircd.conf`)";
28 };
29
30 extraArguments = mkOption {
31 type = types.listOf types.str;
32 default = [];
33 description = lib.mdDoc "Extra arguments to lircd.";
34 };
35 };
36 };
37
38 ###### implementation
39
40 config = mkIf cfg.enable {
41
42 # Note: LIRC executables raises a warning, if lirc_options.conf do not exists
43 environment.etc."lirc/lirc_options.conf".text = cfg.options;
44
45 passthru.lirc.socket = "/run/lirc/lircd";
46
47 environment.systemPackages = [ pkgs.lirc ];
48
49 systemd.sockets.lircd = {
50 description = "LIRC daemon socket";
51 wantedBy = [ "sockets.target" ];
52 socketConfig = {
53 ListenStream = config.passthru.lirc.socket;
54 SocketUser = "lirc";
55 SocketMode = "0660";
56 };
57 };
58
59 systemd.services.lircd = let
60 configFile = pkgs.writeText "lircd.conf" (builtins.concatStringsSep "\n" cfg.configs);
61 in {
62 description = "LIRC daemon service";
63 after = [ "network.target" ];
64
65 unitConfig.Documentation = [ "man:lircd(8)" ];
66
67 serviceConfig = {
68 RuntimeDirectory = ["lirc" "lirc/lock"];
69
70 # Service runtime directory and socket share same folder.
71 # Following hacks are necessary to get everything right:
72
73 # 1. prevent socket deletion during stop and restart
74 RuntimeDirectoryPreserve = true;
75
76 # 2. fix runtime folder owner-ship, happens when socket activation
77 # creates the folder
78 PermissionsStartOnly = true;
79 ExecStartPre = [
80 "${pkgs.coreutils}/bin/chown lirc /run/lirc/"
81 ];
82
83 ExecStart = ''
84 ${pkgs.lirc}/bin/lircd --nodaemon \
85 ${escapeShellArgs cfg.extraArguments} \
86 ${configFile}
87 '';
88 User = "lirc";
89 };
90 };
91
92 users.users.lirc = {
93 uid = config.ids.uids.lirc;
94 group = "lirc";
95 description = "LIRC user for lircd";
96 };
97
98 users.groups.lirc.gid = config.ids.gids.lirc;
99 };
100}