1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 19:46:05 +01:00

lib: add GVariant datatype and functions

This commit is contained in:
Robert Helgesson 2020-03-09 23:25:23 +01:00
parent 3673107bc4
commit ac9e44a831
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
5 changed files with 241 additions and 3 deletions

View file

@ -1,4 +1,7 @@
{ lib, dag ? import ./dag.nix { inherit lib; } }:
{ lib
, dag ? import ./dag.nix { inherit lib; }
, gvariant ? import ./gvariant.nix { inherit lib; }
}:
with lib;
@ -6,9 +9,13 @@ let
typesDag = import ./types-dag.nix { inherit dag lib; };
# Needed since the type is called gvariant and its merge attribute
# must refer back to the type.
gvar = gvariant;
in
{
rec {
inherit (typesDag) dagOf listOrDagOf;
@ -56,4 +63,35 @@ in
};
};
gvariant = mkOptionType rec {
name = "gvariant";
description = "GVariant value";
check = v: gvar.mkValue v != null;
merge = loc: defs:
let
vdefs = map (d: d // { value = gvar.mkValue d.value; }) defs;
vals = map (d: d.value) vdefs;
defTypes = map (x: x.type) vals;
sameOrNull = x: y: if x == y then y else null;
# A bit naive to just check the first entry…
sharedDefType = foldl' sameOrNull (head defTypes) defTypes;
allChecked = all (x: check x) vals;
in
if sharedDefType == null then
throw ("Cannot merge definitions of `${showOption loc}' with"
+ " mismatched GVariant types given in"
+ " ${showFiles (getFiles defs)}.")
else if gvar.isArray sharedDefType && allChecked then
(types.listOf gvariant).merge
loc (map (d: d // { value = d.value.value; } ) vdefs)
else if gvar.isTuple sharedDefType && allChecked then
mergeOneOption loc defs
else if gvar.type.string == sharedDefType && allChecked then
types.str.merge loc defs
else if gvar.type.double == sharedDefType && allChecked then
types.float.merge loc defs
else
mergeDefaultOption loc defs;
};
}