utils.scm (3016B)


      1 (define-module (mt utils)
      2   #:use-module (gnu home)
      3   #:use-module (gnu packages video)
      4   #:use-module (guix packages)
      5   #:use-module (guix download)
      6   #:use-module (nongnu packages game-client)
      7   #:use-module (nongnu packages video)
      8   #:use-module (nonguix utils)
      9   #:use-module (nongnu packages nvidia)
     10   #:use-module (srfi srfi-1)
     11   #:export (remote-file
     12             random-string
     13             mt-transformation-nvidia-home-environment))
     14 
     15 (define (random-string n)
     16   "Generate a random alphanumeric string of length N."
     17   (let ((chars "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))
     18     (list->string
     19      (list-tabulate n (lambda (_)
     20                         (string-ref chars (random (string-length chars))))))))
     21 
     22 (define (remote-file url hash)
     23   "Download a file from a url into the store."
     24   (origin
     25     (method url-fetch)
     26     (uri url)
     27     (file-name (basename url))
     28     (sha256 hash)))
     29 
     30 ;; TODO: Master function that detects the type of variable passed
     31 ;; (operating-system or home-environment) and returns a transformation function
     32 ;; accordingly.
     33 
     34 (define* (mt-transformation-nvidia-home-environment #:key (driver nvda))
     35   "Return a procedure that transforms a home environment, setting up DRIVER
     36 (default: nvda) for NVIDIA graphics card. If the environment variable
     37 MT_DISABLE_NVIDIA is set, then this will simply return the home environment it
     38 was passed."
     39   (if (getenv "MT_DISABLE_NVIDIA")
     40       ;; If it is set, just return the home environment.
     41       (lambda (home-env)
     42         (display "MT: MT_DISABLE_NVIDIA is set, not adjusting home environment \
     43 for NVIDIA drivers.\n"
     44                  (current-error-port))
     45         home-env)
     46       ;; If it is not set, modify the home environment accordingly.
     47       (lambda (home-env)
     48         (let* ((port (current-error-port))
     49                (packages (map (lambda (package)
     50                                 (cond
     51                                   ;; Replace specific packages with specific
     52                                   ;; NVIDIA variants.
     53                                   ((eq? package steam) steam-nvidia)
     54                                   ((eq? package obs) obs-nvidia)
     55                                   ((eq? package mpv) mpv-nvidia)
     56                                   ;; For all other packages, simply graft mesa
     57                                   ;; for DRIVER.
     58                                   (else (replace-mesa package
     59                                                       #:driver driver))))
     60                               (home-environment-packages home-env)))
     61                ;; TODO: Better understand how this works
     62                (services (replace-mesa
     63                           (home-environment-user-services home-env)
     64                           #:driver driver)))
     65            (display "MT: Adjusted home environment for NVIDIA drivers.\n" port)
     66            ;; Return modified home environment
     67            (home-environment
     68             (inherit home-env)
     69             (packages packages)
     70             (services services))))))
     71