2022-09-20 00:51:38 +02:00
|
|
|
{
|
|
|
|
stdenvNoCC
|
|
|
|
, s6-rc
|
2023-03-05 00:08:25 +01:00
|
|
|
, s6
|
2022-09-22 01:10:55 +02:00
|
|
|
, lib
|
2022-09-22 12:10:41 +02:00
|
|
|
, callPackage
|
2022-10-02 17:35:55 +02:00
|
|
|
, writeScript
|
2023-02-25 23:53:06 +01:00
|
|
|
, serviceFns
|
2022-09-26 13:26:54 +02:00
|
|
|
}:
|
|
|
|
let
|
2022-09-20 00:51:38 +02:00
|
|
|
inherit (builtins) concatStringsSep;
|
2024-02-13 22:41:43 +01:00
|
|
|
prefix = "/run/services/outputs";
|
2023-09-13 23:49:00 +02:00
|
|
|
output = service: name: "${prefix}/${service.name}/${name}";
|
2022-10-02 17:35:55 +02:00
|
|
|
serviceScript = commands : ''
|
2023-03-10 19:40:45 +01:00
|
|
|
#!/bin/sh
|
2023-03-05 00:08:25 +01:00
|
|
|
exec 2>&1
|
2023-02-25 23:53:06 +01:00
|
|
|
. ${serviceFns}
|
2022-10-02 17:35:55 +02:00
|
|
|
${commands}
|
|
|
|
'';
|
2023-09-13 23:49:00 +02:00
|
|
|
cleanupScript = name : ''
|
2023-11-21 18:25:50 +01:00
|
|
|
if test -d ${prefix}/${name} ; then rm -rf ${prefix}/${name} ; fi
|
2023-09-13 23:49:00 +02:00
|
|
|
'';
|
2022-10-02 15:44:29 +02:00
|
|
|
service = {
|
2022-09-20 00:51:38 +02:00
|
|
|
name
|
2022-10-02 15:44:29 +02:00
|
|
|
, serviceType
|
|
|
|
, run ? null
|
|
|
|
, up ? null
|
|
|
|
, down ? null
|
2022-09-20 00:51:38 +02:00
|
|
|
, outputs ? []
|
2022-09-25 22:06:08 +02:00
|
|
|
, notification-fd ? null
|
2023-03-05 00:08:25 +01:00
|
|
|
, producer-for ? null
|
|
|
|
, consumer-for ? null
|
|
|
|
, pipeline-name ? null
|
2022-09-20 00:51:38 +02:00
|
|
|
, dependencies ? []
|
2022-10-02 15:44:29 +02:00
|
|
|
, contents ? []
|
2023-03-05 00:08:25 +01:00
|
|
|
, 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;
|
|
|
|
};
|
2022-10-02 15:44:29 +02:00
|
|
|
|
|
|
|
longrun = {
|
|
|
|
name
|
|
|
|
, run
|
|
|
|
, outputs ? []
|
|
|
|
, notification-fd ? null
|
|
|
|
, dependencies ? []
|
2023-03-05 00:08:25 +01:00
|
|
|
, ...
|
|
|
|
} @ 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";
|
2023-09-13 23:49:00 +02:00
|
|
|
run = serviceScript "${run}\n${cleanupScript name}";
|
2023-03-05 00:08:25 +01:00
|
|
|
producer-for = "${name}-log";
|
|
|
|
});
|
|
|
|
|
2022-09-20 00:51:38 +02:00
|
|
|
oneshot = {
|
|
|
|
name
|
|
|
|
, up
|
2023-09-13 23:49:00 +02:00
|
|
|
, down ? ""
|
2022-09-20 00:51:38 +02:00
|
|
|
, outputs ? []
|
|
|
|
, dependencies ? []
|
|
|
|
, ...
|
2022-10-02 15:44:29 +02:00
|
|
|
} @ args : service (args // {
|
2022-09-26 12:46:09 +02:00
|
|
|
serviceType = "oneshot";
|
2022-10-02 17:35:55 +02:00
|
|
|
up = writeScript "${name}-up" (serviceScript up);
|
2023-09-13 23:49:00 +02:00
|
|
|
down = writeScript
|
|
|
|
"${name}-down"
|
|
|
|
"${serviceScript down}\n${cleanupScript name}";
|
2022-10-02 15:44:29 +02:00
|
|
|
});
|
|
|
|
bundle = {
|
2022-09-20 00:51:38 +02:00
|
|
|
name
|
|
|
|
, contents ? []
|
|
|
|
, dependencies ? []
|
|
|
|
, ...
|
2022-10-02 15:44:29 +02:00
|
|
|
} @ args: service (args // {
|
2022-09-26 12:46:09 +02:00
|
|
|
serviceType = "bundle";
|
2022-10-02 15:44:29 +02:00
|
|
|
inherit contents dependencies;
|
|
|
|
});
|
|
|
|
target = bundle;
|
2022-09-20 00:51:38 +02:00
|
|
|
in {
|
2022-09-26 13:26:54 +02:00
|
|
|
inherit target bundle oneshot longrun output;
|
2022-09-20 00:51:38 +02:00
|
|
|
}
|