Home

gen @e423ae92176cc817abac3a983cb7990c809d063e - refs - log -
-
https://git.jolheiser.com/gen.git
Generate Go flags and Nix module
drwxrwxrwx
117 B
contrib/
drwxrwxrwx
251 B
testdata/
-rw-r--r--
6 B
.gitignore
-rw-r--r--
2.3 kB
README.md
-rw-r--r--
5.7 kB
constraints.go
-rw-r--r--
902 B
go.mod
-rw-r--r--
4.8 kB
go.sum
-rw-r--r--
15 kB
golang.go
-rw-r--r--
3.4 kB
main.go
-rw-r--r--
3.9 kB
main_test.go
-rw-r--r--
14 kB
model.go
-rw-r--r--
3.6 kB
names.go
-rw-r--r--
10 kB
nix.go

gen

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

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

-check reports stale outputs instead of writing them.

Go

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.