From 1ff779c1a91a8d5c01f3924085f0c66c2c683d9e Mon Sep 17 00:00:00 2001
From: Arnout Engelen <arnout@bzzt.net>
Date: Tue, 17 Dec 2024 20:24:14 +0100
Subject: [PATCH] use Linux kernel sources associated with openwrt by default

---
 devices/belkin-rt3200/default.nix  |  5 -----
 devices/families/qemu.nix          |  5 -----
 devices/gl-ar750/default.nix       |  6 ------
 devices/gl-mt300a/default.nix      |  5 -----
 devices/gl-mt300n-v2/default.nix   |  5 -----
 devices/tp-archer-ax23/default.nix |  5 -----
 devices/zyxel-nwa50ax/default.nix  |  6 ------
 modules/kernel/default.nix         |  6 +++---
 pkgs/openwrt/default.nix           | 11 +++++++++++
 9 files changed, 14 insertions(+), 40 deletions(-)

diff --git a/devices/belkin-rt3200/default.nix b/devices/belkin-rt3200/default.nix
index edd6f06..4b1790d 100644
--- a/devices/belkin-rt3200/default.nix
+++ b/devices/belkin-rt3200/default.nix
@@ -55,11 +55,6 @@
       ];
       config = {
     kernel = {
-      src = pkgs.pkgsBuildBuild.fetchurl {
-        name = "linux.tar.gz";
-        url = "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.137.tar.gz";
-        hash = "sha256-PkdzUKZ0IpBiWe/RS70J76JKnBFzRblWcKlaIFNxnHQ=";
-      };
       extraPatchPhase = ''
           ${pkgs.openwrt.applyPatches.mediatek}
       '';
diff --git a/devices/families/qemu.nix b/devices/families/qemu.nix
index ce28d88..ccf054c 100644
--- a/devices/families/qemu.nix
+++ b/devices/families/qemu.nix
@@ -5,11 +5,6 @@
   ];
   config = {
     kernel = {
-      src = pkgs.pkgsBuildBuild.fetchurl {
-        name = "linux.tar.gz";
-        url = "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.137.tar.gz";
-        hash = "sha256-PkdzUKZ0IpBiWe/RS70J76JKnBFzRblWcKlaIFNxnHQ=";
-      };
       config = {
         MTD = "y";
         MTD_BLOCK = "y";
diff --git a/devices/gl-ar750/default.nix b/devices/gl-ar750/default.nix
index dd58f1d..53de152 100644
--- a/devices/gl-ar750/default.nix
+++ b/devices/gl-ar750/default.nix
@@ -157,12 +157,6 @@
         appendDTB = true;
       };
       kernel = {
-        src = pkgs.pkgsBuildBuild.fetchurl {
-          name = "linux.tar.gz";
-          url = "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.137.tar.gz";
-          hash = "sha256-PkdzUKZ0IpBiWe/RS70J76JKnBFzRblWcKlaIFNxnHQ=";
-        };
-
         # Mainline linux 5.19 doesn't have device-tree support for
         # this device or even for the SoC, so we use the extensive
         # OpenWrt kernel patches
diff --git a/devices/gl-mt300a/default.nix b/devices/gl-mt300a/default.nix
index e89723d..63d109e 100644
--- a/devices/gl-mt300a/default.nix
+++ b/devices/gl-mt300a/default.nix
@@ -114,11 +114,6 @@
      };
 
       kernel = {
-        src = pkgs.fetchurl {
-          name = "linux.tar.gz";
-          url = "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.137.tar.gz";
-          hash = "sha256-PkdzUKZ0IpBiWe/RS70J76JKnBFzRblWcKlaIFNxnHQ=";
-        };
         extraPatchPhase = ''
           ${openwrt.applyPatches.ramips}
           ${openwrt.applyPatches.rt2x00}
diff --git a/devices/gl-mt300n-v2/default.nix b/devices/gl-mt300n-v2/default.nix
index da3fbba..7bf2c43 100644
--- a/devices/gl-mt300n-v2/default.nix
+++ b/devices/gl-mt300n-v2/default.nix
@@ -125,11 +125,6 @@
       };
 
       kernel = {
-        src = pkgs.fetchurl {
-          name = "linux.tar.gz";
-          url = "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.137.tar.gz";
-          hash = "sha256-PkdzUKZ0IpBiWe/RS70J76JKnBFzRblWcKlaIFNxnHQ=";
-        };
         extraPatchPhase = ''
           ${openwrt.applyPatches.ramips}
         '';
diff --git a/devices/tp-archer-ax23/default.nix b/devices/tp-archer-ax23/default.nix
index 93e289f..d49f177 100644
--- a/devices/tp-archer-ax23/default.nix
+++ b/devices/tp-archer-ax23/default.nix
@@ -50,11 +50,6 @@
       ];
       config = {
         kernel = {
-          src = pkgs.pkgsBuildBuild.fetchurl {
-            name = "linux.tar.gz";
-            url = "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.137.tar.gz";
-            hash = "sha256-PkdzUKZ0IpBiWe/RS70J76JKnBFzRblWcKlaIFNxnHQ=";
-          };
           extraPatchPhase = ''
             ${pkgs.openwrt.applyPatches.ramips}
           '';
diff --git a/devices/zyxel-nwa50ax/default.nix b/devices/zyxel-nwa50ax/default.nix
index 920acac..2eea914 100644
--- a/devices/zyxel-nwa50ax/default.nix
+++ b/devices/zyxel-nwa50ax/default.nix
@@ -250,14 +250,8 @@
       #  IMAGE/ramboot-factory.bin := append-kernel | pad-to $$(KERNEL_SIZE) | append-ubi
 
       kernel = {
-        src = pkgs.fetchurl {
-          name = "linux.tar.gz";
-          url = "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.137.tar.gz";
-          hash = "sha256-PkdzUKZ0IpBiWe/RS70J76JKnBFzRblWcKlaIFNxnHQ=";
-        };
         extraPatchPhase = ''
           ${openwrt.applyPatches.ramips}
-
         '';
         config = {
 
diff --git a/modules/kernel/default.nix b/modules/kernel/default.nix
index 9ebb864..ae488fa 100644
--- a/modules/kernel/default.nix
+++ b/modules/kernel/default.nix
@@ -6,7 +6,7 @@
 { lib, pkgs, config, ...}:
 let
   inherit (lib) mkOption types ;
-  inherit (pkgs) liminix;
+  inherit (pkgs) liminix openwrt;
 
   mergeConditionals = conf : conditions :
     # for each key in conditions, if it is present in conf
@@ -21,8 +21,8 @@ let
 in {
   options = {
     kernel = {
-      src = mkOption { type = types.path; } ;
-      version = mkOption { type = types.str; default = "5.15.137";} ;
+      src = mkOption { type = types.path; default = openwrt.kernelSrc; } ;
+      version = mkOption { type = types.str; default = openwrt.kernelVersion;} ;
       modular = mkOption {
         type = types.bool;
         default = true;
diff --git a/pkgs/openwrt/default.nix b/pkgs/openwrt/default.nix
index 2fb16ec..7376dd8 100644
--- a/pkgs/openwrt/default.nix
+++ b/pkgs/openwrt/default.nix
@@ -29,6 +29,17 @@ let
   '';
 in {
   inherit src;
+
+  # The kernel sources typically used with this version of openwrt
+  # You can find this in `include/kernel-5.15` or similar in the
+  # openwrt sources
+  kernelSrc = pkgsBuildBuild.fetchurl {
+    name = "linux.tar.gz";
+    url = "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.137.tar.gz";
+    hash = "sha256-PkdzUKZ0IpBiWe/RS70J76JKnBFzRblWcKlaIFNxnHQ=";
+  };
+  kernelVersion = "5.15.137";
+
   applyPatches.ath79 = doPatch "ath79";
   applyPatches.ramips = doPatch "ramips";
   applyPatches.mediatek = doPatch "mediatek"; # aarch64