Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkgs.formats: Add libconfig format generator #246115

Merged
merged 3 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkgs/pkgs-lib/formats.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ rec {
generate = ...;

});

Please note that `pkgs` may not always be available for use due to the split
options doc build introduced in fc614c37c653, so lazy evaluation of only the
'type' field is required.

*/


inherit (import ./formats/java-properties/default.nix { inherit lib pkgs; })
javaProperties;

libconfig = (import ./formats/libconfig/default.nix { inherit lib pkgs; }).format;
h7x4 marked this conversation as resolved.
Show resolved Hide resolved

json = {}: {

type = with lib.types; let
Expand Down
121 changes: 121 additions & 0 deletions pkgs/pkgs-lib/formats/libconfig/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{ lib
, pkgs
}:
let
inherit (pkgs) buildPackages callPackage;
# Implementation notes:
# Libconfig spec: https://hyperrealm.github.io/libconfig/libconfig_manual.html
#
# Since libconfig does not allow setting names to start with an underscore,
# this is used as a prefix for both special types and include directives.
#
# The difference between 32bit and 64bit values became optional in libconfig
# 1.5, so we assume 64bit values for all numbers.

libconfig-generator = buildPackages.rustPlatform.buildRustPackage {
name = "libconfig-generator";
version = "0.1.0";
src = ./src;

passthru.updateScript = ./update.sh;

cargoLock.lockFile = ./src/Cargo.lock;
};

libconfig-validator = buildPackages.runCommandCC "libconfig-validator"
{
buildInputs = with buildPackages; [ libconfig ];
}
''
mkdir -p "$out/bin"
$CC -lconfig -x c - -o "$out/bin/libconfig-validator" ${./validator.c}
'';
h7x4 marked this conversation as resolved.
Show resolved Hide resolved
in
{
format = { generator ? libconfig-generator, validator ? libconfig-validator }: {
inherit generator;

type = with lib.types;
let
valueType = (oneOf [
bool
int
float
str
path
(attrsOf valueType)
(listOf valueType)
h7x4 marked this conversation as resolved.
Show resolved Hide resolved
]) // {
description = "libconfig value";
};
in
attrsOf valueType;

lib = {
mkHex = value: {
_type = "hex";
inherit value;
};
mkOctal = value: {
_type = "octal";
inherit value;
};
mkFloat = value: {
_type = "float";
inherit value;
};
mkArray = value: {
_type = "array";
inherit value;
};
mkList = value: {
_type = "list";
inherit value;
};
};

generate = name: value:
callPackage
({
stdenvNoCC
, libconfig-generator
, libconfig-validator
, writeText
}: stdenvNoCC.mkDerivation rec {
inherit name;

dontUnpack = true;

json = builtins.toJSON value;
passAsFile = [ "json" ];

strictDeps = true;
nativeBuildInputs = [ libconfig-generator ];
buildPhase = ''
runHook preBuild
libconfig-generator < $jsonPath > output.cfg
runHook postBuild
'';

doCheck = true;
nativeCheckInputs = [ libconfig-validator ];
checkPhase = ''
runHook preCheck
libconfig-validator output.cfg
runHook postCheck
'';

installPhase = ''
runHook preInstall
mv output.cfg $out
runHook postInstall
'';

passthru.json = writeText "${name}.json" json;
})
{
libconfig-generator = generator;
libconfig-validator = validator;
};
};
}
40 changes: 40 additions & 0 deletions pkgs/pkgs-lib/formats/libconfig/src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions pkgs/pkgs-lib/formats/libconfig/src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "libconfig-generator"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = "1.0.178"
serde_json = "1.0.104"
Loading