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

DoneSubmitted by Maxim Cournoyer.
Details
2 participants
  • Ludovic Courtès
  • Maxim Cournoyer
Owner
unassigned
Severity
normal
M
M
Maxim Cournoyer wrote on 29 Sep 2017 17:58
(name . guix-patches)(address . guix-patches@gnu.org)
87lgkxxzr1.fsf@gmail.com
Hello,
While experimenting with udev rules, I found a need to be able to passfile-like objects containing udev rules to the udev-service. This patchimplements a `file->udev-rule' method that does just that.
From 050e96b3325f851f3118de0c881d25796d76049b Mon Sep 17 00:00:00 2001From: Maxim Cournoyer <maxim.cournoyer@gmail.com>Date: Wed, 27 Sep 2017 21:33:25 -0400Subject: [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(-)
Toggle diff (73 lines)diff --git a/doc/guix.texi b/doc/guix.texiindex 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.scmindex 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.
;; This is an operating system configuration template ;; for a "desktop" setup without full-blown desktop ;; environments. (use-modules (gnu) (gnu system nss) (gnu services) (gnu system shadow) ;for user-group (guix download) ;for url-fetch (guix gexp) ;for file-append (guix packages) ;for origin (guix store) ;for %default-substitute-urls (srfi srfi-1)) ;for the remove function (use-service-modules base ;for nscd-service-type desktop networking ;for wicd-service-type ssh) ;for lsh-service (use-package-modules admin ;for wpa-supplicant ratpoison certs) (define 51-android-udev.rules (let ((version "20170910")) (origin (method url-fetch) (uri (string-append "https://raw.githubusercontent.com/M0Rf30/" "android-udev-rules/" version "/51-android.rules")) (sha256 (base32 "0lmmagpyb6xsq6zcr2w1cyx9qmjqmajkvrdbhjx32gqf1d9is003"))))) (operating-system (host-name "apteryx") (timezone "America/Montreal") ;America/Los_Angeles, Asia/Tokyo (locale "en_US.UTF-8") ;; Assuming /dev/sdX is the target hard disk, and "my-root" ;; is the label of the target root file system. (bootloader (bootloader-configuration (bootloader grub-bootloader) (target "/dev/sda"))) (file-systems (cons (file-system (device "my-root") (title 'label) (mount-point "/") (type "ext4")) %base-file-systems)) (users (cons (user-account (name "maxim") (comment "Maxim Cournoyer") (group "users") (supplementary-groups '("lp" ;for bluetooth "adbusers" ;for adb "wheel" "netdev" "audio" "video")) (home-directory "/home/maxim")) %base-user-accounts)) ;; Add plugdev to %base-groups (groups (cons (user-group (system? #t) (name "adbusers")) %base-groups)) ;; Add a bunch of window managers; we can choose one at ;; the log-in screen with F1. (packages (cons* ratpoison nss-certs ;for HTTPS access %base-packages)) ;; Use the "desktop" services, which include the X11 ;; log-in service and more. (services (cons* (bluetooth-service #:auto-enable? #t) (modify-services %desktop-services ;; Add a couple extra substitute servers. (guix-service-type config => (guix-configuration (inherit config) (substitute-urls (cons* "https://bayfront.guixsd.org" "https://berlin.guixsd.org" %default-substitute-urls)))) ;; Enable using adb as a simple user with a multitude of devices. (udev-service-type config => (udev-configuration (inherit config) (rules (cons* (file->udev-rule "51-android-udev.rules" 51-android-udev.rules) (udev-configuration-rules config)))))))) ;; Allow resolution of '.local' host names with mDNS. (name-service-switch %mdns-host-lookup-nss))
Thanks,
Maxim
L
L
Ludovic Courtès wrote on 3 Oct 2017 15:18
(name . Maxim Cournoyer)(address . maxim.cournoyer@gmail.com)(address . 28647@debbugs.gnu.org)
87bmlo74jq.fsf@gnu.org
Hi Maxim,
Maxim Cournoyer <maxim.cournoyer@gmail.com> skribis:
Toggle quote (4 lines)> 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.
Looks useful!
Toggle quote (10 lines)> From 050e96b3325f851f3118de0c881d25796d76049b Mon Sep 17 00:00:00 2001> From: Maxim Cournoyer <maxim.cournoyer@gmail.com>> 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.
[...]
Toggle quote (12 lines)> -@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.
Could you document the procedures with @deffn so that they have an entryin the index?
@deffn {Scheme Procedure} udev-rules @var{rules} @end deffn
@deffn {Scheme Procedure} file->udev-rules @var{files} Similar to @code{udev-rules}, but… @end deffn
TIA!
Ludo’.
L
L
Ludovic Courtès wrote on 3 Oct 2017 15:20
(name . Maxim Cournoyer)(address . maxim.cournoyer@gmail.com)(address . 28647@debbugs.gnu.org)
87377074gs.fsf@gnu.org
Also, could you add an example in guix.texi that shows the use of theAndroid udev rules that you submitted separately?
Thanks,Ludo’.
L
L
Ludovic Courtès wrote on 23 Oct 2017 00:16
(name . Maxim Cournoyer)(address . maxim.cournoyer@gmail.com)(address . 28647@debbugs.gnu.org)
87wp3mygjn.fsf@gnu.org
Hey Maxim,
Just a friendly reminder. :-)
ludo@gnu.org (Ludovic Courtès) skribis:
Toggle quote (49 lines)> Hi Maxim,>> Maxim Cournoyer <maxim.cournoyer@gmail.com> skribis:>>> 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.>> Looks useful!>>> From 050e96b3325f851f3118de0c881d25796d76049b Mon Sep 17 00:00:00 2001>> From: Maxim Cournoyer <maxim.cournoyer@gmail.com>>> 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.>> [...]>>> -@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.>> Could you document the procedures with @deffn so that they have an entry> in the index?>> @deffn {Scheme Procedure} udev-rules @var{rules}> …> @end deffn>> @deffn {Scheme Procedure} file->udev-rules @var{files}> Similar to @code{udev-rules}, but…> @end deffn>> TIA!>> Ludo’.
ludo@gnu.org (Ludovic Courtès) skribis:
Toggle quote (5 lines)> Also, could you add an example in guix.texi that shows the use of the> Android udev rules that you submitted separately?>> Thanks,> Ludo’.
M
M
Maxim Cournoyer wrote on 23 Oct 2017 03:53
Re: [bug#28647] [PATCHv2] services: base: Add file->udev-rule function.
(name . Ludovic Courtès)(address . ludo@gnu.org)(address . 28647@debbugs.gnu.org)
87mv4i3a07.fsf_-_@gmail.com
Hi Ludovic, and sorry for the delayed reply!
ludo@gnu.org (Ludovic Courtès) writes:
Toggle quote (46 lines)> Hi Maxim,>> Maxim Cournoyer <maxim.cournoyer@gmail.com> skribis:>>> 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.>> Looks useful!>>> From 050e96b3325f851f3118de0c881d25796d76049b Mon Sep 17 00:00:00 2001>> From: Maxim Cournoyer <maxim.cournoyer@gmail.com>>> 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.>> [...]>>> -@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.>> Could you document the procedures with @deffn so that they have an entry> in the index?>> @deffn {Scheme Procedure} udev-rules @var{rules}> …> @end deffn>> @deffn {Scheme Procedure} file->udev-rules @var{files}> Similar to @code{udev-rules}, but…> @end deffn>
I'm hoping the attached patch addresses the requested additions. Thanksfor the review and comments.
Maxim
From d2da854ad8d9d0d588c646e439ebfb7ef1c21f7c Mon Sep 17 00:00:00 2001From: Maxim Cournoyer <maxim.cournoyer@gmail.com>Date: Wed, 27 Sep 2017 21:33:25 -0400Subject: [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 | 113 ++++++++++++++++++++++++++++++++++++++++++-------- gnu/services/base.scm | 17 ++++++++ 2 files changed, 112 insertions(+), 18 deletions(-)
Toggle diff (180 lines)diff --git a/doc/guix.texi b/doc/guix.texiindex b7f4f88f9..832ce8ac0 100644--- a/doc/guix.texi+++ b/doc/guix.texi@@ -9790,35 +9790,112 @@ Return a service that runs the Guix build daemon according to @var{config}. @end deffn -@cindex udev-service-@cindex udev-rule-@deffn {Scheme Procedure} udev-service [#:udev @var{udev}] [#:rules @var{'()}]+@deffn {Scheme Procedure} udev-service [#:udev @var{eudev} #:rules @code{'()}] Run @var{udev}, which populates the @file{/dev} directory dynamically.-Additional udev rules can be provided as a list of files through the-@var{rules} variable. The procedure @var{udev-rule} simplifies the-creation of these rule files.+udev rules can be provided as a list of files through the @var{rules}+variable. The procedures @var{udev-rule} and @var{file->udev-rule} from+@code{(gnu services base)} simplify the creation of such rule files.++@deffn {Scheme Procedure} udev-rule [@var{file-name} @var{contents}]+Return a udev-rule file named @var{file-name} containing the rules+defined by the @var{contents} literal. In the following example, a rule for a USB device is defined to be-stored in the file @file{90-usb-thing.rules}, and the default-@var{udev-service} is extended with it. The rule runs a script upon-detecting a USB device with a given product identifier.+stored in the file @file{90-usb-thing.rules}. The rule runs a script+upon detecting a USB device with a given product identifier. @example (define %example-udev-rule- (udev-rule "90-usb-thing.rules"- "ACTION==\"add\", SUBSYSTEM==\"usb\", ATTR@{product@}==\"Example\", RUN+=\"/path/to/script\""))+ (udev-rule+ "90-usb-thing.rules"+ (string-append "ACTION==\"add\", SUBSYSTEM==\"usb\", "+ "ATTR@{product@}==\"Example\", "+ "RUN+=\"/path/to/script\"")))+@end example+@end deffn++Here we show how the default @var{udev-service} can be extended with it.++@example+(operating-system+ ;; @dots{}+ (services+ (modify-services %desktop-services+ (udev-service-type config =>+ (udev-configuration (inherit config)+ (rules (append (udev-configuration-rules config)+ (list %example-udev-rule))))))))+@end example++@deffn {Scheme Procedure} file->udev-rule [@var{file-name} @var{file}]+Return a udev file named @var{file-name} containing the rules defined+within @var{file}, a file-like object.++The following example showcases how we can use an existing rule file.++@example+(use-modules (guix download) ;for url-fetch+ (guix packages) ;for origin+ ;; @dots{})++(define %android-udev-rules+ (file->udev-rule+ "51-android-udev.rules"+ (let ((version "20170910"))+ (origin+ (method url-fetch)+ (uri (string-append "https://raw.githubusercontent.com/M0Rf30/"+ "android-udev-rules/" version "/51-android.rules"))+ (sha256+ (base32 "0lmmagpyb6xsq6zcr2w1cyx9qmjqmajkvrdbhjx32gqf1d9is003"))))))+@end example+@end deffn++Additionally, Guix package definitions can be included in @var{rules} in+order to extend the udev rules with the definitions found under their+@file{lib/udev/rules.d} sub-directory. In lieu of the previous+@var{file->udev-rule} example, we could have used the+@var{android-udev-rules} package which exists in Guix in the @code{(gnu+packages android)} module.++The following example shows how to use the @var{android-udev-rules}+package so that the Android tool @command{adb} can detect devices+without root privileges. It also details how to create the+@code{adbusers} group, which is required for the proper functioning of+the rules defined within the @var{android-udev-rules} package. To+create such a group, we must define it both as part of the+@var{supplementary-groups} of our @var{user-account} declaration, as+well as in the @var{groups} field of the @var{operating-system} record.++@example+(use-modules (gnu packages android) ;for android-udev-rules+ (gnu system shadow) ;for user-group+ ;; @dots{}) (operating-system ;; @dots{}- (services (modify-services %desktop-services- (udev-service-type config =>- (udev-configuration (inherit config)- (rules (append (udev-configuration-rules config)- (list %example-udev-rule))))))))+ (users (cons (user-acount+ ;; @dots{}+ (supplementary-groups+ '("adbusers" ;for adb+ "wheel" "netdev" "audio" "video"))+ ;; @dots{})))++ (groups (cons (user-group (system? #t) (name "adbusers"))+ %base-groups))++ ;; @dots{}++ (services+ (modify-services %desktop-services+ (udev-service-type config =>+ (udev-configuration (inherit config)+ (rules (cons* android-udev-rules+ (udev-configuration-rules config)))))))) @end example @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@@ -9930,7 +10007,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.scmindex 541ca76f1..b605614ab 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?@@ -1630,6 +1631,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
L
L
Ludovic Courtès wrote on 23 Oct 2017 04:39
(name . Maxim Cournoyer)(address . maxim.cournoyer@gmail.com)(address . 28647@debbugs.gnu.org)
8760b6wpsz.fsf@gnu.org
Hi Maxim,
Maxim Cournoyer <maxim.cournoyer@gmail.com> skribis:
Toggle quote (3 lines)> I'm hoping the attached patch addresses the requested additions. Thanks> for the review and comments.
It does!
Toggle quote (10 lines)> From d2da854ad8d9d0d588c646e439ebfb7ef1c21f7c Mon Sep 17 00:00:00 2001> From: Maxim Cournoyer <maxim.cournoyer@gmail.com>> 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.
Applied, thanks!
Ludo’.
L
L
Ludovic Courtès wrote on 23 Oct 2017 04:39
control message for bug #28647
(address . control@debbugs.gnu.org)
874lqqwpsc.fsf@gnu.org
tags 28647 fixedclose 28647
?
Your comment

This issue is archived.

To comment on this conversation send email to 28647@debbugs.gnu.org