restructure std -> nib.types + change type naming convention
This commit is contained in:
parent
b9f2acf7e7
commit
eac4161b36
11 changed files with 129 additions and 139 deletions
101
nib/default.nix
101
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
|
||||
|
|
|
|||
2
nib/mod.nix
Normal file
2
nib/mod.nix
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
{...}: {
|
||||
}
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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!
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
|
|
@ -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"
|
||||
];
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
@ -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;}
|
||||
]
|
||||
|
|
@ -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));
|
||||
}
|
||||
40
nib/types/result.nix
Normal file
40
nib/types/result.nix
Normal file
|
|
@ -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';
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue