summaryrefslogtreecommitdiff
path: root/nonguix/transformations.scm
blob: 603cfb562d0a003052b334325bde6dd5cef85377 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
;;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Copyright © 2025 Hilton Chain <hako@ultrarare.space>

(define-module (nonguix transformations)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (guix channels)
  #:use-module (guix diagnostics)
  #:use-module (guix gexp)
  #:use-module (guix i18n)
  #:use-module (guix packages)
  #:use-module (guix utils)
  #:use-module (guix build-system trivial)
  #:use-module (gnu system)
  #:use-module (nongnu system linux-initrd)
  #:use-module (gnu services)
  #:use-module (gnu services base)
  #:use-module (gnu services xorg)
  #:use-module (nongnu services nvidia)
  #:use-module (gnu packages base)
  #:use-module (gnu packages package-management)
  #:use-module (nongnu packages linux)
  #:use-module (nongnu packages nvidia)
  #:export (nonguix-transformation-guix
            nonguix-transformation-linux
            nonguix-transformation-nvidia)
  #:re-export (replace-mesa))

(define* (nonguix-transformation-guix #:key (substitutes? #t)
                                      (channel? #t)
                                      (guix-source? #f))
  "Return a procedure that transforms an operating system, setting up Nonguix
signing key for the Guix daemon.

Additionally, SUBSTITUTES? (default: #t) sets up the substitute server,
CHANNEL? (default: #t) adds Nonguix channel specification into
'/etc/guix/channels.scm' and GUIX-SOURCE? (default: #f) builds Nonguix channel
into the default Guix.

FIXME: GUIX-SOURCE? is disabled by default due to performance issue."

  (define %nonguix-signing-key
    (plain-file "nonguix.pub" "
(public-key
 (ecc
  (curve Ed25519)
  (q #C1FD53E5D4CE971933EC50C9F307AE2171A2D3B52C804642A7A35F84F3A4EA98#)))"))

  (define %nonguix-channel
    (channel
      (name 'nonguix)
      (url "https://gitlab.com/nonguix/nonguix")
      ;; Enable signature verification:
      (introduction
       (make-channel-introduction
        "897c1a470da759236cc11798f4e0a5f7d4d59fbc"
        (openpgp-fingerprint
         "2A39 3FFF 68F4 EF7A 3D29  12AF 6F51 20A0 22FB B2D5")))))

  (lambda (os)
    (operating-system
      (inherit os)
      (services
       (modify-services (operating-system-user-services os)
         (guix-service-type
          config => (guix-configuration
                     (inherit config)
                     (channels
                      (let ((configured-channels
                             (guix-configuration-channels config)))
                        (if channel?
                            (cons %nonguix-channel
                                  (or configured-channels %default-channels))
                            configured-channels)))
                     (guix
                      (if guix-source?
                          (guix-for-channels channels)
                          (guix-configuration-guix config)))
                     (authorized-keys
                      (cons %nonguix-signing-key
                            (guix-configuration-authorized-keys config)))
                     (substitute-urls
                      (delete-duplicates
                       `(,@(guix-configuration-substitute-urls config)
                         ,@(if substitutes?
                               '("https://substitutes.nonguix.org")
                               '())))))))))))

(define* (nonguix-transformation-linux #:key (linux linux)
                                       (firmware (list linux-firmware))
                                       (initrd microcode-initrd))
  "Return a procedure that transforms an operating system, setting up
LINUX (default: linux) kernel, with FIRMWARE (default: (list linux-firmware))
and INITRD (default: microcode-initrd)."
  (lambda (os)
    (operating-system
      (inherit os)
      (kernel linux)
      (firmware
       (delete-duplicates
        (append firmware
                (operating-system-firmware os))))
      (initrd initrd))))

(define* (nonguix-transformation-nvidia #:key (driver nvda)
                                        (open-source-kernel-module? #f)
                                        (kernel-mode-setting? #t)
                                        (configure-xorg? #f)
                                        (remove-nvenc-restriction? #f)
                                        ;; Deprecated.
                                        (s0ix-power-management? #f))
  "Return a procedure that transforms an operating system, setting up DRIVER
(default: nvda) for NVIDIA GPU.

OPEN-SOURCE-KERNEL-MODULE? (default: #f) is supported since Turing and required
since Blackwell.

KERNEL-MODE-SETTING? (default: #t) is required for Wayland and rootless Xorg
support.

CONFIGURE-XORG? (default: #f) is required for Xorg display managers.  It accepts
a display manager service type, or #t when using '%desktop-services'.

REMOVE-NVENC-RESTRICTION? (default: #f) applies patches from
<https://github.com/keylase/nvidia-patch>."

  (define* (remove-restriction #:key nvenc nvfbc)
    (lambda (pkg)
      (if (and (or nvenc nvfbc)
               (target-x86-64?))
          (package
            (inherit pkg)
            (build-system trivial-build-system)
            (arguments
             (list
              #:builder
              (with-imported-modules '((guix build utils))
                #~(begin
                    (use-modules (guix build utils))

                    (define (patch-lib patch)
                      (lambda (lib)
                        (when patch
                          (invoke #$(file-append sed "/bin/sed")
                                  "--in-place" patch lib))))

                    (copy-recursively #$pkg #$output)
                    (with-directory-excursion (in-vicinity #$output "lib")
                      (for-each
                       (patch-lib #$nvenc)
                       (find-files "." "libnvidia-encode\\.so"))
                      (for-each
                       (patch-lib #$nvfbc)
                       (find-files "." "libnvidia-fbc\\.so")))))))
            (native-inputs '())
            (inputs '())
            (propagated-inputs '()))
          pkg)))

  (define %driver
    (or (assoc-ref
         `((,nvda-beta
            . ,((if remove-nvenc-restriction?
                   (remove-restriction
                    #:nvenc "s/\\xe8\\x51\\x21\\xfe\\xff\\x41\\x89\\xc6\\x85\\xc0/\\xe8\\x51\\x21\\xfe\\xff\\x29\\xc0\\x41\\x89\\xc6/g"
                    #:nvfbc "s/\\x85\\xc0\\x0f\\x85\\xd4\\x00\\x00\\x00\\x48/\\x85\\xc0\\x90\\x90\\x90\\x90\\x90\\x90\\x48/g")
                   identity)
                driver))
           (,nvda-590
            . ,((if remove-nvenc-restriction?
                    (remove-restriction
                     #:nvenc "s/\\xe8\\x41\\x2e\\xfe\\xff\\x41\\x89\\xc6\\x85\\xc0/\\xe8\\x41\\x2e\\xfe\\xff\\x29\\xc0\\x41\\x89\\xc6/g"
                     #:nvfbc "s/\\x85\\xc0\\x0f\\x85\\xd4\\x00\\x00\\x00\\x48/\\x85\\xc0\\x90\\x90\\x90\\x90\\x90\\x90\\x48/g")
                    identity)
                driver))
           (,nvda-580
            . ,((if remove-nvenc-restriction?
                    (remove-restriction
                     #:nvenc "s/\\xe8\\x81\\x2e\\xfe\\xff\\x41\\x89\\xc6\\x85\\xc0/\\xe8\\x81\\x2e\\xfe\\xff\\x29\\xc0\\x41\\x89\\xc6/g"
                     #:nvfbc "s/\\x85\\xc0\\x0f\\x85\\xd4\\x00\\x00\\x00\\x48/\\x85\\xc0\\x90\\x90\\x90\\x90\\x90\\x90\\x48/g")
                    identity)
                driver))
           (,nvda-470
            . ,((if remove-nvenc-restriction?
                    (remove-restriction
                     #:nvenc "s/\\xe8\\x55\\x1a\\xff\\xff\\x85\\xc0\\x41\\x89\\xc4/\\xe8\\x55\\x1a\\xff\\xff\\x29\\xc0\\x41\\x89\\xc4/g"
                     #:nvfbc "s/\\x83\\xfe\\x01\\x73\\x08\\x48/\\x83\\xfe\\x00\\x72\\x08\\x48/g")
                    identity)
                driver))
           (,nvda-390
            . ,((if remove-nvenc-restriction?
                    (remove-restriction
                     #:nvenc "s/\\x85\\xC0\\x89\\xC5\\x75\\x18/\\x29\\xC0\\x89\\xC5\\x90\\x90/g"
                     #:nvfbc #f)
                    identity)
                driver)))
         driver)
        (leave (G_ "'~a': no driver configuration available for '~a'~%")
               "nonguix-transformation-nvidia"
               driver)))

  (define %firmware
    (assoc-ref
     `((,nvda-beta . ,nvidia-firmware-beta)
       (,nvda-590  . ,nvidia-firmware-590)
       (,nvda-580  . ,nvidia-firmware-580)
       (,nvda-470  . ,nvidia-firmware-470))
     driver))

  (define %module
    (assoc-ref
     `((,nvda-beta . ,(if open-source-kernel-module?
                          nvidia-module-open-beta
                          nvidia-module-beta))
       (,nvda-590  . ,(if open-source-kernel-module?
                          nvidia-module-open-590
                          nvidia-module-590))
       (,nvda-580  . ,(if open-source-kernel-module?
                          nvidia-module-open-580
                          nvidia-module-580))
       (,nvda-470  . ,nvidia-module-470)
       (,nvda-390  . ,nvidia-module-390))
     driver))

  (define %modprobe
    (assoc-ref
     `((,nvda-beta . ,nvidia-modprobe-beta)
       (,nvda-590  . ,nvidia-modprobe-590)
       (,nvda-580  . ,nvidia-modprobe-580)
       (,nvda-470  . ,nvidia-modprobe-470)
       (,nvda-390  . ,nvidia-modprobe-390))
     driver))

  (define %settings
    (and configure-xorg?
         (assoc-ref
          `((,nvda-beta . ,nvidia-settings-beta)
            (,nvda-590  . ,nvidia-settings-590)
            (,nvda-580  . ,nvidia-settings-580)
            (,nvda-470  . ,nvidia-settings-470)
            (,nvda-390  . ,nvidia-settings-390))
          driver)))

  (define %xorg-extension
    (and=> configure-xorg?
           (match-lambda
             (#t
              (set-xorg-configuration
               (xorg-configuration
                 (modules (list %driver)))))
             (display-manager
              (set-xorg-configuration
               (xorg-configuration
                 (modules (list %driver)))
               display-manager)))))

  (lambda (os)
    (operating-system
      (inherit os)
      (kernel-arguments
       `("modprobe.blacklist=nouveau"
         "modprobe.blacklist=nova_core,nova_drm"
         ,@(if s0ix-power-management?
               (begin
                 ;; 2026-03
                 (warning
                  (G_ "'~a': argument '~a' is deprecated, the transformation \
won't add kernel arguments other than the minimum necessary in the future.~%")
                  "nonguix-transformation-nvidia"
                  "#:s0ix-power-management?")
                 '("mem_sleep_default=s2idle"
                   "nvidia.NVreg_EnableS0ixPowerManagement=1"))
               '())
         ,(if kernel-mode-setting?
              "nvidia_drm.modeset=1"
              "nvidia_drm.modeset=0")
         ,@(operating-system-user-kernel-arguments os)))
      (packages
       (replace-mesa (operating-system-packages os) #:driver %driver))
      (services
       (replace-mesa
        `(,(service nvidia-service-type
             (nvidia-configuration
               (driver %driver)
               (firmware %firmware)
               (module %module)
               (modprobe %modprobe)
               (settings %settings)))
          ,@(if configure-xorg?
                (list %xorg-extension)
                '())
          ,@(operating-system-user-services os))
        #:driver %driver)))))