mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-15 21:32:26 +01:00
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
130 lines
3.2 KiB
Nix
130 lines
3.2 KiB
Nix
{
|
|
inputs = {
|
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
|
flake-parts.url = "github:hercules-ci/flake-parts";
|
|
crane.url = "github:ipetkov/crane";
|
|
rust-overlay.url = "github:oxalica/rust-overlay";
|
|
rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
|
|
treefmt-nix.url = "github:numtide/treefmt-nix";
|
|
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
|
|
git-hooks-nix.url = "github:cachix/git-hooks.nix";
|
|
git-hooks-nix.inputs.nixpkgs.follows = "nixpkgs";
|
|
};
|
|
|
|
outputs =
|
|
inputs@{
|
|
nixpkgs,
|
|
flake-parts,
|
|
crane,
|
|
rust-overlay,
|
|
...
|
|
}:
|
|
flake-parts.lib.mkFlake { inherit inputs; } {
|
|
imports = [
|
|
inputs.treefmt-nix.flakeModule
|
|
inputs.git-hooks-nix.flakeModule
|
|
];
|
|
|
|
systems = [
|
|
"x86_64-linux"
|
|
"aarch64-linux"
|
|
"aarch64-darwin"
|
|
];
|
|
|
|
perSystem =
|
|
{
|
|
config,
|
|
system,
|
|
...
|
|
}:
|
|
let
|
|
pkgs = import nixpkgs {
|
|
inherit system;
|
|
overlays = [ rust-overlay.overlays.default ];
|
|
};
|
|
|
|
rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
|
|
nightlyRustfmt = pkgs.rust-bin.nightly.latest.rustfmt;
|
|
rustToolchainWithNightlyRustfmt = pkgs.symlinkJoin {
|
|
name = "rust-toolchain-with-nightly-rustfmt";
|
|
paths = [
|
|
nightlyRustfmt
|
|
rustToolchain
|
|
];
|
|
};
|
|
|
|
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
|
|
|
|
src = pkgs.lib.cleanSourceWith {
|
|
src = ./.;
|
|
filter = craneLib.filterCargoSources;
|
|
};
|
|
|
|
commonArgs = {
|
|
inherit src;
|
|
strictDeps = true;
|
|
};
|
|
|
|
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
|
|
|
|
schemars-nixgen = craneLib.buildPackage (
|
|
commonArgs
|
|
// {
|
|
inherit cargoArtifacts;
|
|
}
|
|
);
|
|
in
|
|
{
|
|
checks = {
|
|
clippy = craneLib.cargoClippy (
|
|
commonArgs
|
|
// {
|
|
inherit cargoArtifacts;
|
|
}
|
|
);
|
|
tests = craneLib.cargoTest (
|
|
commonArgs
|
|
// {
|
|
inherit cargoArtifacts;
|
|
}
|
|
);
|
|
};
|
|
|
|
packages = {
|
|
inherit schemars-nixgen;
|
|
default = schemars-nixgen;
|
|
};
|
|
|
|
devShells.default = pkgs.mkShell {
|
|
inputsFrom = [ schemars-nixgen ];
|
|
packages = [
|
|
rustToolchainWithNightlyRustfmt
|
|
pkgs.bacon
|
|
];
|
|
};
|
|
|
|
pre-commit = {
|
|
check.enable = true;
|
|
settings.hooks.treefmt = {
|
|
enable = true;
|
|
package = config.treefmt.build.wrapper;
|
|
pass_filenames = false;
|
|
};
|
|
};
|
|
|
|
treefmt = {
|
|
projectRootFile = "flake.nix";
|
|
programs = {
|
|
rustfmt = {
|
|
enable = true;
|
|
package = nightlyRustfmt;
|
|
};
|
|
taplo.enable = true;
|
|
nixfmt.enable = true;
|
|
deadnix.enable = true;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
}
|