Home

gen @e423ae92176cc817abac3a983cb7990c809d063e - refs - log -
-
https://git.jolheiser.com/gen.git
Generate Go flags and Nix module
gen / README.md
- raw -
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# gen

Generates Go flags and NixOS module options from a CUE schema.

```sh
go tool gen --schema schema.cue --go args.gen.go --nix nix/options.gen.nix
```

`-check` reports stale outputs instead of writing them.

## Go

- A struct per CUE struct (root: `-go-type`; nested: lowerCamel label + `-go-type-suffix`, e.g. `sshArgs`)
- `defaultArgs()` returns the CUE defaults
- `registerFlags(fs, c)` defines a flag per field
- `c.validate()` checks bounds, enums, regexes and `!=""`

Flags are named by path (`ssh.clone-url`), fields by PascalCase label with initialisms (`CloneURL`). List flags are repeatable; the first use replaces the default.

## Nix

A `{ lib }:` function returning options for an RFC 42 settings submodule. Types follow the constraints: `ints.between`, `enum`, `strMatching`, `nonEmptyStr`, and `nullOr` (default `null`) for optional fields.

Like `@go(type, parse)`, `@nix(type, apply)` lets a field look different in Nix than on the wire. `apply` also runs on the default, so a non-empty CUE default must fit the Nix type; set `@nix(default)` if it doesn't.

## Attributes

Set on a field or on a referenced definition (e.g. `#Port`); the field wins, then the nearest definition.

| Attribute | Meaning |
|---|---|
| `@go(name=X)` | Go field name (field only) |
| `@go(type=T)` | Go type name of a struct |
| `@go(type=T, parse=F, import=P)` | Custom type; `F` is your `func(string) (T, error)`, `P` space-separated imports. On a list, `T` is the element type |
| `@nix(type=E)` | Raw Nix type (whole list, on a list) |
| `@nix(apply=F)` | Nix function mapping the option's value back to what the app expects (whole list, on a list; mapped over elements when set on the element's definition) |
| `@nix(import=N)` | Space-separated names that `F`/`E` use; each becomes an argument of the generated function for you to pass in |
| `@nix(default=V)` | NixOS default (field only); a string for string fields, CUE otherwise |
| `@nix(skip)` | Omit from Nix |

## Supported

`bool`, `int`, `float`, `number`, `string`, open lists of those, nested structs, literal enums, and bounds, regexes and `!=""` on scalars. Anything else is an error.

## Supplementary files

Example supplementary wiring files are provided in [contrib](./contrib).