services: root-file-system: Cleanly unmount upon shutdown.
[jackhill/guix/guix.git] / gnu / services / security-token.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
3 ;;; Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr>
4 ;;; Copyright © 2021 Brice Waegeneire <brice@waegenei.re>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (gnu services security-token)
22 #:use-module (gnu services)
23 #:use-module (gnu services shepherd)
24 #:use-module (gnu packages admin)
25 #:use-module (gnu packages base)
26 #:use-module (gnu packages security-token)
27 #:use-module (gnu system shadow)
28 #:use-module (guix gexp)
29 #:use-module (guix modules)
30 #:use-module (guix records)
31 #:use-module (ice-9 match)
32 #:use-module (srfi srfi-26)
33 #:export (pcscd-configuration
34 pcscd-configuration?
35 pcscd-configuration-pcsc-lite
36 pcscd-configuration-usb-drivers
37 pcscd-service-type))
38
39 ;;;
40 ;;; PC/SC Smart Card Daemon
41 ;;;
42
43 (define-record-type* <pcscd-configuration>
44 pcscd-configuration make-pcscd-configuration pcscd-configuration?
45 (pcsc-lite pcscd-configuration-pcsc-lite
46 (default pcsc-lite))
47 (usb-drivers pcscd-configuration-usb-drivers
48 (default (list ccid))))
49
50 (define pcscd-shepherd-service
51 (match-lambda
52 (($ <pcscd-configuration> pcsc-lite)
53 (with-imported-modules (source-module-closure
54 '((gnu build shepherd)))
55 (shepherd-service
56 (documentation "PC/SC Smart Card Daemon")
57 (provision '(pcscd))
58 (requirement '(syslogd))
59 (modules '((gnu build shepherd)))
60 (start #~(lambda _
61 (let ((socket "/run/pcscd/pcscd.comm"))
62 (when (file-exists? socket)
63 (delete-file socket)))
64 (fork+exec-command
65 (list #$(file-append pcsc-lite "/sbin/pcscd")
66 "--foreground")
67 #:log-file "/var/log/pcscd.log")))
68 (stop #~(make-kill-destructor)))))))
69
70 (define pcscd-activation
71 (match-lambda
72 (($ <pcscd-configuration> pcsc-lite usb-drivers)
73 (with-imported-modules (source-module-closure
74 '((guix build utils)))
75 #~(begin
76 (use-modules (guix build utils))
77 ;; XXX: We can't use (guix utils) because it requires a
78 ;; dynamically-linked Guile, hence the duplicate switch-symlinks.
79 (define (switch-symlinks link target)
80 (let ((pivot (string-append link ".new")))
81 (symlink target pivot)
82 (rename-file pivot link)))
83 (mkdir-p "/var/lib")
84 (switch-symlinks "/var/lib/pcsc"
85 #$(directory-union
86 "pcsc"
87 (map (cut file-append <> "/pcsc")
88 usb-drivers))))))))
89
90 (define pcscd-service-type
91 (service-type
92 (name 'pcscd)
93 (description
94 "Run @command{pcscd}, the PC/SC smart card daemon.")
95 (extensions
96 (list (service-extension shepherd-root-service-type
97 (compose list pcscd-shepherd-service))
98 (service-extension activation-service-type
99 pcscd-activation)))
100 (default-value (pcscd-configuration))))