Nix Code Generation

paradox generate --nix [--path PATH]

Product Types

Input:

type Person
  name: Text
  age: Integer
  admin: Boolean

Output:

mkPerson = { name, age, admin }: { inherit name; inherit age; inherit admin; };

Products use attribute set destructuring for the constructor.

Union Types

union Status
  active
  inactive
  pending: Text
status = {
  active = { _tag = "active"; };
  inactive = { _tag = "inactive"; };
  pending = value: { _tag = "pending"; _value = value; };
};

Unions are attribute sets of constructors using _tag / _value encoding.

Wrap Types

wrap Email: Text
mkEmail = value: { _unwrap = value; };
unwrapEmail = w: w._unwrap;

Wrap types use { _unwrap = value; } attribute sets.

Validators

validPerson = input:
  let errors = builtins.filter (e: e != null) [
    (if !(input.name != "") then "Name required" else null)
  ]; in
  if errors == [] then input
  else builtins.throw (builtins.concatStringsSep "; " errors);

isValidPerson = input: (builtins.tryEval (validPerson input)).success;

Validators accumulate errors as a list, using builtins.throw for invalid inputs and builtins.tryEval for boolean checks.

Collections

Nix has no native set type. Arrays and sets are both represented as lists. Array-to-set casts deduplicate and sort; set-to-array casts sort.