liminix/pkgs/liminix-tools/services/default.nix

107 lines
2.5 KiB
Nix
Raw Normal View History

{
stdenvNoCC
, s6-rc
2023-03-05 00:08:25 +01:00
, s6
, lib
2022-09-22 12:10:41 +02:00
, callPackage
, writeScript
, serviceFns
}:
let
inherit (builtins) concatStringsSep;
prefix = "/run/services/outputs";
output = service: name: "${prefix}/${service.name}/${name}";
serviceScript = commands : ''
#!/bin/sh
2023-03-05 00:08:25 +01:00
exec 2>&1
. ${serviceFns}
${commands}
'';
cleanupScript = name : ''
#!/bin/sh
if test -d ${prefix}/${name} ; then rm -rf ${prefix}/${name} ; fi
'';
service = {
name
, serviceType
, run ? null
, up ? null
, down ? null
, finish ? null
, outputs ? []
, notification-fd ? null
2023-03-05 00:08:25 +01:00
, producer-for ? null
, consumer-for ? null
, pipeline-name ? null
, timeout-up ? 30000 # milliseconds
, timeout-down ? 0
, dependencies ? []
, contents ? []
2023-03-05 00:08:25 +01:00
, buildInputs ? []
, isTrigger ? false
2023-03-05 00:08:25 +01:00
} @ args:
stdenvNoCC.mkDerivation {
# we use stdenvNoCC to avoid generating derivations with names
# like foo.service-mips-linux-musl
inherit name serviceType up down run finish notification-fd
producer-for consumer-for pipeline-name timeout-up timeout-down;
restart-on-upgrade = isTrigger;
2023-03-05 00:08:25 +01:00
buildInputs = buildInputs ++ dependencies ++ contents;
dependencies = builtins.map (d: d.name) dependencies;
contents = builtins.map (d: d.name) contents;
builder = ./builder.sh;
};
longrun = {
name
, run
, outputs ? []
, notification-fd ? null
, dependencies ? []
, buildInputs ? []
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 = buildInputs ++ [ logger ];
2023-03-05 00:08:25 +01:00
serviceType = "longrun";
run = serviceScript run;
finish = cleanupScript name;
2023-03-05 00:08:25 +01:00
producer-for = "${name}-log";
});
oneshot = {
name
, up
, down ? ""
, outputs ? []
, dependencies ? []
, ...
} @ args : service (args // {
2022-09-26 12:46:09 +02:00
serviceType = "oneshot";
up = writeScript "${name}-up" (serviceScript up);
down = writeScript
"${name}-down"
"${serviceScript down}\n${cleanupScript name}";
});
bundle = {
name
, contents ? []
, dependencies ? []
, ...
} @ args: service (args // {
2022-09-26 12:46:09 +02:00
serviceType = "bundle";
inherit contents dependencies;
});
target = bundle;
in {
inherit target bundle oneshot longrun output;
}