# Given an attribute set of attribute sets (»{ ${l1name}.${l2name} = value; }«), flips the positions of the first and second name level, producing »{ ${l3name}.${l1name} = value; }«. The set of »l2name«s does not need to be the same for each »l1name«.
# Like »builtins.catAttrs«, just for attribute sets instead of lists: Given an attribute set of attribute sets (»{ ${l1name}.${l2name} = value; }«) and the »name« of a second-level attribute, this returns the attribute set mapping directly from the first level's names to the second-level's values (»{ ${l1name} = value; }«), omitting any first-level attributes that lack the requested second-level attribute.
# Given a regular expression with capture groups and a list of strings, returns the flattened list of all the matched capture groups of the strings matched in their entirety by the regular expression.
# If »exp« (which mustn't match across »\n«) matches (a part of) exactly one line in »text«, return that »line« including tailing »\n«, plus the text part »before« and »after«, the text »without« the line, and any »captures« made by »exp«. If »text« does not end in a »\n«, then one will be added (since this function operates on lines).
# The »*Anchored« version allows the expression to require to match from the »start« and/or to the »end« of its line, by passing the respective bool(s) as »true«.
extractLineAnchored=exp:start:end:text:let
exp'="(${ifstartthen"^|\n"else""})(${ifstartthen""else"[^\n]*"}(${exp})${ifendthen""else"[^\n]*"}\n)";# First capture group is the optional start anchor, the second one the line itself.
text'=(builtins.unsafeDiscardStringContext(if(lastChartext)=="\n"thentextelsetext+"\n"));# Ensure tailing newline and drop context (since it needs to be added again anyway).
split=builtins.splitexp'text';
get=builtins.elemAtsplit;matches=get1;
ctxify=str:lib.addContextFromtextstr;
inifbuiltins.lengthsplit!=3thennullelserec{# length < 3 => no match ; length < 3 => multiple matches
## Reproducibly generates a GUID by sha256-hashing a prefixed name. The result looks like a RFC 4122 GUID "generated by [SHA1] hashing a namespace identifier and name".
## Generate an entry for »systemd.tmpfiles.rules« with named attributes and proper escaping.
# This behaves according to the man page, but contrary to what the man page says/implies:
# * a single »%« is actually interpreted verbatim, as long as it is not followed by a letter (I guess a "specifier" is a »%« followed by a letter or another »%«),
# * »\xDD« escapes don't work in the »type« field (e.g. error: "Unknown modifiers in command 'x66+'" for "\x66+", which should be interpreted as "f+"),
# * none of the »\« I tried (»\n«, »\t«, »\xDD«) worked in the »path« (it simply removed the »\«); Only »\\« correctly results in »\«.
# => I assume the "Fields may contain C-style escapes" isn't technically incorrect, but the implied "... and they are interpreted as such" actually only applies to the »argument« field. The man page also doesn't actually say what consequence quoting has (I assume it prevents splitting at " ", but anything else?).
mkTmpfile={
type?"d",# One of [ "f" "f+" "w" "w+" "d" "D" "e" "v" "q" "Q" "p" "p+" "L" "L+" "c" "c+" "b" "b+" "C" "x" "X" "r" "R" "z" "Z" "t" "T" "h" "H" "a" "a+" "A" "A+" ].
path,pathSubstitute?false,# String starting with "/" or (if »pathSubstitute == true«) also "%"
mode?"-",# 4 digit octal works. Can be prefixed with "~" (mask with existing mode) or ":" (keep existing mode).
user?"-",group?user,
age?"-",
argument?"",argumentSubstitute?false,# Depends on type.
}:let
# »systemd/src/basic/string-util.h« defines »WHITESPACE = " \t\n\r"«. »toJSON« escapes all of these except for " ", but that only matters if it is the first char of the (unquoted) argument.