Subject: [PATCH] services: base: Add file->udev-rule function.

From: Maxim Cournoyer <[email-masked]>
Date: Fri, 29 Sep 2017 11:58:58 -0400

Hello,

While experimenting with udev rules, I found a need to be able to pass
file-like objects containing udev rules to the udev-service. This patch
implements a `file->udev-rule' method that does just that.

>From 050e96b3325f851f3118de0c881d25796d76049b Mon Sep 17 00:00:00 2001
From: Maxim Cournoyer <[email-masked]>
Date: Wed, 27 Sep 2017 21:33:25 -0400
Subject: [PATCH] services: base: Add file->udev-rule function.

This function allows passing a file-like object to the udev service.

* gnu/services/base.scm (file->udev-rule): New function.
* doc/guix.texi (Base Services): Document it.
---
 doc/guix.texi         | 15 ++++++++++++---
 gnu/services/base.scm | 17 +++++++++++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index fff3fbd5f..a073dccb5 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -9707,11 +9707,20 @@ Return a service that runs the Guix build daemon according to
 @var{config}.
 @end deffn
 
-@deffn {Scheme Procedure} udev-service [#:udev udev]
+@deffn {Scheme Procedure} udev-service [#:udev @var{eudev} #:rules @code{'()}]
 Run @var{udev}, which populates the @file{/dev} directory dynamically.
+Packages can be included in the @var{rules} list in order to extend the
+udev rules with the definitions found under their
+@file{lib/udev/rules.d} sub-directory.
+
+The functions @code{udev-rules} and @code{file->udev-rules} from
+@code{(gnu services base)} can be used to create rule objects based on a
+string or a file-like object, respectively. Those rule objects can be
+passed to udev-service just like packages.
+
 @end deffn
 
-@deffn {Scheme Procedure} urandom-seed-service @var{#f}
+@deffn {Scheme Procedure} urandom-seed-service
 Save some entropy in @var{%random-seed-file} to seed @file{/dev/urandom}
 when rebooting.
 @end deffn
@@ -9823,7 +9832,7 @@ to add @var{device} to the kernel's entropy pool.  The service will fail if
 @cindex session limits
 @cindex ulimit
 @cindex priority
-@deffn {Scheme Procedure} pam-limits-service [#:limits @var{limits}]
+@deffn {Scheme Procedure} pam-limits-service [#:limits @code{'()}]
 
 Return a service that installs a configuration file for the
 @uref{http://linux-pam.org/Linux-PAM-html/sag-pam_limits.html,
diff --git a/gnu/services/base.scm b/gnu/services/base.scm
index 64620a9b0..711167a2f 100644
--- a/gnu/services/base.scm
+++ b/gnu/services/base.scm
@@ -71,6 +71,7 @@
             udev-service-type
             udev-service
             udev-rule
+            file->udev-rule
 
             login-configuration
             login-configuration?
@@ -1628,6 +1629,22 @@ item of @var{packages}."
                          (lambda (port)
                            (display #$contents port)))))))
 
+(define (file->udev-rule file-name file)
+  "Return a directory with a udev rule file FILE-NAME which is a copy of FILE."
+  (computed-file file-name
+                 (with-imported-modules '((guix build utils))
+                   #~(begin
+                       (use-modules (guix build utils))
+
+                       (define rules.d
+                         (string-append #$output "/lib/udev/rules.d"))
+
+                       (define file-copy-dest
+                         (string-append rules.d "/" #$file-name))
+
+                       (mkdir-p rules.d)
+                       (copy-file #$file file-copy-dest)))))
+
 (define kvm-udev-rule
   ;; Return a directory with a udev rule that changes the group of /dev/kvm to
   ;; "kvm" and makes it #o660.  Apparently QEMU-KVM used to ship this rule,
-- 
2.14.1

Below is an operating-system declaration that makes use of it.

Thanks,

Maxim