Subject: [PATCH 1/3] gnu: Add make-linux-module.

From: Danny Milosavljevic <[email-masked]>
Date: Tue, 2 Apr 2019 21:28:53 +0200

* gnu/packages/linux.scm (make-linux-module): New procedure.
---
 gnu/packages/linux.scm | 66 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/gnu/packages/linux.scm b/gnu/packages/linux.scm
index 9e4261eb02..55a314258f 100644
--- a/gnu/packages/linux.scm
+++ b/gnu/packages/linux.scm
@@ -415,6 +415,72 @@ for ARCH and optionally VARIANT, or #f if there is no such configuration."
 It has been modified to remove all non-free binary blobs.")
     (license license:gpl2)))
 
+;; FIXME: Remove CONFIG_MODULE_SRCVERSION_ALL=y from our configs.
+(define (make-linux-module linux module)
+  "Given a LINUX package and a MODULE package, build MODULE using LINUX."
+  (let ((linux-source
+         (package
+           (inherit linux)
+           (name "linux-source")
+           (arguments
+             (substitute-keyword-arguments (package-arguments linux)
+              ((#:phases phases)
+               `(modify-phases ,phases
+                  (replace 'build
+                    (lambda _
+                      (invoke "make" "modules_prepare")))
+                  (delete 'strip) ; faster.
+                  (replace 'install
+                    (lambda* (#:key outputs #:allow-other-keys)
+                      (let* ((out (assoc-ref outputs "out"))
+                             (out-lib-build (string-append out "/lib/modules/build")))
+                        ; TODO: Only preserve the minimum, i.e. [Kbuild], Kconfig, scripts, include, ".config".
+                        (copy-recursively "." out-lib-build)
+                        #t))))))))))
+    (package
+      (inherit module)
+      (name (string-append (package-name module) "-" (package-name linux)))
+      (native-inputs
+       `(("linux-source" ,linux-source)
+         ("kmod" ,kmod)
+         ;("elfutils" ,elfutils)  ; Needed to enable CONFIG_STACK_VALIDATION
+         ("gcc" ,gcc-7)
+         ,@(package-native-inputs module)))
+      (arguments
+        (substitute-keyword-arguments
+         (default-keyword-arguments (package-arguments module)
+          `(#:phases #f
+            #:make-flags '()))
+         ((#:phases phases)
+          `(modify-phases ,phases
+             (replace 'build
+               (lambda* (#:key inputs make-flags #:allow-other-keys)
+                 (apply invoke "make" "-C"
+                        (string-append (assoc-ref inputs "linux-source")
+                                       "/lib/modules/build")
+                        (string-append "M=" (getcwd))
+                        make-flags)))
+         ;; This block was copied from make-linux-libre--only took the
+         ;; "modules_install" part.
+         (replace 'install
+           (lambda* (#:key inputs native-inputs outputs #:allow-other-keys)
+             (let* ((out    (assoc-ref outputs "out"))
+                    (moddir (string-append out "/lib/modules"))
+                    (kmod   (assoc-ref (or native-inputs inputs) "kmod")))
+               ;; Install kernel modules
+               (mkdir-p moddir)
+               (invoke "make"
+                       "-C"
+                       (string-append (assoc-ref inputs "linux-source")
+                                      "/lib/modules/build")
+                       (string-append "M=" (getcwd))
+                       (string-append "DEPMOD=" kmod "/bin/depmod")
+                       (string-append "MODULE_DIR=" moddir)
+                       (string-append "INSTALL_PATH=" out)
+                       (string-append "INSTALL_MOD_PATH=" out)
+                       "INSTALL_MOD_STRIP=1"
+                       "modules_install")))))))))))
+
 (define %linux-libre-version "5.0.5")
 (define %linux-libre-hash "1yivxqprxfzhzid4qv9hpnb5i38kijrj2g2pyzz7niliya1c58li")