services: nvidia: Only invoke commands when NVIDIA driver is in use.

* nongnu/services/nvidia.scm (nvidia-service-type): Don't extend
kernel-module-loader-service-type, move this part to…
(nvidia-shepherd-service)[start]: …here.
Check driver and command availablity.
[stop]: Likewise.
[modules]: Add (guix build utils) for ‘invoke/quiet’.
Add (rnrs io ports) for ‘get-line’.

Signed-off-by: John Kehayias <john.kehayias@protonmail.com>
This commit is contained in:
Hilton Chain 2024-02-04 15:08:37 +08:00 committed by John Kehayias
parent 97925d2091
commit 8a56ebb666
No known key found for this signature in database
GPG key ID: 499097AE5EA815D9

View file

@ -34,10 +34,17 @@
(provision '(nvidia)) (provision '(nvidia))
(requirement '(udev)) (requirement '(udev))
(one-shot? #t) (one-shot? #t)
(modules '(((guix build utils) #:select (invoke/quiet))
((rnrs io ports) #:select (get-line))))
(start (start
#~(lambda _ #~(lambda _
(when (file-exists? #$nvidia-smi) (when (file-exists? "/proc/driver/nvidia")
(system* #$nvidia-smi)))))))) (let ((modprobe (call-with-input-file
"/proc/sys/kernel/modprobe" get-line)))
(false-if-exception
(begin
(invoke/quiet modprobe "--" "nvidia_uvm")
(invoke/quiet #$nvidia-smi)))))))))))
(define nvidia-service-type (define nvidia-service-type
(service-type (service-type
@ -52,8 +59,6 @@
(service-extension firmware-service-type (service-extension firmware-service-type
(compose list nvidia-configuration-firmware)) (compose list nvidia-configuration-firmware))
(service-extension linux-loadable-module-service-type (service-extension linux-loadable-module-service-type
(compose list nvidia-configuration-module)) (compose list nvidia-configuration-module))))
(service-extension kernel-module-loader-service-type
(const '("nvidia_uvm")))))
(default-value (nvidia-configuration)) (default-value (nvidia-configuration))
(description "Prepare system environment for NVIDIA driver."))) (description "Prepare system environment for NVIDIA driver.")))