55 lines
1.2 KiB
Nix
55 lines
1.2 KiB
Nix
let
|
|
inherit (builtins) length elemAt isList isString hasAttr getAttr;
|
|
|
|
###########################################################################
|
|
# Helpers
|
|
|
|
# Source: https://github.com/NixOS/nixpkgs/blob/d61bc96d16ca288c69b798b8e31eca64050098e3/lib/lists.nix
|
|
foldr = op: nul: list:
|
|
let
|
|
len = length list;
|
|
fold' = n:
|
|
if n == len
|
|
then nul
|
|
else op (elemAt list n) (fold' (n + 1));
|
|
in
|
|
fold' 0;
|
|
|
|
concatMap = op: list:
|
|
let
|
|
concat = (a: b: op a + b);
|
|
in
|
|
foldr concat "" list;
|
|
|
|
############################################################################
|
|
# Configs
|
|
|
|
optional = cond: val:
|
|
let
|
|
def =
|
|
if isList val then [ ]
|
|
else if isString val then ""
|
|
else null;
|
|
in
|
|
if cond then val else def;
|
|
|
|
getAttrOpt = a: s:
|
|
if hasAttr a s then getAttr a s else null;
|
|
|
|
#############################################################################
|
|
# Lua
|
|
|
|
mkLuaHeredoc = content: ''
|
|
lua << EOF
|
|
${content}
|
|
EOF
|
|
'';
|
|
|
|
mkLuaRc = contents: concatMap mkLuaHeredoc contents;
|
|
|
|
in
|
|
{
|
|
inherit foldr concatMap;
|
|
inherit optional getAttrOpt;
|
|
inherit mkLuaHeredoc mkLuaRc;
|
|
}
|