From a8cb87285923ef7b08d667623f62afc9642452b8 Mon Sep 17 00:00:00 2001
From: Daniel Barlow <dan@telent.net>
Date: Sat, 4 Mar 2023 23:08:25 +0000
Subject: [PATCH] add service name to log lines

---
 overlay.nix                             | 10 ++++++
 pkgs/liminix-tools/services/builder.sh  |  9 ++---
 pkgs/liminix-tools/services/default.nix | 47 ++++++++++++++++++-------
 rotuer.nix                              |  2 +-
 4 files changed, 50 insertions(+), 18 deletions(-)

diff --git a/overlay.nix b/overlay.nix
index 4f40b0f..16215b5 100644
--- a/overlay.nix
+++ b/overlay.nix
@@ -4,6 +4,16 @@ in
 extraPkgs // {
   strace = prev.strace.override { libunwind = null; };
 
+  s6 = prev.s6.overrideAttrs(o: {
+    patches =
+      (if o ? patches then o.patches else []) ++ [
+        (final.fetchpatch {
+          # add "p" directive in s6-log
+          url = "https://github.com/skarnet/s6/commit/ddc76841398dfd5e18b22943727ad74b880236d3.patch";
+          hash = "sha256-fBtUinBdp5GqoxgF6fcR44Tu8hakxs/rOShhuZOgokc=";
+        })];
+  });
+
   dnsmasq =
     let d =  prev.dnsmasq.overrideAttrs(o: {
           preBuild =  ''
diff --git a/pkgs/liminix-tools/services/builder.sh b/pkgs/liminix-tools/services/builder.sh
index f6b84be..5709d4a 100644
--- a/pkgs/liminix-tools/services/builder.sh
+++ b/pkgs/liminix-tools/services/builder.sh
@@ -10,10 +10,11 @@ test -n "$contents" && for d in $contents; do
     mkdir -p $out/${name}/contents.d
     touch $out/${name}/contents.d/$d
 done
-test -n "$run" && (echo -e "$run" > $out/${name}/run)
-test -n "${notification-fd}" && (echo ${notification-fd} > $out/${name}/notification-fd)
-test -n "$up" && (echo -e "$up" > $out/${name}/up)
-test -n "$down" && (echo -e "$down" > $out/${name}/down)
+
+for i in run notification-fd up down consumer-for producer-for pipeline-name ; do
+    test -n "$(printenv $i)" && (echo "$(printenv $i)" > $out/${name}/$i)
+done
+
 ( cd $out && ln -s /run/service-state/${name} ./.outputs )
 for i in $out/${name}/{down,up,run} ; do test -f $i && chmod +x $i; done
 true
diff --git a/pkgs/liminix-tools/services/default.nix b/pkgs/liminix-tools/services/default.nix
index 4e20a2f..b2cb939 100644
--- a/pkgs/liminix-tools/services/default.nix
+++ b/pkgs/liminix-tools/services/default.nix
@@ -1,6 +1,7 @@
 {
   stdenvNoCC
 , s6-rc
+, s6
 , lib
 , busybox
 , callPackage
@@ -12,6 +13,7 @@ let
   output = service: name: "/run/service-state/${service.name}/${name}";
   serviceScript = commands : ''
     #!${busybox}/bin/sh
+    exec 2>&1
     . ${serviceFns}
     ${commands}
   '';
@@ -23,17 +25,23 @@ let
     , down ? null
     , outputs ? []
     , notification-fd ? null
+    , producer-for ? null
+    , consumer-for ? null
+    , pipeline-name ? null
     , dependencies ? []
     , contents ? []
-  } @ args: stdenvNoCC.mkDerivation {
-    # we use stdenvNoCC to avoid generating derivations with names
-    # like foo.service-mips-linux-musl
-    inherit name serviceType up down run notification-fd;
-    buildInputs = dependencies ++ contents;
-    dependencies = builtins.map (d: d.name) dependencies;
-    contents = builtins.map (d: d.name) contents;
-    builder = ./builder.sh;
-  };
+    , buildInputs ? []
+  } @ args:
+    stdenvNoCC.mkDerivation {
+      # we use stdenvNoCC to avoid generating derivations with names
+      # like foo.service-mips-linux-musl
+      inherit name serviceType up down run notification-fd
+        producer-for consumer-for pipeline-name;
+      buildInputs = buildInputs ++ dependencies ++ contents;
+      dependencies = builtins.map (d: d.name) dependencies;
+      contents = builtins.map (d: d.name) contents;
+      builder = ./builder.sh;
+    };
 
   longrun = {
     name
@@ -41,10 +49,23 @@ let
     , outputs ? []
     , notification-fd ? null
     , dependencies ? []
-  } @ args: service (args //{
-    serviceType = "longrun";
-    run = serviceScript run;
-  });
+    , ...
+  } @ args:
+    let logger = service {
+          serviceType = "longrun";
+          name = "${name}-log";
+          run = serviceScript "${s6}/bin/s6-log -d 10 -- p${name} 1";
+          notification-fd = 10;
+          consumer-for = name;
+          pipeline-name = "${name}-pipeline";
+        };
+    in service (args // {
+      buildInputs = [ logger ];
+      serviceType = "longrun";
+      run = serviceScript run;
+      producer-for = "${name}-log";
+    });
+
   oneshot = {
     name
     , up
diff --git a/rotuer.nix b/rotuer.nix
index 25c3972..660d5e6 100644
--- a/rotuer.nix
+++ b/rotuer.nix
@@ -43,7 +43,7 @@ in rec {
   imports = [
     ./modules/wlan.nix
     ./modules/tftpboot.nix
-    ./modules/flashable.nix
+#    ./modules/flashable.nix
   ];
 
   kernel = {