mkdir nib/std && replace --all 'with' 'let ... in'

This commit is contained in:
Emile Clark-Boman 2025-12-14 20:47:34 +10:00
parent f9bb6ad937
commit fd008cd2e1
11 changed files with 108 additions and 104 deletions

View file

@ -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

View file

@ -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
]

View file

@ -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;
}

View file

@ -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);
}

9
nib/std/default.nix Normal file
View file

@ -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
]

View file

@ -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;
}

View file

@ -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"
];

View file

@ -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)
]

View file

@ -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_;

View file

@ -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_;

View file

@ -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_;