Commit Graph
5 Commits
Author SHA1 Message Date
LGUG2Z 080fef2a5a chore(layout): move the files of the repository to where they live in the komorebi monorepo 2026-11-05 13:05:34 -08:00
LGUG2Z 50ce2860c9 feat(nixgen): write the hash of the schema as the first line of the output
Consumers that check whether generated options are older than their schema
(komorebi-for-kde's `schema-fresh`) compare a `# schema-sha256: <hex>` first
line with the hash of schema.json. That line was added by hand in the
consumer's justfile with `printf` and `nix hash file`, so the tool's own output
could never pass the check.

Write the line from the tool. `schema_hash_header` returns it for the raw bytes
of the schema file, in lowercase hex, which is the digest `nix hash file --type
sha256 --base16` prints. The binary puts it before the generated code; the
library functions that return generated code are unchanged, so library users get
no new line unless they ask for it.

Adds the sha2 dependency.

Verified: cargo test passes (49, including two known SHA-256 vectors). On the
komorebi-for-kde schema.json the first line equals `nix hash file --type sha256
--base16 schema.json`.
2026-10-31 08:40:11 -07:00
LGUG2Z e28a8b7956 fix(nixgen): make the generated output order the same on every run
The generator kept its definitions, reference counts and type bindings in
HashMap and HashSet, whose iteration order changes from run to run. The
topological sort walked the keys in that order, so which shared types were
bound before their dependents, and so how some types were written out,
changed between runs. Running the same schema eight times gave eight
different files.

Use BTreeMap and BTreeSet in generator.rs and prepare.rs so every walk is in
sorted key order. The final let block was already sorted by identifier and is
unchanged.

Verified: cargo test passes (48 tests). Running the release binary on the KDE
schema.json eight times gave one identical output; the same eight runs before
the change gave eight different outputs.
2026-10-31 08:38:06 -07:00
LGUG2Z a26728ab19 build(nix): add cargo-udeps and cargo-outdated to devshell 2025-12-24 17:20:19 -08:00
LGUG2Z 7763d679a5 feat(nixgen): initial implementation of schemars-nixgen
This crate converts JSON Schema (as generated by schemars) into
strongly-typed Nix option definitions for the NixOS module system.

The primary motivation was to generate NixOS module options for komorebi
and komorebi-bar configurations, allowing users to write type-checked
Nix configurations that map directly to the JSON config files expected
by those applications.

Some high level notes on the implementation:

* Handles all common JSON Schema patterns including primitives, arrays,
  objects, enums, refs, oneOf/anyOf/allOf, and nullable types
* Detects and correctly generates externally-tagged unions (Rust's
  default serde enum representation) as submodules with optional fields
* Detects internally-tagged unions and mixed enum+object unions
* Generates let bindings for frequently-used type definitions to avoid
  repetition in the output (e.g., large enums like Base16Value)
* Uses topological sorting to generate type bindings in dependency order,
  ensuring nested refs are deduplicated correctly
* Handles patternProperties for index-based configuration patterns
2025-12-24 17:20:16 -08:00