From b0121d7ce393f1ffeb4fcf562c2b4328fe5ef4c8 Mon Sep 17 00:00:00 2001
From: Chris Andreae <chris@andreae.gen.nz>
Date: Wed, 31 Jan 2024 20:41:33 +0900
Subject: [PATCH] Golf the zephyr modules derivation some more

---
 nix/zephyr.nix | 36 ++++++++++++++++++++++--------------
 nix/zmk.nix    |  2 +-
 2 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/nix/zephyr.nix b/nix/zephyr.nix
index 8a80a20e54fe..d7c1bd056241 100644
--- a/nix/zephyr.nix
+++ b/nix/zephyr.nix
@@ -1,31 +1,39 @@
-{ stdenv, lib, fetchgit, runCommand }:
+{ stdenvNoCC, lib, fetchgit, runCommand }:
 let
   manifestJSON = builtins.fromJSON (builtins.readFile ./manifest.json);
 
-  projects = lib.listToAttrs (lib.forEach manifestJSON ({ name, revision, url, sha256, ... }@args: (
-    lib.nameValuePair name (let
+  mkModule = { name, revision, url, sha256, ... }:
+    stdenvNoCC.mkDerivation (finalAttrs: {
+      name = "zmk-module-${name}";
+
       src = fetchgit {
         inherit name url sha256;
         rev = revision;
       };
-      module = runCommand "zephyr-module-${name}" {
-        passthru = {
-          inherit name src;
-          modulePath = "${module}/${name}";
-        };
-      } ''
+
+      dontUnpack = true;
+      dontBuild = true;
+
+      installPhase = ''
         mkdir $out
-        ln -s ${src} $out/${name}
+        ln -s ${finalAttrs.src} $out/${name}
       '';
-    in module))));
 
+      passthru = {
+        modulePath = "${finalAttrs.finalPackage}/${name}";
+      };
+   });
+
+  modules = lib.listToAttrs (lib.forEach manifestJSON ({ name, ... }@args:
+    lib.nameValuePair name (mkModule args)));
 in
 
+
 # Zephyr with no modules, from the frozen manifest.
 # For now the modules are passed through as passthru
-stdenv.mkDerivation {
+stdenvNoCC.mkDerivation {
   name = "zephyr";
-  src = projects.zephyr.src;
+  src = modules.zephyr.src;
 
   dontBuild = true;
 
@@ -39,6 +47,6 @@ stdenv.mkDerivation {
   '';
 
   passthru = {
-    modules = lib.attrValues (removeAttrs projects ["zephyr"]);
+    modules = removeAttrs modules ["zephyr"];
   };
 }
diff --git a/nix/zmk.nix b/nix/zmk.nix
index 2e363e1e8d38..7f601199c85f 100644
--- a/nix/zmk.nix
+++ b/nix/zmk.nix
@@ -49,7 +49,7 @@ let
   ];
 
   zephyrModuleDeps =
-    let modules = builtins.filter (x: builtins.elem x.name requiredZephyrModules) zephyr.modules;
+    let modules = lib.attrVals requiredZephyrModules zephyr.modules;
     in map (x: x.modulePath) modules;
 in