1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfgFile = pkgs.writeText "reader.conf" config.services.pcscd.readerConfig;
7
8 package = if config.security.polkit.enable
9 then pkgs.pcscliteWithPolkit
10 else pkgs.pcsclite;
11
12 pluginEnv = pkgs.buildEnv {
13 name = "pcscd-plugins";
14 paths = map (p: "${p}/pcsc/drivers") config.services.pcscd.plugins;
15 };
16
17in
18{
19
20 ###### interface
21
22 options.services.pcscd = {
23 enable = mkEnableOption (lib.mdDoc "PCSC-Lite daemon");
24
25 plugins = mkOption {
26 type = types.listOf types.package;
27 default = [ pkgs.ccid ];
28 defaultText = literalExpression "[ pkgs.ccid ]";
29 example = literalExpression "[ pkgs.pcsc-cyberjack ]";
30 description = lib.mdDoc "Plugin packages to be used for PCSC-Lite.";
31 };
32
33 readerConfig = mkOption {
34 type = types.lines;
35 default = "";
36 example = ''
37 FRIENDLYNAME "Some serial reader"
38 DEVICENAME /dev/ttyS0
39 LIBPATH /path/to/serial_reader.so
40 CHANNELID 1
41 '';
42 description = lib.mdDoc ''
43 Configuration for devices that aren't hotpluggable.
44
45 See {manpage}`reader.conf(5)` for valid options.
46 '';
47 };
48 };
49
50 ###### implementation
51
52 config = mkIf config.services.pcscd.enable {
53
54 environment.etc."reader.conf".source = cfgFile;
55
56 environment.systemPackages = [ package ];
57 systemd.packages = [ (getBin package) ];
58
59 systemd.sockets.pcscd.wantedBy = [ "sockets.target" ];
60
61 systemd.services.pcscd = {
62 environment.PCSCLITE_HP_DROPDIR = pluginEnv;
63 restartTriggers = [ "/etc/reader.conf" ];
64
65 # If the cfgFile is empty and not specified (in which case the default
66 # /etc/reader.conf is assumed), pcscd will happily start going through the
67 # entire confdir (/etc in our case) looking for a config file and try to
68 # parse everything it finds. Doesn't take a lot of imagination to see how
69 # well that works. It really shouldn't do that to begin with, but to work
70 # around it, we force the path to the cfgFile.
71 #
72 # https://github.com/NixOS/nixpkgs/issues/121088
73 serviceConfig.ExecStart = [ "" "${getBin package}/bin/pcscd -f -x -c ${cfgFile}" ];
74 };
75 };
76}