A tiny & fast composable type system for Nix, in Nix.
Named after the little penguin.
string, int, etc)union, attrsOf, etc)Basic verification is done with the type function verify:
{ korora }:
let
t = korora.string;
value = 1;
# Error contains the string "Expected type 'string' but value '1' is of type 'int'"
error = t.verify 1;
in if error != null then throw error else value
Errors are returned as a string.
On success null is returned.
For convenience you can also check a value on-the-fly:
{ korora }:
let
t = korora.string;
value = 1;
# Same error as previous example, but `check` throws.
value = t.check value value;
in value
On error check throws. On success it returns the value that was passed in.
For usage example see tests.nix.
lib.types.typedefDeclare a custom type using a bool function
name
: Name of the type as a string
verify
: Verification function returning a bool.
lib.types.typedef'Declare a custom type using an option function.
name
: Name of the type as a string
verify
: Verification function returning null on success & a string with error message on error.
lib.types.stringString
lib.types.strType alias for string
lib.types.anyAny
lib.types.neverNever
lib.types.intInt
lib.types.floatSingle precision floating point
lib.types.numberEither an int or a float
lib.types.boolBool
lib.types.nullNull
lib.types.attrsAttribute with undefined attribute types
lib.types.listAttribute with undefined element types
lib.types.functionFunction
lib.types.pathPath
lib.types.pathLikeValue that may not technically be a path, but has path-like properties
Either an actual path ./foo, a derivation, or a string
lib.types.derivationDerivation
lib.types.typeType
lib.types.optionOption
t
: Null or t
lib.types.listOflistOf
t
: Element type
lib.types.attrsOflistOf
t
: Attribute value type
lib.types.unionunion<types...>
types
: Any of
lib.types.intersectionintersection<types...>
types
: All of
lib.types.renamerename<name, type>
Because some polymorphic types such as attrsOf inherits names from it's sub-types we need to erase the name to not cause infinite recursion.
myType = types.attrsOf (
types.rename "eitherType" (types.union [
types.string
myType
])
);
name
: Function argument
type
: Function argument
lib.types.structstruct<name, members...>
korora.struct "myStruct" {
foo = types.string;
}
By default, all attribute names must be present in a struct. It is possible to override this by specifying totality. Here is how to do this:
(korora.struct "myStruct" {
foo = types.string;
}).override { total = false; }
This means that a myStruct struct can have any of the keys omitted. Thus these are valid:
let
s1 = { };
s2 = { foo = "bar"; }
in ...
By default, unknown attribute names are allowed.
It is possible to override this by specifying unknown.
(korora.struct "myStruct" {
foo = types.string;
}).override { unknown = false; }
This means that
{
foo = "bar";
baz = "hello";
}
is normally valid, but not when unknown is set to false.
Because Nix lacks primitive operations to iterate over attribute sets dynamically without allocation this function allocates one intermediate attribute set per struct verification.
Custom struct verification functions can be added as such:
(types.struct "testStruct2" {
x = types.int;
y = types.int;
}).override {
verify = v: if v.x + v.y == 2 then "VERBOTEN" else null;
};
name
: Name of struct type as a string
members
: Attribute set of type definitions.
lib.types.optionalAttroptionalAttr
t
: Function argument
lib.types.enumenum<name, elems...>
name
: Name of enum type as a string
elems
: List of allowable enum members
lib.types.tupletuple<elems...>
members
: List of tuple memeber types
lib.types.defunCreate a wrapped type checked function.
name
: Function argument
args
: Function argument
T
: Function argument
f
: Function argument
60 commits
8 commits
Nix
100.0%
A tiny & fast composable type system for Nix, in Nix.
Named after the little penguin.
string, int, etc)union, attrsOf, etc)Basic verification is done with the type function verify:
{ korora }:
let
t = korora.string;
value = 1;
# Error contains the string "Expected type 'string' but value '1' is of type 'int'"
error = t.verify 1;
in if error != null then throw error else value
Errors are returned as a string.
On success null is returned.
For convenience you can also check a value on-the-fly:
{ korora }:
let
t = korora.string;
value = 1;
# Same error as previous example, but `check` throws.
value = t.check value value;
in value
On error check throws. On success it returns the value that was passed in.
For usage example see tests.nix.
lib.types.typedefDeclare a custom type using a bool function
name
: Name of the type as a string
verify
: Verification function returning a bool.
lib.types.typedef'Declare a custom type using an option function.
name
: Name of the type as a string
verify
: Verification function returning null on success & a string with error message on error.
lib.types.stringString
lib.types.strType alias for string
lib.types.anyAny
lib.types.neverNever
lib.types.intInt
lib.types.floatSingle precision floating point
lib.types.numberEither an int or a float
lib.types.boolBool
lib.types.nullNull
lib.types.attrsAttribute with undefined attribute types
lib.types.listAttribute with undefined element types
lib.types.functionFunction
lib.types.pathPath
lib.types.pathLikeValue that may not technically be a path, but has path-like properties
Either an actual path ./foo, a derivation, or a string
lib.types.derivationDerivation
lib.types.typeType
lib.types.optionOption
t
: Null or t
lib.types.listOflistOf
t
: Element type
lib.types.attrsOflistOf
t
: Attribute value type
lib.types.unionunion<types...>
types
: Any of
lib.types.intersectionintersection<types...>
types
: All of
lib.types.renamerename<name, type>
Because some polymorphic types such as attrsOf inherits names from it's sub-types we need to erase the name to not cause infinite recursion.
myType = types.attrsOf (
types.rename "eitherType" (types.union [
types.string
myType
])
);
name
: Function argument
type
: Function argument
lib.types.structstruct<name, members...>
korora.struct "myStruct" {
foo = types.string;
}
By default, all attribute names must be present in a struct. It is possible to override this by specifying totality. Here is how to do this:
(korora.struct "myStruct" {
foo = types.string;
}).override { total = false; }
This means that a myStruct struct can have any of the keys omitted. Thus these are valid:
let
s1 = { };
s2 = { foo = "bar"; }
in ...
By default, unknown attribute names are allowed.
It is possible to override this by specifying unknown.
(korora.struct "myStruct" {
foo = types.string;
}).override { unknown = false; }
This means that
{
foo = "bar";
baz = "hello";
}
is normally valid, but not when unknown is set to false.
Because Nix lacks primitive operations to iterate over attribute sets dynamically without allocation this function allocates one intermediate attribute set per struct verification.
Custom struct verification functions can be added as such:
(types.struct "testStruct2" {
x = types.int;
y = types.int;
}).override {
verify = v: if v.x + v.y == 2 then "VERBOTEN" else null;
};
name
: Name of struct type as a string
members
: Attribute set of type definitions.
lib.types.optionalAttroptionalAttr
t
: Function argument
lib.types.enumenum<name, elems...>
name
: Name of enum type as a string
elems
: List of allowable enum members
lib.types.tupletuple<elems...>
members
: List of tuple memeber types
lib.types.defunCreate a wrapped type checked function.
name
: Function argument
args
: Function argument
T
: Function argument
f
: Function argument
60 commits
8 commits
Nix
100.0%