From eac4161b367b7ed2c76ef0484fc37be623f1ca37 Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Sun, 14 Dec 2025 12:41:52 +1000 Subject: [PATCH] restructure std -> nib.types + change type naming convention --- nib/default.nix | 101 +++++++++++++++++---------------- nib/mod.nix | 2 + nib/parse/default.nix | 8 +-- nib/parse/struct.nix | 44 +++++++------- nib/std/result.nix | 47 --------------- nib/sys/default.nix | 13 ++--- nib/{std => types}/attrs.nix | 4 +- nib/{std => types}/default.nix | 5 +- nib/{std => types}/fault.nix | 4 +- nib/{std => types}/lists.nix | 0 nib/types/result.nix | 40 +++++++++++++ 11 files changed, 129 insertions(+), 139 deletions(-) create mode 100644 nib/mod.nix delete mode 100644 nib/std/result.nix rename nib/{std => types}/attrs.nix (98%) rename nib/{std => types}/default.nix (90%) rename nib/{std => types}/fault.nix (78%) rename nib/{std => types}/lists.nix (100%) create mode 100644 nib/types/result.nix diff --git a/nib/default.nix b/nib/default.nix index 72599c8..c45d98f 100644 --- a/nib/default.nix +++ b/nib/default.nix @@ -1,54 +1,55 @@ {systems, ...}: let - mergeAttrsList = std.attrs.mergeAttrsList; + mergeAttrsList = types.mergeAttrsList; - std = import ./std {}; - stdSubMods = { - attrs = std.attrs; - fault = std.fault; - lists = std.lists; - result = std.result; - }; + submodArgs = {inherit nib;}; - parse = import ./parse (mergeAttrsList [stdSubMods]); + # TODO: move this to a new module + mkMod' = args: mod: import mod args; + mkMod = mkMod' submodArgs; + + parse = mkMod ./parse; + types = mkMod ./types; + + nib = with types; + mergeAttrsList [ + # submodule content is accessible first by submodule name + # then by the name of the content (ie self.submodule.myFunc) + {inherit parse types;} + + # submodule is included directly to this module (ie self.myFunc) + + # this module + { + # === External Functions === + withPkgs = repo: config: system: + import repo { + inherit system; + } + // config; + + mkSys = input: let + # function taking a system as argument + pkgsFor = input.pkgs; + in { + inherit pkgsFor; + forAllSystems = f: + std.attrs.genAttrs systems ( + system: f system (pkgsFor system) + ); + }; + + mkUSys = input: let + # functions taking a system as argument + pkgsFor = input.pkgs; + upkgsFor = input.upkgs; + in { + inherit pkgsFor upkgsFor; + forAllSystems = f: + std.attrs.genAttrs systems ( + system: f system (pkgsFor system) (upkgsFor system) + ); + }; + } + ]; in - mergeAttrsList [ - # submodule content is accessible first by submodule name - # then by the name of the content (ie self.submodule.myFunc) - {inherit parse;} - - # submodule is included directly to this module (ie self.myFunc) - std - - # this module - { - # === External Functions === - withPkgs = repo: config: system: - import repo { - inherit system; - } - // config; - - mkSys = input: let - # function taking a system as argument - pkgsFor = input.pkgs; - in { - inherit pkgsFor; - forAllSystems = f: - std.attrs.genAttrs systems ( - system: f system (pkgsFor system) - ); - }; - - mkUSys = input: let - # functions taking a system as argument - pkgsFor = input.pkgs; - upkgsFor = input.upkgs; - in { - inherit pkgsFor upkgsFor; - forAllSystems = f: - std.attrs.genAttrs systems ( - system: f system (pkgsFor system) (upkgsFor system) - ); - }; - } - ] + nib diff --git a/nib/mod.nix b/nib/mod.nix new file mode 100644 index 0000000..ea8f50d --- /dev/null +++ b/nib/mod.nix @@ -0,0 +1,2 @@ +{...}: { +} diff --git a/nib/parse/default.nix b/nib/parse/default.nix index 0918ec8..5b24085 100644 --- a/nib/parse/default.nix +++ b/nib/parse/default.nix @@ -1,11 +1,7 @@ -{ - attrs, - result, - ... -} @ args: let +{nib, ...} @ args: let struct = import ./struct.nix args; in - attrs.mergeAttrsList [ + nib.types.mergeAttrsList [ # submodule is included directly to this module (ie self.myFunc) struct diff --git a/nib/parse/struct.nix b/nib/parse/struct.nix index 71f1dc8..3263d53 100644 --- a/nib/parse/struct.nix +++ b/nib/parse/struct.nix @@ -1,8 +1,7 @@ -{ - attrs, - result, - ... -}: rec { +{nib, ...}: +with nib.types; let + attrs = nib.attrs; +in rec { cmpStructErr' = errBadKeys: errBadValues: path: S: T: if builtins.isAttrs S && builtins.isAttrs T then let @@ -13,7 +12,7 @@ if !(keysS == keysT) then errBadKeys path keysS keysT else - (result.firstErr + (firstErr (builtins.map (k: cmpStructErr' errBadKeys errBadValues (path ++ [k]) (keysS.${k}) (keysT.${k})) keysS)) @@ -27,33 +26,31 @@ cmpStruct = cmpStructErr - (path: keysS: keysT: - result.Err { + (path: _: _: + Err { reason = "keys"; inherit path; }) - (path: S: T: - result.Ok "ok"); + (_: _: _: Ok'); cmpTypedStruct = cmpStructErr - (path: keysS: keysT: - result.Err { + (path: _: _: + Err { reason = "keys"; inherit path; }) - (path: S: T: - result.Err { + (path: _: _: + Err { reason = "values"; inherit path; }); cmpTypedPartialStruct = cmpStructErr - (path: keysS: keysT: - result.Ok "ok") - (path: S: T: - result.Err { + (_: _: _: Ok') + (path: _: _: + Err { reason = "values"; inherit path; }); @@ -61,10 +58,10 @@ # check is a function taking two structs # and returning a result monad. mergeStruct' = check: template: S: let - res = check template S; + R = check template S; in - result.errOr res ({...}: - result.Ok ( + errOr ({...}: + Ok ( attrs.mapAttrsRecursive ( path: value: let valueS = attrs.attrValueAt S path; @@ -74,10 +71,11 @@ else value ) template - )); + )) + R; # mergeStruct ensures no properties are evaluated (entirely lazy) - mergeStruct = mergeStruct' (S: T: result.Ok "ok"); + mergeStruct = mergeStruct' (_: _: Ok'); # mergeTypedPartialStruct must evaluate properties (not lazy) # for lazy evaluation use mergeStruct instead! diff --git a/nib/std/result.nix b/nib/std/result.nix deleted file mode 100644 index 740bf41..0000000 --- a/nib/std/result.nix +++ /dev/null @@ -1,47 +0,0 @@ -{lists, ...}: rec { - # Result Monad - Ok = value: { - success = true; - value = value; - }; - Err = err: { - success = false; - value = err; - }; - - # Pattern Matching - isResult = r: builtins.attrNames r == ["success" "value"]; - isOk = r: isResult r && r.success; - isErr = r: isResult r && !r.success; - - # Unwrap (Monadic Return Operation) - unwrap = f: r: - if isOk r - then r.value - else f r.value; - - unwrapDefault = default: unwrap (_: default); - - # Map (Monadic Bind Operation) - identity = r: r; - - map = r: f: g: - if isOk r - then Ok (f r.value) - else Err (g r.value); - mapOk = f: map f identity; - mapErr = f: map identity f; - - # Conditionals - okOr = r: f: - if isOk r - then r - else f r; - - errOr = r: f: - if isErr r - then r - else f r; - - firstErr = lists.findFirst isErr (Ok "No errors"); -} diff --git a/nib/sys/default.nix b/nib/sys/default.nix index 1793f7e..36e7e9a 100644 --- a/nib/sys/default.nix +++ b/nib/sys/default.nix @@ -1,26 +1,23 @@ -{ - attrs, - lists, - ... -}: let +{nib, ...}: +with nib.types; let # === Internal Helper Functions === toSystemName = arch: platform: "${arch}-${platform}"; listsToSystemNames = archs: platforms: - lists.crossLists (arch: platform: toSystemName arch platform) + crossLists (arch: platform: toSystemName arch platform) [ (builtins.attrValues archs) (builtins.attrValues platforms) ]; in rec { # REF: https://github.com/nix-systems/nix-systems - archs = attrs.identityAttrsList [ + archs = identityAttrsList [ "x86_64" "aarch64" "riscv64" ]; # REF: https://github.com/nix-systems/nix-systems - platforms = attrs.identityAttrsList [ + platforms = identityAttrsList [ "linux" "darwin" ]; diff --git a/nib/std/attrs.nix b/nib/types/attrs.nix similarity index 98% rename from nib/std/attrs.nix rename to nib/types/attrs.nix index dcb4869..f815bf8 100644 --- a/nib/std/attrs.nix +++ b/nib/types/attrs.nix @@ -1,4 +1,4 @@ -{lists, ...}: rec { +{nib, ...}: rec { nameValuePair = name: value: {inherit name value;}; identityAttrs = value: {${value} = value;}; @@ -91,7 +91,7 @@ # form: attrValueAt :: xs -> path -> value # given path as a list of strings, return that value of an # attribute set at that path - attrValueAt = lists.foldl (l: r: + attrValueAt = nib.types.foldl (l: r: if l != null && builtins.hasAttr r l then l.${r} else null); diff --git a/nib/std/default.nix b/nib/types/default.nix similarity index 90% rename from nib/std/default.nix rename to nib/types/default.nix index e064449..62b2f37 100644 --- a/nib/std/default.nix +++ b/nib/types/default.nix @@ -6,8 +6,11 @@ in attrs.mergeAttrsList [ # submodule is included directly to this module (ie self.myFunc) + attrs + fault + lists + result # submodule content is accessible first by submodule name # then by the name of the content (ie self.submodule.myFunc) - {inherit attrs fault lists result;} ] diff --git a/nib/std/fault.nix b/nib/types/fault.nix similarity index 78% rename from nib/std/fault.nix rename to nib/types/fault.nix index b5f1aa6..b437ded 100644 --- a/nib/std/fault.nix +++ b/nib/types/fault.nix @@ -9,8 +9,8 @@ isFault = F: builtins.attrNames F == ["error"]; # Unwrap (Monadic Return Operation) - unwrap = F: F.error; + unwrapFault = F: F.error; # Map (Monadic Bind Operation) - map = f: F: Fault (f (unwrap F)); + mapFault = f: F: Fault (f (unwrapFault F)); } diff --git a/nib/std/lists.nix b/nib/types/lists.nix similarity index 100% rename from nib/std/lists.nix rename to nib/types/lists.nix diff --git a/nib/types/result.nix b/nib/types/result.nix new file mode 100644 index 0000000..f57d1b5 --- /dev/null +++ b/nib/types/result.nix @@ -0,0 +1,40 @@ +{nib, ...}: rec { + # Res (Result) Monad + Res = success: value: {inherit success value;}; + Ok = value: Res true value; + Ok' = Ok "ok"; + Err = value: Res false value; + Err' = Err "err"; + + # Pattern Matching + isRes = R: builtins.attrNames R == ["success" "value"]; + isOk = R: isRes R && R.success; + isErr = R: isRes R && !R.success; + + # Unwrap (Monadic Return Operation) + unwrapRes = f: R: + if isErr R + then f R.value + else R.value; + + # Map (Monadic Bind Operation) + mapRes = f: g: R: + if isOk R + then Ok (f R.value) + else Err (g R.value); + mapOk = f: mapRes f (x: x); + mapErr = f: mapRes (x: x) f; + + # Conditionals + okOr = f: R: + if isOk R + then R + else f R; + + errOr = f: R: + if isErr R + then R + else f R; + + firstErr = nib.types.findFirst isErr Ok'; +}