s6: add some minimal logging

the root s6 call seems to be doing some logging, notably feedbackd; still don't know where the other logs are going
This commit is contained in:
Colin 2024-03-19 06:50:03 +00:00
parent cbecdc4a95
commit 8bedc860ae

View File

@ -1,20 +1,18 @@
{ lib, pkgs, ... }:
let
logBase = "$HOME/.local/state/s6/logs";
normalizeName = name: lib.removeSuffix ".service" (lib.removeSuffix ".target" name);
# infers the service type from the arguments and dispatches appropriately
genService = { name, run, depends }: if run != null then
genService' (normalizeName name) "longrun" depends [
(pkgs.writeTextFile {
name = "s6-${name}-run";
destination = "/${normalizeName name}/run";
executable = true;
# TODO: consider using `makeWrapper`/`makeWrapperBin`?
text = ''
#!/bin/sh
exec ${run}
'';
})
genService = { name, run, depends }: let
name' = normalizeName name;
logDir = "${logBase}/${name'}";
# N.B. s6-log will create the logDir. at least, if the parent dir exists (haven't tested without that)
logger = genRun "${name'}/log" ''s6-log -- T "${logDir}"'';
in if run != null then
genService' name' "longrun" depends [
(genRun name' run)
logger
]
else
# TODO: a bundle can totally have dependencies. i can't just map them *all* to contents.
@ -33,10 +31,10 @@ let
# '')
# ]
# )
genService' (normalizeName name) "bundle" [] [
genService' name' "bundle" [] [
(pkgs.writeTextFile {
name = "s6-${name}-contents";
destination = "/${normalizeName name}/contents";
name = "s6-${name'}-contents";
destination = "/${name'}/contents";
text = lib.concatStringsSep "\n" (builtins.map normalizeName depends);
})
]
@ -58,6 +56,17 @@ let
depends;
};
genRun = serviceName: shellCommand: pkgs.writeTextFile {
name = "s6-${serviceName}-run";
destination = "/${serviceName}/run";
executable = true;
# TODO: consider using `makeWrapper`/`makeBinaryWrapper`?
text = ''
#!/bin/sh
exec ${shellCommand} 2>&1
'';
};
# create a directory, containing N subdirectories:
# - svc-a/
# - type
@ -139,37 +148,37 @@ in
function startS6() {
local S6_TARGET="''${1:-default}"
local COMPILED=$HOME/.config/s6/compiled
local LIVE=$HOME/.config/s6/live
local SCANDIR=$HOME/.config/s6/scandir
local COMPILED="$HOME/.config/s6/compiled"
local LIVE="$HOME/.config/s6/live"
local SCANDIR="$HOME/.config/s6/scandir"
rm -rf $SCANDIR
mkdir $SCANDIR
s6-svscan $SCANDIR &
rm -rf "$SCANDIR"
mkdir "$SCANDIR"
s6-svscan "$SCANDIR" &
local SVSCAN=$!
# the scandir is just links back into the compiled dir,
# so the compiled dir therefore needs to be writable:
rm -rf $COMPILED
cp --dereference -R $COMPILED-static $COMPILED
chmod -R 0700 $COMPILED
rm -rf "$COMPILED"
cp --dereference -R "$COMPILED-static" "$COMPILED"
chmod -R 0700 "$COMPILED"
s6-rc-init -c $COMPILED -l $LIVE -d $SCANDIR
# echo default: deps
# s6-rc-db -c $COMPILED contents default
# echo graphical-session: deps
# s6-rc-db -c $COMPILED contents graphical-session
s6-rc-init -c "$COMPILED" -l "$LIVE" -d "$SCANDIR"
if [ -n "$S6_TARGET" ]; then
s6-rc -l $LIVE start "$S6_TARGET"
s6-rc -l "$LIVE" start "$S6_TARGET"
fi
echo "s6 initialized: Ctrl+C to stop"
wait $SVSCAN
echo 's6 initialized: Ctrl+C to stop'
wait "$SVSCAN"
}
function startS6WithLogging() {
# TODO: might not want to create log dir here: move to nix fs/persistence.
mkdir -p "${logBase}"
startS6 2>&1 | tee /dev/stderr | s6-log -- T "${logBase}"
}
primarySessionCommands+=('startS6 &')
primarySessionCommands+=('startS6WithLogging &')
'';
}));
};