00adb6e8f3
while trying to yantsify `mkSecrets` in https://cl.tvl.fyi/c/depot/+/4688, I(zseri) needed to debug a failing evaluation which boiled down to a result.ok containing something which wasn't boolean, but the error message didn't indicate where that value came from. I debugged yants and found that the only place which didn't simply combine boolean values or use functions which always return booleans, I managed to isolate the error to the `pred v` expression. To avoid the necessity to debug yants to find this, I improve the error message for this case to mention that - a restriction predicate is invalid - what's the name of the failing restriction - the unexpected predicate return value Change-Id: I6c570a33ccc5afc445f208e2e8855c49fb37abaf Reviewed-on: https://cl.tvl.fyi/c/depot/+/4698 Tested-by: BuildkiteCI Reviewed-by: zseri <zseri.devel@ytrizja.de> Reviewed-by: tazjin <mail@tazj.in> Autosubmit: zseri <zseri.devel@ytrizja.de> |
||
---|---|---|
.. | ||
screenshots | ||
tests | ||
default.nix | ||
README.md |
yants
This is a tiny type-checker for data in Nix, written in Nix.
Features
- Checking of primitive types (
int
,string
etc.) - Checking polymorphic types (
option
,list
,either
) - Defining & checking struct/record types
- Defining & matching enum types
- Defining & matching sum types
- Defining function signatures (including curried functions)
- Types are composable!
option string
!list (either int (option float))
! - Type errors also compose!
Currently lacking:
- Any kind of inference
- Convenient syntax for attribute-set function signatures
Primitives & simple polymorphism
Structs
Nested structs!
Enums!
Functions!
Usage
Yants can be imported from its default.nix
. A single attribute (lib
) can be
passed, which will otherwise be imported from <nixpkgs>
.
TIP: You do not need to clone my whole repository to use Yants! It is split out
into the nix/yants
branch which you can clone with, for example, git clone -b nix/yants https://git.tazj.in yants
.
Examples for the most common import methods would be:
-
Import into scope with
with
:with (import ./default.nix {}); # ... Nix code that uses yants ...
-
Import as a named variable:
let yants = import ./default.nix {}; in yants.string "foo" # or other uses ...
-
Overlay into
pkgs.lib
:# wherever you import your package set (e.g. from <nixpkgs>): import <nixpkgs> { overlays = [ (self: super: { lib = super.lib // { yants = import ./default.nix { inherit (super) lib; }; }; }) ]; } # yants now lives at lib.yants, besides the other library functions!
Please see my Nix one-pager for more generic information about the Nix language and what the above constructs mean.
Stability
The current API of Yants is not yet considered stable, but it works fine and should continue to do so even if used at an older version.
Yants' tests use Nix versions above 2.2 - compatibility with older versions is not guaranteed.