From fd008cd2e11a71b9ab96b8a8ef3aa509cb263c83 Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Sun, 14 Dec 2025 20:47:34 +1000 Subject: [PATCH] mkdir nib/std && replace --all 'with' 'let ... in' --- nib/default.nix | 82 +++++++++++++++++------------------- nib/parse/default.nix | 12 ++---- nib/parse/struct.nix | 30 ++++++++----- nib/{types => std}/attrs.nix | 21 ++++----- nib/std/default.nix | 9 ++++ nib/{types => std}/lists.nix | 13 +++--- nib/sys/default.nix | 15 ++++--- nib/types/default.nix | 9 +--- nib/types/fault.nix | 5 +-- nib/types/maybe.nix | 8 ++-- nib/types/res.nix | 8 ++-- 11 files changed, 108 insertions(+), 104 deletions(-) rename nib/{types => std}/attrs.nix (87%) create mode 100644 nib/std/default.nix rename nib/{types => std}/lists.nix (87%) diff --git a/nib/default.nix b/nib/default.nix index 5777125..515f205 100644 --- a/nib/default.nix +++ b/nib/default.nix @@ -1,55 +1,51 @@ {systems, ...}: let - mergeAttrsList = types.mergeAttrsList; - - submodArgs = {inherit nib;}; - # TODO: move this to a new module mkMod' = args: mod: import mod args; - mkMod = mkMod' submodArgs; + mkMod = mkMod' {inherit nib;}; - parse = mkMod ./parse; + std = mkMod ./std; types = mkMod ./types; + parse = mkMod ./parse; - 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;} + nib = std.mergeAttrsList [ + # submodule content is accessible first by submodule name + # then by the name of the content (ie self.submodule.myFunc) + {inherit std types parse;} - # submodule is included directly to this module (ie self.myFunc) + # submodule is included directly to this module (ie self.myFunc) - # this module - { - # === External Functions === - withPkgs = repo: config: system: - import repo { - inherit system; - } - // config; + # 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: - genAttrs systems ( - system: f system (pkgsFor system) - ); - }; + mkSys = input: let + # function taking a system as argument + pkgsFor = input.pkgs; + in { + inherit pkgsFor; + forAllSystems = f: + std.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: - genAttrs systems ( - system: f system (pkgsFor system) (upkgsFor system) - ); - }; - } - ]; + mkUSys = input: let + # functions taking a system as argument + pkgsFor = input.pkgs; + upkgsFor = input.upkgs; + in { + inherit pkgsFor upkgsFor; + forAllSystems = f: + std.genAttrs systems ( + system: f system (pkgsFor system) (upkgsFor system) + ); + }; + } + ]; in nib diff --git a/nib/parse/default.nix b/nib/parse/default.nix index f636706..af28ce3 100644 --- a/nib/parse/default.nix +++ b/nib/parse/default.nix @@ -1,11 +1,7 @@ {nib, ...} @ args: let struct = import ./struct.nix args; in - with nib.types; - mergeAttrsList [ - # submodule is included directly to this module (ie self.myFunc) - struct - - # submodule content is accessible first by submodule name - # then by the name of the content (ie self.submodule.myFunc) - ] + nib.std.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 048533f..0324f33 100644 --- a/nib/parse/struct.nix +++ b/nib/parse/struct.nix @@ -1,11 +1,21 @@ -{nib, ...}: -with builtins; -with nib.types; rec { +{nib, ...}: let + Err = nib.types.Err; + Ok' = nib.types.Ok'; + firstErr = nib.types.firstErr; + + unwrapSome = nib.types.unwrapSome; + + isTerminal = nib.types.isTerminal; + unwrapTerminal = nib.types.unwrapTerminal; + + mapAttrsRecursiveCond = nib.std.mapAttrsRecursiveCond; + attrValueAt = nib.std.attrValueAt; +in rec { cmpStructErr' = errBadKeys: errBadValues: path: S: T: - if isAttrs S && isAttrs T + if builtins.isAttrs S && builtins.isAttrs T then let - keysS = attrNames S; - keysT = attrNames T; + keysS = builtins.attrNames S; + keysT = builtins.attrNames T; in # ensure all key names match, then recurse if !(keysS == keysT) @@ -18,7 +28,7 @@ with nib.types; rec { else # terminating leaf in recursion tree reached # ensure values' types match - (typeOf S == typeOf T) + (builtins.typeOf S == builtins.typeOf T) || errBadValues path S T; cmpStructErr = errBadKeys: errBadValues: cmpStructErr' errBadKeys errBadValues []; @@ -73,14 +83,14 @@ with nib.types; rec { (value: if isTerminal value then unwrapTerminal value - else valueS); + else value); # TODO: Define: # TODO: throwUnreachable = throw "Unreachable code was evaluated.."; # TODO: abortUnreachable = abort "Unreachable code was evaluated..."; - mergeStruct = mergeStruct' (_: _: Ok'); + mergeStruct = mergeStructs' (_: _: Ok'); # mergeTypedPartialStruct must evaluate properties (not lazy) # for lazy evaluation use mergeStruct instead! - mergeTypedPartialStruct = mergeStruct' cmpTypedPartialStruct; + mergeTypedPartialStruct = mergeStructs' cmpTypedPartialStruct; } diff --git a/nib/types/attrs.nix b/nib/std/attrs.nix similarity index 87% rename from nib/types/attrs.nix rename to nib/std/attrs.nix index 7266521..fa6fdd9 100644 --- a/nib/types/attrs.nix +++ b/nib/std/attrs.nix @@ -1,11 +1,12 @@ -{nib, ...}: -with builtins; -with nib.types; rec { +{nib, ...}: let + foldl = nib.std.foldl; + nullableToMaybe = nib.types.nullableToMany; +in rec { nameValuePair = name: value: {inherit name value;}; identityAttrs = value: {${value} = value;}; - identityAttrsList = values: map (v: identityAttrs v) values; + identityAttrsMany = values: map (v: identityAttrs v) values; /** Generate an attribute set by mapping a function over a list of @@ -75,15 +76,15 @@ with nib.types; rec { ::: */ - genAttrs' = xs: f: listToAttrs (map f xs); + genAttrs' = xs: f: builtins.listToAttrs (map f xs); mapAttrsRecursiveCond = cond: f: set: let recurse = path: - mapAttrs ( + builtins.mapAttrs ( name: value: let next = path ++ [name]; in - if isAttrs value && cond value + if builtins.isAttrs value && cond value then recurse next value else f next value ); @@ -97,7 +98,7 @@ with nib.types; rec { # attribute set at that path attrValueAt = let value = foldl (l: r: - if isAttrs l && hasAttr r l + if builtins.isAttrs l && builtins.hasAttr r l then l.${r} else null); in @@ -115,11 +116,11 @@ with nib.types; rec { binaryMerge start (start + (end - start) / 2) // binaryMerge (start + (end - start) / 2) end else # Otherwise there will be exactly 1 element due to the invariant, in which case we just return it directly - elemAt list start; + builtins.elemAt list start; in if list == [] then # Calling binaryMerge as below would not satisfy its invariant {} - else binaryMerge 0 (length list); + else binaryMerge 0 (builtins.length list); } diff --git a/nib/std/default.nix b/nib/std/default.nix new file mode 100644 index 0000000..431f7d1 --- /dev/null +++ b/nib/std/default.nix @@ -0,0 +1,9 @@ +{...} @ args: let + attrs = import ./attrs.nix args; + lists = import ./lists.nix args; +in + attrs.mergeAttrsList [ + # submodule is included directly to this module (ie self.myFunc) + attrs + lists + ] diff --git a/nib/types/lists.nix b/nib/std/lists.nix similarity index 87% rename from nib/types/lists.nix rename to nib/std/lists.nix index 8c0fd67..a8e9ff3 100644 --- a/nib/types/lists.nix +++ b/nib/std/lists.nix @@ -1,14 +1,13 @@ -{...}: -with builtins; rec { +{...}: rec { foldl = op: nul: list: let foldl' = n: if n == -1 then nul - else op (foldl' (n - 1)) (elemAt list n); + else op (foldl' (n - 1)) (builtins.elemAt list n); in - foldl' (length list - 1); + foldl' (builtins.length list - 1); - crossLists = f: foldl (fs: args: concatMap (f: map f args) fs) [f]; + crossLists = f: foldl (fs: args: builtins.concatMap (f: map f args) fs) [f]; findFirstIndex = pred: default: list: let # A naive recursive implementation would be much simpler, but @@ -24,7 +23,7 @@ with builtins; rec { # # We start with index -1 and the 0'th element of the list, which satisfies the invariant resultIndex = - foldl' ( + builtins.foldl' ( index: el: if index < 0 then @@ -51,5 +50,5 @@ with builtins; rec { in if index == null then default - else elemAt list index; + else builtins.elemAt list index; } diff --git a/nib/sys/default.nix b/nib/sys/default.nix index d05d9f7..5f7ba35 100644 --- a/nib/sys/default.nix +++ b/nib/sys/default.nix @@ -1,24 +1,25 @@ -{nib, ...}: -with builtins; -with nib.types; let +{nib, ...}: let + crossLists = nib.std.crossLists; + identityAttrsMany = nib.std.identityAttrsMany; + # === Internal Helper Functions === toSystemName = arch: platform: "${arch}-${platform}"; listsToSystemNames = archs: platforms: crossLists (arch: platform: toSystemName arch platform) [ - (attrValues archs) - (attrValues platforms) + (builtins.attrValues archs) + (builtins.attrValues platforms) ]; in rec { # REF: https://github.com/nix-systems/nix-systems - archs = identityAttrsList [ + archs = identityAttrsMany [ "x86_64" "aarch64" "riscv64" ]; # REF: https://github.com/nix-systems/nix-systems - platforms = identityAttrsList [ + platforms = identityAttrsMany [ "linux" "darwin" ]; diff --git a/nib/types/default.nix b/nib/types/default.nix index 3cb8350..b1d11b2 100644 --- a/nib/types/default.nix +++ b/nib/types/default.nix @@ -1,18 +1,11 @@ {nib, ...} @ args: let - attrs = import ./attrs.nix args; fault = import ./fault.nix args; - lists = import ./lists.nix args; maybe = import ./maybe.nix args; res = import ./res.nix args; in - attrs.mergeAttrsList [ + nib.std.mergeAttrsList [ # submodule is included directly to this module (ie self.myFunc) - attrs fault - lists maybe res - - # submodule content is accessible first by submodule name - # then by the name of the content (ie self.submodule.myFunc) ] diff --git a/nib/types/fault.nix b/nib/types/fault.nix index bcc5ad1..76fccc4 100644 --- a/nib/types/fault.nix +++ b/nib/types/fault.nix @@ -1,5 +1,4 @@ -{...}: -with builtins; rec { +{...}: rec { # Fault Monad # Wrapper around an error (ie builtins.abort) Fault = error: { @@ -7,7 +6,7 @@ with builtins; rec { }; # Pattern Matching - isFault = F: attrNames F == ["_error_"]; + isFault = F: builtins.attrNames F == ["_error_"]; # Unwrap (Monadic Return Operation) unwrapFault = F: F._error_; diff --git a/nib/types/maybe.nix b/nib/types/maybe.nix index 361b54a..800cde8 100644 --- a/nib/types/maybe.nix +++ b/nib/types/maybe.nix @@ -1,6 +1,6 @@ -{nib, ...}: -with builtins; -with nib.types; let +{nib, ...}: let + Res = nib.types.Res; + findFirst = nib.std.findFirst; # TODO: try get enum generation working (and other type constructors) # Maybe = mkEnum "nib::Maybe" { # Some = mkEnumVariant {value = "nix::String";}; @@ -66,7 +66,7 @@ in rec { None = Maybe false null; # Pattern Matching - isMaybe = T: attrNames T == ["_some_" "_value_"]; + isMaybe = T: builtins.attrNames T == ["_some_" "_value_"]; isSome = T: isMaybe T && T._some_; isNone = T: isMaybe T && !T._some_; diff --git a/nib/types/res.nix b/nib/types/res.nix index 7e34f39..23b7484 100644 --- a/nib/types/res.nix +++ b/nib/types/res.nix @@ -1,6 +1,6 @@ -{nib, ...}: -with builtins; -with nib.types; rec { +{nib, ...}: let + findFirst = nib.std.findFirst; +in rec { # Res (Result) Monad Res = success: value: { _success_ = success; @@ -12,7 +12,7 @@ with nib.types; rec { Err' = Err "err"; # Pattern Matching - isRes = R: attrNames R == ["_success_" "_value_"]; + isRes = R: builtins.attrNames R == ["_success_" "_value_"]; isOk = R: isRes R && R._success_; isErr = R: isRes R && !R._success_;