services: Add udev-rules-service helper.

* doc/guix.texi (Base services): Add documentation for
'udev-rules-service'. Replace examples of 'udev-service-type' extension
with 'udev-rules-service'.
* gnu/services/base.scm (udev-rules-service): New procedure.

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
This commit is contained in:
Brice Waegeneire 2020-04-05 21:51:41 +02:00 committed by Ludovic Courtès
parent 6ca2d7dae0
commit 2b0c0d9301
No known key found for this signature in database
GPG key ID: 090B11993D9AEBB5
2 changed files with 45 additions and 23 deletions

View file

@ -12850,8 +12850,12 @@ A directory path where the @command{guix-daemon} will perform builds.
@deffn {Scheme Procedure} udev-service [#:udev @var{eudev} #:rules @code{'()}] @deffn {Scheme Procedure} udev-service [#:udev @var{eudev} #:rules @code{'()}]
Run @var{udev}, which populates the @file{/dev} directory dynamically. Run @var{udev}, which populates the @file{/dev} directory dynamically.
udev rules can be provided as a list of files through the @var{rules} udev rules can be provided as a list of files through the @var{rules}
variable. The procedures @code{udev-rule} and @code{file->udev-rule} from variable. The procedures @code{udev-rule}, @code{udev-rules-service}
@code{(gnu services base)} simplify the creation of such rule files. and @code{file->udev-rule} from @code{(gnu services base)} simplify the
creation of such rule files.
The @command{herd rules udev} command, as root, returns the name of the
directory containing all the active udev rules.
@end deffn @end deffn
@deffn {Scheme Procedure} udev-rule [@var{file-name} @var{contents}] @deffn {Scheme Procedure} udev-rule [@var{file-name} @var{contents}]
@ -12870,23 +12874,27 @@ upon detecting a USB device with a given product identifier.
"ATTR@{product@}==\"Example\", " "ATTR@{product@}==\"Example\", "
"RUN+=\"/path/to/script\""))) "RUN+=\"/path/to/script\"")))
@end lisp @end lisp
The @command{herd rules udev} command, as root, returns the name of the
directory containing all the active udev rules.
@end deffn @end deffn
Here we show how the default @var{udev-service} can be extended with it. @deffn {Scheme Procedure} udev-rules-service [@var{name} @var{rules}] @
[#:groups @var{groups}]
Return a service that extends @code{udev-service-type } with @var{rules}
and @code{account-service-type} with @var{groups} as system groups.
This works by creating a singleton service type
@var{name}@code{-udev-rules}, of which the returned service is an
instance.
Here we show how it can be used to extend @var{udev-service} with the
previously defined rule @code{%example-udev-rule}.
@lisp @lisp
(operating-system (operating-system
;; @dots{} ;; @dots{}
(services (services
(modify-services %desktop-services (cons* (udev-rules-service 'usb-thing %example-udev-rule)
(udev-service-type config => %desktop-services)))
(udev-configuration (inherit config)
(rules (append (udev-configuration-rules config)
(list %example-udev-rule))))))))
@end lisp @end lisp
@end deffn
@deffn {Scheme Procedure} file->udev-rule [@var{file-name} @var{file}] @deffn {Scheme Procedure} file->udev-rule [@var{file-name} @var{file}]
Return a udev file named @var{file-name} containing the rules defined Return a udev file named @var{file-name} containing the rules defined
@ -12926,7 +12934,7 @@ without root privileges. It also details how to create the
the rules defined within the @var{android-udev-rules} package. To the rules defined within the @var{android-udev-rules} package. To
create such a group, we must define it both as part of the create such a group, we must define it both as part of the
@var{supplementary-groups} of our @var{user-account} declaration, as @var{supplementary-groups} of our @var{user-account} declaration, as
well as in the @var{groups} field of the @var{operating-system} record. well as in the @var{groups} of the @var{udev-rules-service} procedure.
@lisp @lisp
(use-modules (gnu packages android) ;for android-udev-rules (use-modules (gnu packages android) ;for android-udev-rules
@ -12940,19 +12948,11 @@ well as in the @var{groups} field of the @var{operating-system} record.
(supplementary-groups (supplementary-groups
'("adbusers" ;for adb '("adbusers" ;for adb
"wheel" "netdev" "audio" "video"))))) "wheel" "netdev" "audio" "video")))))
(groups (cons (user-group (system? #t) (name "adbusers"))
%base-groups))
;; @dots{} ;; @dots{}
(services (services
(modify-services %desktop-services (cons (udev-rules-service 'android android-udev-rules
(udev-service-type #:groups '("adbusers"))
config => %desktop-services)))
(udev-configuration (inherit config)
(rules (cons android-udev-rules
(udev-configuration-rules config))))))))
@end lisp @end lisp
@defvr {Scheme Variable} urandom-seed-service-type @defvr {Scheme Variable} urandom-seed-service-type

View file

@ -12,6 +12,7 @@
;;; Copyright © 2019 John Soo <jsoo1@asu.edu> ;;; Copyright © 2019 John Soo <jsoo1@asu.edu>
;;; Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org> ;;; Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de> ;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de>
;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
;;; ;;;
;;; This file is part of GNU Guix. ;;; This file is part of GNU Guix.
;;; ;;;
@ -92,6 +93,7 @@ (define-module (gnu services base)
udev-service udev-service
udev-rule udev-rule
file->udev-rule file->udev-rule
udev-rules-service
login-configuration login-configuration
login-configuration? login-configuration?
@ -2042,6 +2044,26 @@ (define* (udev-service #:key (udev eudev/btrfs-fix) (rules '()))
(service udev-service-type (service udev-service-type
(udev-configuration (udev udev) (rules rules)))) (udev-configuration (udev udev) (rules rules))))
(define* (udev-rules-service name rules #:key (groups '()))
"Return a service that extends udev-service-type with RULES and
account-service-type with GROUPS as system groups. This works by creating a
singleton service type NAME-udev-rules, of which the returned service is an
instance."
(let* ((name (symbol-append name '-udev-rules))
(account-extension
(const (map (lambda (group)
(user-group (name group) (system? #t)))
groups)))
(udev-extension (const (list rules)))
(type (service-type
(name name)
(extensions (list
(service-extension
account-service-type account-extension)
(service-extension
udev-service-type udev-extension))))))
(service type #f)))
(define swap-service-type (define swap-service-type
(shepherd-service-type (shepherd-service-type
'swap 'swap