fork/home-manager
git clone https://git.julia.blue/fork/home-manager
fce9dbfe
master
1{ lib } : 2 3{ 4toKDL = { } : 5let 6inherit ( lib ) concatStringsSep splitString mapAttrsToList any ; 7inherit ( builtins ) typeOf replaceStrings elem ; 8 9# ListOf String -> String 10indentStrings = let 11# Although the input of this function is a list of strings, 12# the strings themselves *will* contain newlines, so you need 13# to normalize the list by joining and resplitting them. 14unlines = lib . splitString "\n" ; 15lines = lib . concatStringsSep "\n" ; 16indentAll = lines :concatStringsSep "\n" ( map ( x :" " + x ) lines ); 17in stringsWithNewlines :indentAll ( unlines ( lines stringsWithNewlines )); 18 19# String -> String 20sanitizeString = replaceStrings [ "\n" ''"'' ] [ "\\n" ''\"'' ]; 21 22# OneOf [Int Float String Bool Null] -> String 23literalValueToString = element : 24lib . throwIfNot 25( elem ( typeOf element ) [ "int" "float" "string" "bool" "null" ]) 26"Cannot convert value of type ${ typeOf element } to KDL literal." 27( if typeOf element == "null" then 28"null" 29else if element == false then 30"false" 31else if element == true then 32"true" 33else if typeOf element == "string" then 34''" ${ sanitizeString element } "'' 35else 36toString element ); 37 38# Attrset Conversion 39# String -> AttrsOf Anything -> String 40convertAttrsToKDL = name :attrs : 41let 42optArgsString = lib . optionalString ( attrs ?"_args" ) 43( lib . pipe attrs . _args [ 44( map literalValueToString ) 45( lib . concatStringsSep " " ) 46( s :s + " " ) 47]); 48 49optPropsString = lib . optionalString ( attrs ?"_props" ) 50( lib . pipe attrs . _props [ 51( lib . mapAttrsToList 52( name :value :" ${ name } = ${ literalValueToString value } " )) 53( lib . concatStringsSep " " ) 54( s :s + " " ) 55]); 56 57children = 58lib . filterAttrs ( name :_ :! ( elem name [ "_args" "_props" ])) attrs ; 59in '' 60${ name } ${ optArgsString }${ optPropsString } { 61${ indentStrings ( mapAttrsToList convertAttributeToKDL children )} 62}'' ; 63 64# List Conversion 65# String -> ListOf (OneOf [Int Float String Bool Null]) -> String 66convertListOfFlatAttrsToKDL = name :list : 67let flatElements = map literalValueToString list ; 68in " ${ name } ${ concatStringsSep " " flatElements } " ; 69 70# String -> ListOf Anything -> String 71convertListOfNonFlatAttrsToKDL = name :list :'' 72${ name } { 73${ indentStrings ( map ( x : convertAttributeToKDL "-" x ) list )} 74}'' ; 75 76# String -> ListOf Anything -> String 77convertListToKDL = name :list : 78let elementsAreFlat = ! any ( el :elem ( typeOf el ) [ "list" "set" ]) list ; 79in if elementsAreFlat then 80convertListOfFlatAttrsToKDL name list 81else 82convertListOfNonFlatAttrsToKDL name list ; 83 84# Combined Conversion 85# String -> Anything -> String 86convertAttributeToKDL = name :value : 87let vType = typeOf value ; 88in if elem vType [ "int" "float" "bool" "null" "string" ] then 89" ${ name } ${ literalValueToString value } " 90else if vType == "set" then 91convertAttrsToKDL name value 92else if vType == "list" then 93convertListToKDL name value 94else 95throw '' 96Cannot convert type `( ${ typeOf value } )` to KDL: 97${ name } = ${ toString value } 98'' ; 99in attrs :'' 100${ concatStringsSep "\n" ( mapAttrsToList convertAttributeToKDL attrs )} 101'' ; 102}