From 20e5b7dac43fa898485b3846528c5cfbd762fd1c Mon Sep 17 00:00:00 2001
From: matt1432 <matt@nelim.org>
Date: Mon, 20 Jan 2025 20:37:52 -0500
Subject: [PATCH] feat(gen-docs): write nix code outside script

---
 apps/gen-docs/getConfigMeta.nix           | 12 ++++++++++++
 apps/gen-docs/getPackageMeta.nix          | 13 +++++++++++++
 apps/gen-docs/script.sh                   | 23 +++++------------------
 apps/gen-docs/templates/configurations.md |  2 +-
 configurations/README.md                  |  6 +++---
 5 files changed, 34 insertions(+), 22 deletions(-)
 create mode 100644 apps/gen-docs/getConfigMeta.nix
 create mode 100644 apps/gen-docs/getPackageMeta.nix

diff --git a/apps/gen-docs/getConfigMeta.nix b/apps/gen-docs/getConfigMeta.nix
new file mode 100644
index 00000000..ed3df8c4
--- /dev/null
+++ b/apps/gen-docs/getConfigMeta.nix
@@ -0,0 +1,12 @@
+configs: let
+  inherit (builtins) mapAttrs replaceStrings;
+
+  trimNewlines = s: replaceStrings ["\n"] [" "] s;
+in {
+  attrs =
+    mapAttrs (_: v: {
+      roleDesc = trimNewlines (v.config.meta.roleDescription or "");
+      hwDesc = trimNewlines (v.config.meta.hardwareDescription or "");
+    })
+    configs;
+}
diff --git a/apps/gen-docs/getPackageMeta.nix b/apps/gen-docs/getPackageMeta.nix
new file mode 100644
index 00000000..7df49ac1
--- /dev/null
+++ b/apps/gen-docs/getPackageMeta.nix
@@ -0,0 +1,13 @@
+x: let
+  inherit (builtins) currentSystem mapAttrs removeAttrs replaceStrings;
+
+  trimNewlines = s: replaceStrings ["\n"] [" "] s;
+  packages = removeAttrs x.${currentSystem} ["default"];
+in {
+  attrs =
+    mapAttrs (_: v: {
+      desc = trimNewlines (v.meta.description or "");
+      homepage = v.meta.homepage or "";
+    })
+    packages;
+}
diff --git a/apps/gen-docs/script.sh b/apps/gen-docs/script.sh
index 83bfe1e0..5c4ea475 100755
--- a/apps/gen-docs/script.sh
+++ b/apps/gen-docs/script.sh
@@ -1,29 +1,16 @@
-getMetaFunc=$(cat << EOF
-(x: {
-    attrs = builtins.mapAttrs (_: v: {
-        desc = builtins.replaceStrings ["\n"] [" "] (v.meta.description or "");
-        homepage = v.meta.homepage or "";
-        roleDesc = builtins.replaceStrings ["\n"] [" "] (v.config.meta.roleDescription or "");
-        hwDesc = builtins.replaceStrings ["\n"] [" "] (v.config.meta.hardwareDescription or "");
-    }) (builtins.removeAttrs (x.\${builtins.currentSystem} or x) ["default"]);
-})
-EOF
-)
-
-
 substitute() {
     echo '' | pandoc --metadata-file <(
         nix eval \
             --impure \
             --json \
             "$FLAKE"#"$1" \
-            --apply "$getMetaFunc" |
+            --apply "import \"$FLAKE/apps/gen-docs/$3.nix\"" |
             jq -r
     ) -t markdown --template "$FLAKE/apps/gen-docs/templates/$2.md" -o "$FLAKE/$2/README.md"
 }
 
 # TODO: add homeManagerModules, lib, modules, nixFastChecks, overlays, scopedPackages
-substitute "appsPackages" "apps"
-substitute "nixosConfigurations" "configurations"
-substitute "devShells" "devShells"
-substitute "packages" "packages"
+substitute "appsPackages" "apps" "getPackageMeta"
+substitute "nixosConfigurations" "configurations" "getConfigMeta"
+substitute "devShells" "devShells" "getPackageMeta"
+substitute "packages" "packages" "getPackageMeta"
diff --git a/apps/gen-docs/templates/configurations.md b/apps/gen-docs/templates/configurations.md
index 122a658e..0ab64ff4 100644
--- a/apps/gen-docs/templates/configurations.md
+++ b/apps/gen-docs/templates/configurations.md
@@ -3,7 +3,7 @@
 This directory contains every device's main configuration file, their `hardware-configuration.nix` and some custom modules
 unique to them.
 
-## List of my Devices
+## List of my devices
 
 | Name      | Model / Specs | Description                                                                                      |
 | --------- | ------------- | ------------------------------------------------------------------------------------------------ |
diff --git a/configurations/README.md b/configurations/README.md
index 3357e196..b49fde29 100644
--- a/configurations/README.md
+++ b/configurations/README.md
@@ -3,7 +3,7 @@
 This directory contains every device's main configuration file, their `hardware-configuration.nix` and some custom modules
 unique to them.
 
-## List of my Devices
+## List of my devices
 
 | Name      | Model / Specs | Description                                                                                      |
 | --------- | ------------- | ------------------------------------------------------------------------------------------------ |
@@ -14,6 +14,6 @@ unique to them.
 | `live-image` | USB key | Basic configuration that serves as my custom ISO target |
 | `nos` | NVIDIA 3060 with i5-3600 | Custom built NAS and seedbox for Linux ISOs ;) |
 | `servivi` | Headless Ryzen 5 3600 | Gaming PC in a previous life, it is now used as a build farm and hosts game servers |
-| `thingone` | Lenovo ThinkCentre M900 | Mini PC that makes use of [NixOS-pcsd](https://github.com/matt1432/nixos-pcsd) to form a cluster with its twin |
-| `thingtwo` | Lenovo ThinkCentre M900 | Mini PC that makes use of [NixOS-pcsd](https://github.com/matt1432/nixos-pcsd) to form a cluster with its twin |
+| `thingone` | Lenovo ThinkCentre M900 | Mini PC that makes use of [NixOS-pcsd](https://github.com/matt1432/nixos-pcsd) to form a cluster with its twin. Files located in `cluster` |
+| `thingtwo` | Lenovo ThinkCentre M900 | Mini PC that makes use of [NixOS-pcsd](https://github.com/matt1432/nixos-pcsd) to form a cluster with its twin. Files located in `cluster` |
 | `wim` | ThinkPad L13 Yoga Gen 3 (Ryzen 7 PRO 5875U) | 2-1 Lenovo Laptop that I use for university |