2022-05-09 13:20:43 +02:00
dirname : inputs @ { self , nixpkgs , . . . }: let
inherit ( nixpkgs ) lib ;
inherit ( import " ${ dirname } / v a r s . n i x " dirname inputs ) startsWith ;
in rec {
## Logic Flow
notNull = value : value != null ;
ifNull = value : default : ( if value == null then default else value ) ;
withDefault = default : value : ( if value == null then default else value ) ;
passNull = mayNull : expression : ( if mayNull == null then null else expression ) ;
## Misc
# Creates a package for `config.systemd.packages` that adds an `override.conf` to the specified `unit` (which is the only way to modify a single service template instance).
mkSystemdOverride = pkgs : unit : text : ( pkgs . runCommandNoCC unit { preferLocalBuild = true ; allowSubstitutes = false ; } ''
mkdir - p $ out / $ { lib . escapeShellArg " / e t c / s y s t e m d / s y s t e m / ${ unit } . d / " }
< < < $ { lib . escapeShellArg text } cat > $ out / $ { lib . escapeShellArg " / e t c / s y s t e m d / s y s t e m / ${ unit } . d / o v e r r i d e . c o n f " }
'' ) ;
# Given »config.ids« (or equivalent) and a user name, returns the users numeric »uid:gid« pair as string.
getOwnership = { gids , uids , . . . }: user : " ${ toString uids . ${ user } } : ${ toString gids . ${ user } } " ;
# Given »from« and »to« as »config.my.network.spec.hosts.*«,
# picks the first of »to«'s IPs whose required subnet is either empty/any, or a prefix to any of the subnets in »from«:
# ip = preferredRoute self.subNets other.routes;
# ip6 = preferredRoute self.subNets (builtins.filter (r: r.is6) other.routes);
# to.find(({ ip, prefix }) => from.any(_=>_.startsWith(prefix))).ip
preferredRoute = from : to : ( lib . findFirst ( { prefix , ip , . . . }: prefix == " " || ( builtins . any ( fromSub : startsWith prefix fromSub ) from ) ) { ip = " " ; } to ) . ip ;
# Given a message and any value, traces both the message and the value, and returns the value.
2022-05-18 16:06:27 +02:00
trace = message : value : ( builtins . trace ( message + " : " + ( lib . generators . toPretty { } value ) ) value ) ;
2022-05-09 13:20:43 +02:00
rpoolOf = hostName : " r p o o l - ${ builtins . substring 0 8 ( builtins . hashString " s h a 2 5 6 " hostName ) } " ;
}