Something went wrong. Try again.
What happens if I push all of nixpkgs
Something went wrong. Try again.
Nix
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593/** A partial and basic implementation of GVariant formatted strings. See [GVariant Format Strings](https://docs.gtk.org/glib/gvariant-format-strings.html) for details.
:::{.warning} This API is not considered fully stable and it might therefore change in backwards incompatible ways without prior notice. :::*/
# This file is based on https://github.com/nix-community/home-manager# Copyright (c) 2017-2022 Home Manager contributors{ lib }:
let inherit (lib) concatMapStringsSep concatStrings escape head replaceString ;
mkPrimitive = t: v: { _type = "gvariant"; type = t; value = v; __toString = self: "@${self.type} ${toString self.value}"; # https://docs.gtk.org/glib/gvariant-text.html };
type = { arrayOf = t: "a${t}"; maybeOf = t: "m${t}"; tupleOf = ts: "(${concatStrings ts})"; dictionaryEntryOf = nameType: valueType: "{${nameType}${valueType}}"; string = "s"; boolean = "b"; uchar = "y"; int16 = "n"; uint16 = "q"; int32 = "i"; uint32 = "u"; int64 = "x"; uint64 = "t"; double = "d"; variant = "v"; };
inrec {
inherit type;
/** Check if a value is a GVariant value
# Inputs
`v`
: value to check
# Type
``` isGVariant :: Any -> Bool ``` */ isGVariant = v: v._type or "" == "gvariant";
intConstructors = [ { name = "mkInt32"; type = type.int32; min = -2147483648; max = 2147483647; } { name = "mkUint32"; type = type.uint32; min = 0; max = 4294967295; } { name = "mkInt64"; type = type.int64; # Nix does not support such large numbers. min = null; max = null; } { name = "mkUint64"; type = type.uint64; min = 0; # Nix does not support such large numbers. max = null; } { name = "mkInt16"; type = type.int16; min = -32768; max = 32767; } { name = "mkUint16"; type = type.uint16; min = 0; max = 65535; } { name = "mkUchar"; type = type.uchar; min = 0; max = 255; } ];
/** Returns the GVariant value that most closely matches the given Nix value. If no GVariant value can be found unambiguously then error is thrown.
# Inputs
`v`
: 1\. Function argument
# Type
``` mkValue :: Any -> gvariant ``` */ mkValue = v: if builtins.isBool v then mkBoolean v else if builtins.isFloat v then mkDouble v else if builtins.isString v then mkString v else if builtins.isList v then mkArray v else if isGVariant v then v else if builtins.isInt v then let validConstructors = builtins.filter ( { min, max, ... }: (min == null || min <= v) && (max == null || v <= max) ) intConstructors; in throw '' The GVariant type for number “${builtins.toString v}” is unclear. Please wrap the value with one of the following, depending on the value type in GSettings schema:
${lib.concatMapStringsSep "\n" ( { name, type, ... }: "- `lib.gvariant.${name}` for `${type}`" ) validConstructors} '' else if builtins.isAttrs v then throw "Cannot construct GVariant value from an attribute set. If you want to construct a dictionary, you will need to create an array containing items constructed with `lib.gvariant.mkDictionaryEntry`." else throw "The GVariant type of “${builtins.typeOf v}” can't be inferred.";
/** Returns the GVariant array from the given type of the elements and a Nix list.
# Inputs
`elems`
: 1\. Function argument
# Type
``` mkArray :: [Any] -> gvariant ```
# Examples :::{.example} ## `lib.gvariant.mkArray` usage example
```nix # Creating a string array lib.gvariant.mkArray [ "a" "b" "c" ] ```
::: */ mkArray = elems: let vs = map mkValue (lib.throwIf (elems == [ ]) "Please create empty array with mkEmptyArray." elems); elemType = lib.throwIfNot (lib.all (t: (head vs).type == t) ( map (v: v.type) vs )) "Elements in a list should have same type." (head vs).type; in mkPrimitive (type.arrayOf elemType) vs // { __toString = self: "@${self.type} [${concatMapStringsSep "," toString self.value}]"; };
/** Returns the GVariant array from the given empty Nix list.
# Inputs
`elemType`
: 1\. Function argument
# Type
``` mkEmptyArray :: gvariant.type -> gvariant ```
# Examples :::{.example} ## `lib.gvariant.mkEmptyArray` usage example
```nix # Creating an empty string array lib.gvariant.mkEmptyArray (lib.gvariant.type.string) ```
::: */ mkEmptyArray = elemType: mkPrimitive (type.arrayOf elemType) [ ] // { __toString = self: "@${self.type} []"; };
/** Returns the GVariant variant from the given Nix value. Variants are containers of different GVariant type.
# Inputs
`elem`
: 1\. Function argument
# Type
``` mkVariant :: Any -> gvariant ```
# Examples :::{.example} ## `lib.gvariant.mkVariant` usage example
```nix lib.gvariant.mkArray [ (lib.gvariant.mkVariant "a string") (lib.gvariant.mkVariant (lib.gvariant.mkInt32 1)) ] ```
::: */ mkVariant = elem: let gvarElem = mkValue elem; in mkPrimitive type.variant gvarElem // { __toString = self: "<${toString self.value}>"; };
/** Returns the GVariant dictionary entry from the given key and value.
# Inputs
`name`
: The key of the entry
`value`
: The value of the entry
# Type
``` mkDictionaryEntry :: String -> Any -> gvariant ```
# Examples :::{.example} ## `lib.gvariant.mkDictionaryEntry` usage example
```nix # A dictionary describing an Epiphany’s search provider [ (lib.gvariant.mkDictionaryEntry "url" (lib.gvariant.mkVariant "https://duckduckgo.com/?q=%s&t=epiphany")) (lib.gvariant.mkDictionaryEntry "bang" (lib.gvariant.mkVariant "!d")) (lib.gvariant.mkDictionaryEntry "name" (lib.gvariant.mkVariant "DuckDuckGo")) ] ```
::: */ mkDictionaryEntry = name: value: let name' = mkValue name; value' = mkValue value; dictionaryType = type.dictionaryEntryOf name'.type value'.type; in mkPrimitive dictionaryType { inherit name value; } // { __toString = self: "@${self.type} {${name'},${value'}}"; };
/** Returns the GVariant maybe from the given element type.
# Inputs
`elemType`
: 1\. Function argument
`elem`
: 2\. Function argument
# Type
``` mkMaybe :: gvariant.type -> Any -> gvariant ``` */ mkMaybe = elemType: elem: mkPrimitive (type.maybeOf elemType) elem // { __toString = self: if self.value == null then "@${self.type} nothing" else "just ${toString self.value}"; };
/** Returns the GVariant nothing from the given element type.
# Inputs
`elemType`
: 1\. Function argument
# Type
``` mkNothing :: gvariant.type -> gvariant ``` */ mkNothing = elemType: mkMaybe elemType null;
/** Returns the GVariant just from the given Nix value.
# Inputs
`elem`
: 1\. Function argument
# Type
``` mkJust :: Any -> gvariant ``` */ mkJust = elem: let gvarElem = mkValue elem; in mkMaybe gvarElem.type gvarElem;
/** Returns the GVariant tuple from the given Nix list.
# Inputs
`elems`
: 1\. Function argument
# Type
``` mkTuple :: [Any] -> gvariant ``` */ mkTuple = elems: let gvarElems = map mkValue elems; tupleType = type.tupleOf (map (e: e.type) gvarElems); in mkPrimitive tupleType gvarElems // { __toString = self: "@${self.type} (${concatMapStringsSep "," toString self.value})"; };
/** Returns the GVariant boolean from the given Nix bool value.
# Inputs
`v`
: 1\. Function argument
# Type
``` mkBoolean :: Bool -> gvariant ``` */ mkBoolean = v: mkPrimitive type.boolean v // { __toString = self: if self.value then "true" else "false"; };
/** Returns the GVariant string from the given Nix string value.
# Inputs
`v`
: 1\. Function argument
# Type
``` mkString :: String -> gvariant ``` */ mkString = v: let sanitize = s: replaceString "\n" "\\n" (escape [ "'" "\\" ] s); in mkPrimitive type.string v // { __toString = self: "'${sanitize self.value}'"; };
/** Returns the GVariant object path from the given Nix string value.
# Inputs
`v`
: 1\. Function argument
# Type
``` mkObjectpath :: String -> gvariant ``` */ mkObjectpath = v: mkPrimitive type.string v // { __toString = self: "objectpath '${escape [ "'" ] self.value}'"; };
/** Returns the GVariant uchar from the given Nix int value.
# Type
``` mkUchar :: Int -> gvariant ``` */ mkUchar = mkPrimitive type.uchar;
/** Returns the GVariant int16 from the given Nix int value.
# Type
``` mkInt16 :: Int -> gvariant ``` */ mkInt16 = mkPrimitive type.int16;
/** Returns the GVariant uint16 from the given Nix int value.
# Type
``` mkUint16 :: Int -> gvariant ``` */ mkUint16 = mkPrimitive type.uint16;
/** Returns the GVariant int32 from the given Nix int value.
# Inputs
`v`
: 1\. Function argument
# Type
``` mkInt32 :: Int -> gvariant ``` */ mkInt32 = v: mkPrimitive type.int32 v // { __toString = self: toString self.value; };
/** Returns the GVariant uint32 from the given Nix int value.
# Type
``` mkUint32 :: Int -> gvariant ``` */ mkUint32 = mkPrimitive type.uint32;
/** Returns the GVariant int64 from the given Nix int value.
# Type
``` mkInt64 :: Int -> gvariant ``` */ mkInt64 = mkPrimitive type.int64;
/** Returns the GVariant uint64 from the given Nix int value.
# Type
``` mkUint64 :: Int -> gvariant ``` */ mkUint64 = mkPrimitive type.uint64;
/** Returns the GVariant double from the given Nix float value.
# Inputs
`v`
: 1\. Function argument
# Type
``` mkDouble :: Float -> gvariant ``` */ mkDouble = v: mkPrimitive type.double v // { __toString = self: toString self.value; };}