zaphyra's git: tgcNUR

fork of https://git.transgirl.cafe/zaphoid/tgc-nix-user-repository

commit 66d8d56cca0c74492f23adeffc3b90e261836d03
parent b57136bef0aa8e30f512b6555e3f2570f06edeac
Author: Katja Ramona Sophie Kwast (zaphyra) <git@zaphyra.eu>
Date: Wed, 20 Aug 2025 22:32:15 +0200

flake: move functions to `lib`
5 files changed, 147 insertions(+), 81 deletions(-)
M
flake.nix
|
108
++++++++++++++++++++-----------------------------------------------------------
A
lib/collectModules.nix
|
40
++++++++++++++++++++++++++++++++++++++++
A
lib/default.nix
|
24
++++++++++++++++++++++++
A
lib/forAllSystems.nix
|
24
++++++++++++++++++++++++
A
lib/toCamelCase.nix
|
32
++++++++++++++++++++++++++++++++
diff --git a/flake.nix b/flake.nix
@@ -14,108 +14,54 @@
     inputs:
     (
       let
-        maintainersAttrset = import ./maintainers.nix;
         nixpkgsLib = inputs.nixpkgs.lib;
-        toCamelCase =
-          str:
-          nixpkgsLib.throwIfNot (nixpkgsLib.isString str) "toCamelCase does only accepts string values, but got ${nixpkgsLib.typeOf str}" (
-            let
-              separators = nixpkgsLib.splitStringBy (
-                prev: curr:
-                builtins.elem curr [
-                  "-"
-                  "_"
-                  " "
-                ]
-              ) false str;
 
-              parts = nixpkgsLib.flatten (
-                map (nixpkgsLib.splitStringBy (
-                  prev: curr: nixpkgsLib.match "[a-z]" prev != null && nixpkgsLib.match "[A-Z]" curr != null
-                ) true) separators
-              );
+        maintainersAttrset = import ./maintainers.nix;
 
-              first = if nixpkgsLib.length parts > 0 then nixpkgsLib.toLower (nixpkgsLib.head parts) else "";
-              rest = if nixpkgsLib.length parts > 1 then builtins.map nixpkgsLib.toSentenceCase (nixpkgsLib.tail parts) else [ ];
-            in
-            nixpkgsLib.concatStrings (map (nixpkgsLib.addContextFrom str) ([ first ] ++ rest))
-          );
-        collectModules = modulesDir: (
-          let
-            files = nixpkgsLib.pipe (nixpkgsLib.fileset.fileFilter (file: file.name == "default.nix") modulesDir) [
-              nixpkgsLib.fileset.toList
-              (builtins.map builtins.toString)
-            ];
-          in nixpkgsLib.pipe files [
-            (builtins.map (elem:
-              let
-                pathList = nixpkgsLib.pipe (nixpkgsLib.splitString "/" elem) [
-                  (nixpkgsLib.drop 4)
-                ];
-                name = nixpkgsLib.pipe pathList [
-                  (nixpkgsLib.drop 1)
-                  (nixpkgsLib.dropEnd 1)
-                  (builtins.concatStringsSep "-")
-                  toCamelCase
-                ];
-                path = ./. + (builtins.concatStringsSep "/" ([ "" ] ++ pathList));
-              in nixpkgsLib.nameValuePair name (import path)
-            ))
-            builtins.listToAttrs
-          ]
-        );
+        homeManagerModules = lib.collectModules { path = ./homeManagerModules; };
+        nixosModules = lib.collectModules { path = ./nixosModules; };
 
-        forAllSystems =
-          function:
-          nixpkgsLib.genAttrs
-            [
-              "x86_64-linux"
-              "aarch64-linux"
-            ]
-            (
-              system:
-              function (
-                import inputs.nixpkgs {
-                  system = system;
-                  overlays = builtins.attrValues inputs.self.overlays;
-                }
-              )
-            );
+        lib = import ./lib nixpkgsLib inputs;
 
       in
       {
 
-        formatter = forAllSystems (pkgs: pkgs.nixfmt-rfc-style);
+        inherit lib;
 
-        packages = forAllSystems (
-          pkgs: nixpkgsLib.packagesFromDirectoryRecursive {
-            directory = ./packages;
-            callPackage = path: scope: pkgs.callPackage path (scope // { tgcMaintainers = maintainersAttrset; });
-          }
-        );
+        formatter = lib.forAllSystems { body = (pkgs: pkgs.nixfmt-rfc-style); };
+
+        packages = lib.forAllSystems {
+          body = (
+            pkgs:
+            nixpkgsLib.packagesFromDirectoryRecursive {
+              directory = ./packages;
+              callPackage =
+                path: scope: pkgs.callPackage path (scope // { tgcMaintainers = maintainersAttrset; });
+            }
+          );
+        };
 
         overlays = {
           emacs-overlay = inputs.emacs-overlay.overlays.package;
+          lib = final: prev: {
+            lib = prev.lib.extend (_: _: { tgc = lib; });
+          };
           default = final: prev: {
             tgc = inputs.self.packages.${prev.system};
           };
         };
 
-        homeManagerModules = let
-          modules = collectModules ./homeManagerModules;
-        in modules // {
-          default = { ... }: {
-            imports = builtins.attrValues modules;
+        homeManagerModules = homeManagerModules // {
+          default = {
+            imports = builtins.attrValues homeManagerModules;
           };
         };
 
-        nixosModules = let
-          modules = collectModules ./nixosModules;
-        in modules // {
-          allModules = { ... }: {
-            imports = builtins.attrValues modules;
+        nixosModules = nixosModules // {
+          allModules = {
+            imports = builtins.attrValues nixosModules;
           };
-          allPackages = { ... }: {
+          allPackages = {
             nixpkgs.overlays = [ inputs.self.overlays.default ];
           };
           default = inputs.self.nixosModules.allPackages;
diff --git a/lib/collectModules.nix b/lib/collectModules.nix
@@ -0,0 +1,40 @@
+{ lib, self, ... }:
+
+{
+
+  collectModules =
+    {
+      path,
+      fileName ? "default.nix",
+    }:
+    (
+      let
+        files = lib.pipe (lib.fileset.fileFilter (file: file.name == fileName) path) [
+          lib.fileset.toList
+          (builtins.map builtins.toString)
+        ];
+      in
+      lib.pipe files [
+        (builtins.map (
+          elem:
+          let
+            pathList = (
+              lib.drop (lib.pipe path [
+                (lib.splitString "/")
+                (lib.length)
+              ]) (lib.splitString "/" elem)
+            );
+            name = lib.pipe pathList [
+              (lib.dropEnd 1)
+              (builtins.concatStringsSep "-")
+              self.toCamelCase
+            ];
+            filePath = path + "/${lib.concatStringsSep "/" pathList}";
+          in
+          lib.nameValuePair name (import filePath)
+        ))
+        builtins.listToAttrs
+      ]
+    );
+
+}
diff --git a/lib/default.nix b/lib/default.nix
@@ -0,0 +1,24 @@
+nixpkgsLib: inputs:
+(
+  let
+    # read the current directorys files and pipe the result through a list of functions
+    lib = nixpkgsLib.pipe (builtins.readDir ./.) [
+      # convert to a list containing just the attribute names
+      (builtins.attrNames)
+      # drop "default.nix" from the list
+      (builtins.filter (name: name != "default.nix"))
+      # convert each list-element containing its file-name to an element containing the file's content
+      (builtins.map (
+        name:
+        import ./${name} {
+          inherit inputs;
+          self = lib;
+          lib = nixpkgsLib;
+        }
+      ))
+      # merge list of attribute sets together
+      (nixpkgsLib.mergeAttrsList)
+    ];
+  in
+  lib
+)
diff --git a/lib/forAllSystems.nix b/lib/forAllSystems.nix
@@ -0,0 +1,24 @@
+{ inputs, lib, ... }:
+
+{
+
+  forAllSystems =
+    {
+      body,
+      systems ? [
+        "x86_64-linux"
+        "aarch64-linux"
+      ],
+      nixpkgs ? inputs.nixpkgs,
+      overlays ? (builtins.attrValues inputs.self.overlays),
+    }:
+    lib.genAttrs systems (
+      system:
+      body (
+        import nixpkgs {
+          inherit system overlays;
+        }
+      )
+    );
+
+}
diff --git a/lib/toCamelCase.nix b/lib/toCamelCase.nix
@@ -0,0 +1,32 @@
+{ lib, ... }:
+
+{
+
+  toCamelCase =
+    str:
+    lib.throwIfNot (lib.isString str)
+      "toCamelCase does only accepts string values, but got ${lib.typeOf str}"
+      (
+        let
+          separators = lib.splitStringBy (
+            prev: curr:
+            builtins.elem curr [
+              "-"
+              "_"
+              " "
+            ]
+          ) false str;
+
+          parts = lib.flatten (
+            map (lib.splitStringBy (
+              prev: curr: lib.match "[a-z]" prev != null && lib.match "[A-Z]" curr != null
+            ) true) separators
+          );
+
+          first = if lib.length parts > 0 then lib.toLower (lib.head parts) else "";
+          rest = if lib.length parts > 1 then builtins.map lib.toSentenceCase (lib.tail parts) else [ ];
+        in
+        lib.concatStrings (map (lib.addContextFrom str) ([ first ] ++ rest))
+      );
+
+}