about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorsefidel <contact@sefidel.net>2023-03-29 20:54:19 +0900
committersefidel <contact@sefidel.net>2023-04-03 18:32:29 +0900
commitce06f43476863da90dc60dcee606d2b6c5a89a8e (patch)
tree5d14946330cb09ff0ebd97bee59407fccee4d860 /lib
downloadinfra-ce06f43476863da90dc60dcee606d2b6c5a89a8e.tar.gz
infra-ce06f43476863da90dc60dcee606d2b6c5a89a8e.zip
project: initial commit
Diffstat (limited to 'lib')
-rw-r--r--lib/README.md13
-rw-r--r--lib/attrs.nix26
-rw-r--r--lib/default.nix18
-rw-r--r--lib/misc.nix9
-rw-r--r--lib/modules.nix54
-rw-r--r--lib/system.nix22
6 files changed, 142 insertions, 0 deletions
diff --git a/lib/README.md b/lib/README.md
new file mode 100644
index 0000000..21ca023
--- /dev/null
+++ b/lib/README.md
@@ -0,0 +1,13 @@
+infra->lib
+==========
+
+Attribution
+-----------
+
+Most of the 'moving parts' of this is largely copied/modified from:
+
+- [hlissner/dotfiles][hlissner]: 2023-Feb, MIT (C) Henrik Lissner
+- [NobbZ/nixos-config][nobbz]: 2022, MIT (C) Nobert Melzer
+
+[hlissner]: https://github.com/hlissner/dotfiles
+[nobbz]: https://github.com/NobbZ/nixos-config
diff --git a/lib/attrs.nix b/lib/attrs.nix
new file mode 100644
index 0000000..0f8ebd1
--- /dev/null
+++ b/lib/attrs.nix
@@ -0,0 +1,26 @@
+{ lib, ... }:
+
+with builtins;
+with lib;
+rec {
+  # attrsToList
+  attrsToList = attrs:
+    mapAttrsToList (name: value: { inherit name value; }) attrs;
+
+  # mapFilterAttrs ::
+  #   (name -> value -> bool)
+  #   (name -> value -> { name = any; value = any; })
+  #   attrs
+  mapFilterAttrs = pred: f: attrs: filterAttrs pred (mapAttrs' f attrs);
+
+  # Generate an attribute set by mapping a function over a list of values.
+  genAttrs' = values: f: listToAttrs (map f values);
+
+  # anyAttrs :: (name -> value -> bool) attrs
+  anyAttrs = pred: attrs:
+    any (attr: pred attr.name attr.value) (attrsToList attrs);
+
+  # countAttrs :: (name -> value -> bool) attrs
+  countAttrs = pred: attrs:
+    count (attr: pred attr.name attr.value) (attrsToList attrs);
+}
diff --git a/lib/default.nix b/lib/default.nix
new file mode 100644
index 0000000..a275c09
--- /dev/null
+++ b/lib/default.nix
@@ -0,0 +1,18 @@
+{ inputs, lib, pkgs, ... }:
+
+let
+  inherit (lib) makeExtensible attrValues foldr;
+  inherit (modules) mapModules;
+
+  modules = import ./modules.nix {
+    inherit lib;
+    self.attrs = import ./attrs.nix { inherit lib; self = { }; };
+  };
+
+  mylib = makeExtensible (self:
+    with self; mapModules ./.
+      (file: import file { inherit self lib pkgs inputs; }));
+in
+mylib.extend
+  (self: super:
+  foldr (a: b: a // b) { } (attrValues super))
diff --git a/lib/misc.nix b/lib/misc.nix
new file mode 100644
index 0000000..484d0d5
--- /dev/null
+++ b/lib/misc.nix
@@ -0,0 +1,9 @@
+{ pkgs, ... }:
+
+rec {
+  # ifd3f/infra
+  wrapFile = name: path:
+    (pkgs.runCommand name { inherit path; } ''
+      cp -r "$path" "$out"
+    '');
+}
diff --git a/lib/modules.nix b/lib/modules.nix
new file mode 100644
index 0000000..ef7c289
--- /dev/null
+++ b/lib/modules.nix
@@ -0,0 +1,54 @@
+{ self, lib, ... }:
+
+let
+  inherit (builtins) attrValues readDir pathExists concatLists;
+  inherit (lib) id mapAttrsToList filterAttrs hasPrefix hasSuffix nameValuePair removeSuffix;
+  inherit (self.attrs) mapFilterAttrs;
+in
+rec {
+  mapModules = dir: fn:
+    mapFilterAttrs
+      (n: v:
+        v != null &&
+        !(hasPrefix "_" n))
+      (n: v:
+        let path = "${toString dir}/${n}"; in
+        if v == "directory" && pathExists "${path}/default.nix"
+        then nameValuePair n (fn path)
+        else if v == "regular" &&
+          n != "default.nix" &&
+          hasSuffix ".nix" n
+        then nameValuePair (removeSuffix ".nix" n) (fn path)
+        else nameValuePair "" null)
+      (readDir dir);
+
+  mapModules' = dir: fn:
+    attrValues (mapModules dir fn);
+
+  mapModulesRec = dir: fn:
+    mapFilterAttrs
+      (n: v:
+        v != null &&
+        !(hasPrefix "_" n))
+      (n: v:
+        let path = "${toString dir}/${n}"; in
+        if v == "directory"
+        then nameValuePair n (mapModulesRec path fn)
+        else if v == "regular" && n != "default.nix" && hasSuffix ".nix" n
+        then nameValuePair (removeSuffix ".nix" n) (fn path)
+        else nameValuePair "" null)
+      (readDir dir);
+
+  mapModulesRec' = dir: fn:
+    let
+      dirs =
+        mapAttrsToList
+          (k: _: "${dir}/${k}")
+          (filterAttrs
+            (n: v: v == "directory" && !(hasPrefix "_" n))
+            (readDir dir));
+      files = attrValues (mapModules dir id);
+      paths = files ++ concatLists (map (d: mapModulesRec' d id) dirs);
+    in
+    map fn paths;
+}
diff --git a/lib/system.nix b/lib/system.nix
new file mode 100644
index 0000000..8fc4dce
--- /dev/null
+++ b/lib/system.nix
@@ -0,0 +1,22 @@
+{ self, inputs, lib, pkgs, ... }:
+
+with lib;
+with lib.my;
+{
+  mkSystem = path: attrs @ { ... }: {
+    imports = [
+      {
+        networking.hostName = mkDefault
+          (removeSuffix ".nix" (baseNameOf path));
+        system.configurationRevision = self.rev or "dirty";
+      }
+      ../. # /default.nix
+      (import path)
+    ];
+  };
+
+  mapSystems = dir: attrs @ { system ? system, ... }:
+    mapModules dir
+      (hostPath: mkSystem hostPath attrs);
+}
+