about summary refs log tree commit diff
path: root/lib/default.nix
diff options
context:
space:
mode:
authorAzat Bahawi <azat@bahawi.net>2024-04-21 02:15:42 +0300
committerAzat Bahawi <azat@bahawi.net>2024-04-21 02:15:42 +0300
commite6ed60548397627bf10f561f9438201dbba0a36e (patch)
treef9a84c5957d2cc4fcd148065ee9365a0c851ae1c /lib/default.nix
parent2024-04-18 (diff)
2024-04-21
Diffstat (limited to '')
-rw-r--r--lib/default.nix48
1 files changed, 33 insertions, 15 deletions
diff --git a/lib/default.nix b/lib/default.nix
index ab4ca0d..cd4b601 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -1,4 +1,4 @@
-lib: _: rec {
+lib: _: with lib; rec {
   my = import ./my.nix lib;
 
   dns = import ./dns.nix;
@@ -8,7 +8,7 @@ lib: _: rec {
     assert (builtins.isInt number) || (builtins.isFloat number);
     builtins.div number 2 == 0;
 
-  isOdd = !isEven;
+  isOdd = number: !isEven number;
 
   pow =
     base: exponent:
@@ -16,19 +16,10 @@ lib: _: rec {
     assert exponent > 0;
     builtins.foldl' (x: _: x * base) 1 (builtins.genList (_: _) exponent);
 
-  mapListToAttrs =
-    f: attrs:
-    builtins.listToAttrs (
-      map (name: {
-        name = if builtins.isList name then builtins.elemAt name (builtins.length name - 1) else name;
-        value = f name;
-      }) attrs
-    );
-
   mkTcpMem =
     min: ini: max:
     assert min <= ini && ini <= max;
-    lib.concatMapStrings (x: toString x + " ") (
+    concatMapStrings (x: toString x + " ") (
       map (pow 2) [
         min
         ini
@@ -36,6 +27,33 @@ lib: _: rec {
       ]
     );
 
+  modulesIn =
+    dir:
+    pipe dir [
+      builtins.readDir
+      (mapAttrsToList (
+        name: type:
+        if type == "regular" && hasSuffix ".nix" name && name != "default.nix" then
+          [
+            {
+              name = removeSuffix ".nix" name;
+              value = dir + "/${name}";
+            }
+          ]
+        else if type == "directory" && pathExists (dir + "/${name}/default.nix") then
+          [
+            {
+              inherit name;
+              value = dir + "/${name}";
+            }
+          ]
+        else
+          [ ]
+      ))
+      concatLists
+      listToAttrs
+    ];
+
   moduleFromRef = module: ref: sha256: {
     disabledModules = [ module ];
     imports = [
@@ -44,9 +62,9 @@ lib: _: rec {
           src = builtins.fetchTarball {
             url =
               let
-                cons = lib.splitString ":" ref;
-                owner = lib.head cons;
-                branch = lib.last cons;
+                cons = splitString ":" ref;
+                owner = head cons;
+                branch = last cons;
               in
               "https://github.com/${owner}/nixpkgs/archive/refs/heads/${branch}.tar.gz";
             inherit sha256;

Consider giving Nix/NixOS a try! <3