modules/users/s6-rc: add per-service logging

This commit is contained in:
Colin 2024-03-21 13:29:58 +00:00
parent 218072b2fe
commit 5c9c7f8073

View File

@ -55,12 +55,38 @@ let
fsToDerivation = fs: fsItemToDerivation "/" { dir = fs; };
# infers the service type from the arguments and creates an attrset usable by `fsToDerivation`.
# also configures the service for logging, if applicable.
serviceToFs = { name, run, finish, depends }: let
name' = normalizeName name;
type = if run != null then "longrun" else "bundle";
in {
logger = serviceToFs' {
name = "logger:${name'}";
consumerFor = name';
run = ''exec s6-log -- T p'${name'}:' "${logBase}/${name'}"'';
type = "longrun";
};
in (serviceToFs' {
inherit type run finish;
name = name';
# TODO: a bundle can have dependencies too!
depends = lib.optionals (type == "longrun") depends;
contents = maybe (type == "bundle") depends;
producerFor = maybe (type == "longrun") "logger:${name'}";
}) // (lib.optionalAttrs (type == "longrun") logger);
serviceToFs'= {
name,
type,
contents ? null,
run ? null,
depends ? [],
finish ? null,
producerFor ? null,
consumerFor ? null,
}: {
"${name}".dir = {
"type".text = type;
# TODO: finish and run should `exec` into their cli
"finish".executable = maybe (finish != null) ''
#!/bin/sh
${finish}
@ -70,15 +96,16 @@ let
echo "starting: s6-${name}"
${run} 2>&1
'';
"contents".text = maybe (type == "bundle") (
lib.concatStringsSep "\n" (builtins.map normalizeName depends)
"contents".text = maybe (contents != null) (
lib.concatStringsSep "\n" (builtins.map normalizeName contents)
);
# TODO: a bundle can also have dependencies
"dependencies.d".dir = lib.optionalAttrs (type == "longrun") (
lib.genAttrs
(builtins.map normalizeName depends)
(dep: { text = ""; })
);
"dependencies.d".dir = lib.genAttrs
(builtins.map normalizeName depends)
(dep: { text = ""; })
;
"consumer-for".text = maybe (consumerFor != null) consumerFor;
"producer-for".text = maybe (producerFor != null) producerFor;
};
};