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
|
{systems, ...}: let
|
||||||
mergeAttrsList = std.attrs.mergeAttrsList;
|
mergeAttrsList = types.mergeAttrsList;
|
||||||
|
|
||||||
std = import ./std {};
|
submodArgs = {inherit nib;};
|
||||||
stdSubMods = {
|
|
||||||
attrs = std.attrs;
|
|
||||||
fault = std.fault;
|
|
||||||
lists = std.lists;
|
|
||||||
result = std.result;
|
|
||||||
};
|
|
||||||
|
|
||||||
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
|
in
|
||||||
mergeAttrsList [
|
nib
|
||||||
# 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)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
|
||||||
2
nib/mod.nix
Normal file
2
nib/mod.nix
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
{...}: {
|
||||||
|
}
|
||||||
|
|
@ -1,11 +1,7 @@
|
||||||
{
|
{nib, ...} @ args: let
|
||||||
attrs,
|
|
||||||
result,
|
|
||||||
...
|
|
||||||
} @ args: let
|
|
||||||
struct = import ./struct.nix args;
|
struct = import ./struct.nix args;
|
||||||
in
|
in
|
||||||
attrs.mergeAttrsList [
|
nib.types.mergeAttrsList [
|
||||||
# submodule is included directly to this module (ie self.myFunc)
|
# submodule is included directly to this module (ie self.myFunc)
|
||||||
struct
|
struct
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
{
|
{nib, ...}:
|
||||||
attrs,
|
with nib.types; let
|
||||||
result,
|
attrs = nib.attrs;
|
||||||
...
|
in rec {
|
||||||
}: rec {
|
|
||||||
cmpStructErr' = errBadKeys: errBadValues: path: S: T:
|
cmpStructErr' = errBadKeys: errBadValues: path: S: T:
|
||||||
if builtins.isAttrs S && builtins.isAttrs T
|
if builtins.isAttrs S && builtins.isAttrs T
|
||||||
then let
|
then let
|
||||||
|
|
@ -13,7 +12,7 @@
|
||||||
if !(keysS == keysT)
|
if !(keysS == keysT)
|
||||||
then errBadKeys path keysS keysT
|
then errBadKeys path keysS keysT
|
||||||
else
|
else
|
||||||
(result.firstErr
|
(firstErr
|
||||||
(builtins.map
|
(builtins.map
|
||||||
(k: cmpStructErr' errBadKeys errBadValues (path ++ [k]) (keysS.${k}) (keysT.${k}))
|
(k: cmpStructErr' errBadKeys errBadValues (path ++ [k]) (keysS.${k}) (keysT.${k}))
|
||||||
keysS))
|
keysS))
|
||||||
|
|
@ -27,33 +26,31 @@
|
||||||
|
|
||||||
cmpStruct =
|
cmpStruct =
|
||||||
cmpStructErr
|
cmpStructErr
|
||||||
(path: keysS: keysT:
|
(path: _: _:
|
||||||
result.Err {
|
Err {
|
||||||
reason = "keys";
|
reason = "keys";
|
||||||
inherit path;
|
inherit path;
|
||||||
})
|
})
|
||||||
(path: S: T:
|
(_: _: _: Ok');
|
||||||
result.Ok "ok");
|
|
||||||
|
|
||||||
cmpTypedStruct =
|
cmpTypedStruct =
|
||||||
cmpStructErr
|
cmpStructErr
|
||||||
(path: keysS: keysT:
|
(path: _: _:
|
||||||
result.Err {
|
Err {
|
||||||
reason = "keys";
|
reason = "keys";
|
||||||
inherit path;
|
inherit path;
|
||||||
})
|
})
|
||||||
(path: S: T:
|
(path: _: _:
|
||||||
result.Err {
|
Err {
|
||||||
reason = "values";
|
reason = "values";
|
||||||
inherit path;
|
inherit path;
|
||||||
});
|
});
|
||||||
|
|
||||||
cmpTypedPartialStruct =
|
cmpTypedPartialStruct =
|
||||||
cmpStructErr
|
cmpStructErr
|
||||||
(path: keysS: keysT:
|
(_: _: _: Ok')
|
||||||
result.Ok "ok")
|
(path: _: _:
|
||||||
(path: S: T:
|
Err {
|
||||||
result.Err {
|
|
||||||
reason = "values";
|
reason = "values";
|
||||||
inherit path;
|
inherit path;
|
||||||
});
|
});
|
||||||
|
|
@ -61,10 +58,10 @@
|
||||||
# check is a function taking two structs
|
# check is a function taking two structs
|
||||||
# and returning a result monad.
|
# and returning a result monad.
|
||||||
mergeStruct' = check: template: S: let
|
mergeStruct' = check: template: S: let
|
||||||
res = check template S;
|
R = check template S;
|
||||||
in
|
in
|
||||||
result.errOr res ({...}:
|
errOr ({...}:
|
||||||
result.Ok (
|
Ok (
|
||||||
attrs.mapAttrsRecursive (
|
attrs.mapAttrsRecursive (
|
||||||
path: value: let
|
path: value: let
|
||||||
valueS = attrs.attrValueAt S path;
|
valueS = attrs.attrValueAt S path;
|
||||||
|
|
@ -74,10 +71,11 @@
|
||||||
else value
|
else value
|
||||||
)
|
)
|
||||||
template
|
template
|
||||||
));
|
))
|
||||||
|
R;
|
||||||
|
|
||||||
# mergeStruct ensures no properties are evaluated (entirely lazy)
|
# mergeStruct ensures no properties are evaluated (entirely lazy)
|
||||||
mergeStruct = mergeStruct' (S: T: result.Ok "ok");
|
mergeStruct = mergeStruct' (_: _: Ok');
|
||||||
|
|
||||||
# mergeTypedPartialStruct must evaluate properties (not lazy)
|
# mergeTypedPartialStruct must evaluate properties (not lazy)
|
||||||
# for lazy evaluation use mergeStruct instead!
|
# 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 @@
|
||||||
{
|
{nib, ...}:
|
||||||
attrs,
|
with nib.types; let
|
||||||
lists,
|
|
||||||
...
|
|
||||||
}: let
|
|
||||||
# === Internal Helper Functions ===
|
# === Internal Helper Functions ===
|
||||||
toSystemName = arch: platform: "${arch}-${platform}";
|
toSystemName = arch: platform: "${arch}-${platform}";
|
||||||
listsToSystemNames = archs: platforms:
|
listsToSystemNames = archs: platforms:
|
||||||
lists.crossLists (arch: platform: toSystemName arch platform)
|
crossLists (arch: platform: toSystemName arch platform)
|
||||||
[
|
[
|
||||||
(builtins.attrValues archs)
|
(builtins.attrValues archs)
|
||||||
(builtins.attrValues platforms)
|
(builtins.attrValues platforms)
|
||||||
];
|
];
|
||||||
in rec {
|
in rec {
|
||||||
# REF: https://github.com/nix-systems/nix-systems
|
# REF: https://github.com/nix-systems/nix-systems
|
||||||
archs = attrs.identityAttrsList [
|
archs = identityAttrsList [
|
||||||
"x86_64"
|
"x86_64"
|
||||||
"aarch64"
|
"aarch64"
|
||||||
"riscv64"
|
"riscv64"
|
||||||
];
|
];
|
||||||
|
|
||||||
# REF: https://github.com/nix-systems/nix-systems
|
# REF: https://github.com/nix-systems/nix-systems
|
||||||
platforms = attrs.identityAttrsList [
|
platforms = identityAttrsList [
|
||||||
"linux"
|
"linux"
|
||||||
"darwin"
|
"darwin"
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
{lists, ...}: rec {
|
{nib, ...}: rec {
|
||||||
nameValuePair = name: value: {inherit name value;};
|
nameValuePair = name: value: {inherit name value;};
|
||||||
|
|
||||||
identityAttrs = value: {${value} = value;};
|
identityAttrs = value: {${value} = value;};
|
||||||
|
|
@ -91,7 +91,7 @@
|
||||||
# form: attrValueAt :: xs -> path -> value
|
# form: attrValueAt :: xs -> path -> value
|
||||||
# given path as a list of strings, return that value of an
|
# given path as a list of strings, return that value of an
|
||||||
# attribute set at that path
|
# attribute set at that path
|
||||||
attrValueAt = lists.foldl (l: r:
|
attrValueAt = nib.types.foldl (l: r:
|
||||||
if l != null && builtins.hasAttr r l
|
if l != null && builtins.hasAttr r l
|
||||||
then l.${r}
|
then l.${r}
|
||||||
else null);
|
else null);
|
||||||
|
|
@ -6,8 +6,11 @@
|
||||||
in
|
in
|
||||||
attrs.mergeAttrsList [
|
attrs.mergeAttrsList [
|
||||||
# submodule is included directly to this module (ie self.myFunc)
|
# submodule is included directly to this module (ie self.myFunc)
|
||||||
|
attrs
|
||||||
|
fault
|
||||||
|
lists
|
||||||
|
result
|
||||||
|
|
||||||
# submodule content is accessible first by submodule name
|
# submodule content is accessible first by submodule name
|
||||||
# then by the name of the content (ie self.submodule.myFunc)
|
# 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"];
|
isFault = F: builtins.attrNames F == ["error"];
|
||||||
|
|
||||||
# Unwrap (Monadic Return Operation)
|
# Unwrap (Monadic Return Operation)
|
||||||
unwrap = F: F.error;
|
unwrapFault = F: F.error;
|
||||||
|
|
||||||
# Map (Monadic Bind Operation)
|
# 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