···230230 in
231231 toINI_ (gitFlattenAttrs attrs);
232232233233+ # mkKeyValueDefault wrapper that handles dconf INI quirks.
234234+ # The main differences of the format is that it requires strings to be quoted.
235235+ mkDconfKeyValue = mkKeyValueDefault { mkValueString = v: toString (lib.gvariant.mkValue v); } "=";
236236+237237+ # Generates INI in dconf keyfile style. See https://help.gnome.org/admin/system-admin-guide/stable/dconf-keyfiles.html.en
238238+ # for details.
239239+ toDconfINI = toINI { mkKeyValue = mkDconfKeyValue; };
240240+233241 /* Generates JSON from an arbitrary (non-function) value.
234242 * For more information see the documentation of the builtin.
235243 */
+290
lib/gvariant.nix
···11+# This file is based on https://github.com/nix-community/home-manager
22+# Copyright (c) 2017-2022 Home Manager contributors
33+#
44+55+66+{ lib }:
77+88+/* A partial and basic implementation of GVariant formatted strings.
99+ See https://docs.gtk.org/glib/gvariant-format-strings.html for detauls.
1010+1111+ Note, this API is not considered fully stable and it might therefore
1212+ change in backwards incompatible ways without prior notice.
1313+*/
1414+let
1515+ inherit (lib)
1616+ concatMapStringsSep concatStrings escape head replaceStrings;
1717+1818+ mkPrimitive = t: v: {
1919+ _type = "gvariant";
2020+ type = t;
2121+ value = v;
2222+ __toString = self: "@${self.type} ${toString self.value}"; # https://docs.gtk.org/glib/gvariant-text.html
2323+ };
2424+2525+ type = {
2626+ arrayOf = t: "a${t}";
2727+ maybeOf = t: "m${t}";
2828+ tupleOf = ts: "(${concatStrings ts})";
2929+ dictionaryEntryOf = nameType: valueType: "{${nameType}${valueType}}";
3030+ string = "s";
3131+ boolean = "b";
3232+ uchar = "y";
3333+ int16 = "n";
3434+ uint16 = "q";
3535+ int32 = "i";
3636+ uint32 = "u";
3737+ int64 = "x";
3838+ uint64 = "t";
3939+ double = "d";
4040+ variant = "v";
4141+ };
4242+4343+ /* Check if a value is a GVariant value
4444+4545+ Type:
4646+ isGVariant :: Any -> Bool
4747+ */
4848+ isGVariant = v: v._type or "" == "gvariant";
4949+5050+in
5151+rec {
5252+5353+ inherit type isGVariant;
5454+5555+ /* Returns the GVariant value that most closely matches the given Nix value.
5656+ If no GVariant value can be found unambiguously then error is thrown.
5757+5858+ Type:
5959+ mkValue :: Any -> gvariant
6060+ */
6161+ mkValue = v:
6262+ if builtins.isBool v then
6363+ mkBoolean v
6464+ else if builtins.isFloat v then
6565+ mkDouble v
6666+ else if builtins.isString v then
6767+ mkString v
6868+ else if builtins.isList v then
6969+ mkArray v
7070+ else if isGVariant v then
7171+ v
7272+ else
7373+ throw "The GVariant type of ${v} can't be inferred.";
7474+7575+ /* Returns the GVariant array from the given type of the elements and a Nix list.
7676+7777+ Type:
7878+ mkArray :: [Any] -> gvariant
7979+8080+ Example:
8181+ # Creating a string array
8282+ lib.gvariant.mkArray [ "a" "b" "c" ]
8383+ */
8484+ mkArray = elems:
8585+ let
8686+ vs = map mkValue (lib.throwIf (elems == [ ]) "Please create empty array with mkEmptyArray." elems);
8787+ elemType = lib.throwIfNot (lib.all (t: (head vs).type == t) (map (v: v.type) vs))
8888+ "Elements in a list should have same type."
8989+ (head vs).type;
9090+ in
9191+ mkPrimitive (type.arrayOf elemType) vs // {
9292+ __toString = self:
9393+ "@${self.type} [${concatMapStringsSep "," toString self.value}]";
9494+ };
9595+9696+ /* Returns the GVariant array from the given empty Nix list.
9797+9898+ Type:
9999+ mkEmptyArray :: gvariant.type -> gvariant
100100+101101+ Example:
102102+ # Creating an empty string array
103103+ lib.gvariant.mkEmptyArray (lib.gvariant.type.string)
104104+ */
105105+ mkEmptyArray = elemType: mkPrimitive (type.arrayOf elemType) [ ] // {
106106+ __toString = self: "@${self.type} []";
107107+ };
108108+109109+110110+ /* Returns the GVariant variant from the given Nix value. Variants are containers
111111+ of different GVariant type.
112112+113113+ Type:
114114+ mkVariant :: Any -> gvariant
115115+116116+ Example:
117117+ lib.gvariant.mkArray [
118118+ (lib.gvariant.mkVariant "a string")
119119+ (lib.gvariant.mkVariant (lib.gvariant.mkInt32 1))
120120+ ]
121121+ */
122122+ mkVariant = elem:
123123+ let gvarElem = mkValue elem;
124124+ in mkPrimitive type.variant gvarElem // {
125125+ __toString = self: "<${toString self.value}>";
126126+ };
127127+128128+ /* Returns the GVariant dictionary entry from the given key and value.
129129+130130+ Type:
131131+ mkDictionaryEntry :: String -> Any -> gvariant
132132+133133+ Example:
134134+ # A dictionary describing an Epiphany’s search provider
135135+ [
136136+ (lib.gvariant.mkDictionaryEntry "url" (lib.gvariant.mkVariant "https://duckduckgo.com/?q=%s&t=epiphany"))
137137+ (lib.gvariant.mkDictionaryEntry "bang" (lib.gvariant.mkVariant "!d"))
138138+ (lib.gvariant.mkDictionaryEntry "name" (lib.gvariant.mkVariant "DuckDuckGo"))
139139+ ]
140140+ */
141141+ mkDictionaryEntry =
142142+ # The key of the entry
143143+ name:
144144+ # The value of the entry
145145+ value:
146146+ let
147147+ name' = mkValue name;
148148+ value' = mkValue value;
149149+ dictionaryType = type.dictionaryEntryOf name'.type value'.type;
150150+ in
151151+ mkPrimitive dictionaryType { inherit name value; } // {
152152+ __toString = self: "@${self.type} {${name'},${value'}}";
153153+ };
154154+155155+ /* Returns the GVariant maybe from the given element type.
156156+157157+ Type:
158158+ mkMaybe :: gvariant.type -> Any -> gvariant
159159+ */
160160+ mkMaybe = elemType: elem:
161161+ mkPrimitive (type.maybeOf elemType) elem // {
162162+ __toString = self:
163163+ if self.value == null then
164164+ "@${self.type} nothing"
165165+ else
166166+ "just ${toString self.value}";
167167+ };
168168+169169+ /* Returns the GVariant nothing from the given element type.
170170+171171+ Type:
172172+ mkNothing :: gvariant.type -> gvariant
173173+ */
174174+ mkNothing = elemType: mkMaybe elemType null;
175175+176176+ /* Returns the GVariant just from the given Nix value.
177177+178178+ Type:
179179+ mkJust :: Any -> gvariant
180180+ */
181181+ mkJust = elem: let gvarElem = mkValue elem; in mkMaybe gvarElem.type gvarElem;
182182+183183+ /* Returns the GVariant tuple from the given Nix list.
184184+185185+ Type:
186186+ mkTuple :: [Any] -> gvariant
187187+ */
188188+ mkTuple = elems:
189189+ let
190190+ gvarElems = map mkValue elems;
191191+ tupleType = type.tupleOf (map (e: e.type) gvarElems);
192192+ in
193193+ mkPrimitive tupleType gvarElems // {
194194+ __toString = self:
195195+ "@${self.type} (${concatMapStringsSep "," toString self.value})";
196196+ };
197197+198198+ /* Returns the GVariant boolean from the given Nix bool value.
199199+200200+ Type:
201201+ mkBoolean :: Bool -> gvariant
202202+ */
203203+ mkBoolean = v:
204204+ mkPrimitive type.boolean v // {
205205+ __toString = self: if self.value then "true" else "false";
206206+ };
207207+208208+ /* Returns the GVariant string from the given Nix string value.
209209+210210+ Type:
211211+ mkString :: String -> gvariant
212212+ */
213213+ mkString = v:
214214+ let sanitize = s: replaceStrings [ "\n" ] [ "\\n" ] (escape [ "'" "\\" ] s);
215215+ in mkPrimitive type.string v // {
216216+ __toString = self: "'${sanitize self.value}'";
217217+ };
218218+219219+ /* Returns the GVariant object path from the given Nix string value.
220220+221221+ Type:
222222+ mkObjectpath :: String -> gvariant
223223+ */
224224+ mkObjectpath = v:
225225+ mkPrimitive type.string v // {
226226+ __toString = self: "objectpath '${escape [ "'" ] self.value}'";
227227+ };
228228+229229+ /* Returns the GVariant uchar from the given Nix int value.
230230+231231+ Type:
232232+ mkUchar :: Int -> gvariant
233233+ */
234234+ mkUchar = mkPrimitive type.uchar;
235235+236236+ /* Returns the GVariant int16 from the given Nix int value.
237237+238238+ Type:
239239+ mkInt16 :: Int -> gvariant
240240+ */
241241+ mkInt16 = mkPrimitive type.int16;
242242+243243+ /* Returns the GVariant uint16 from the given Nix int value.
244244+245245+ Type:
246246+ mkUint16 :: Int -> gvariant
247247+ */
248248+ mkUint16 = mkPrimitive type.uint16;
249249+250250+ /* Returns the GVariant int32 from the given Nix int value.
251251+252252+ Type:
253253+ mkInt32 :: Int -> gvariant
254254+ */
255255+ mkInt32 = v:
256256+ mkPrimitive type.int32 v // {
257257+ __toString = self: toString self.value;
258258+ };
259259+260260+ /* Returns the GVariant uint32 from the given Nix int value.
261261+262262+ Type:
263263+ mkUint32 :: Int -> gvariant
264264+ */
265265+ mkUint32 = mkPrimitive type.uint32;
266266+267267+ /* Returns the GVariant int64 from the given Nix int value.
268268+269269+ Type:
270270+ mkInt64 :: Int -> gvariant
271271+ */
272272+ mkInt64 = mkPrimitive type.int64;
273273+274274+ /* Returns the GVariant uint64 from the given Nix int value.
275275+276276+ Type:
277277+ mkUint64 :: Int -> gvariant
278278+ */
279279+ mkUint64 = mkPrimitive type.uint64;
280280+281281+ /* Returns the GVariant double from the given Nix float value.
282282+283283+ Type:
284284+ mkDouble :: Float -> gvariant
285285+ */
286286+ mkDouble = v:
287287+ mkPrimitive type.double v // {
288288+ __toString = self: toString self.value;
289289+ };
290290+}