1
0
forked from dan/liminix
liminix/pkgs/liminix-tools/services/default.nix
Daniel Barlow e6e4665a18 flip dependencies for triggered/controlled services
Instead of treating the trigger as the "main" service and the
triggered service as subsidary, now we treat the triggered
service as the service and the trigger as "subsidary". This
needs some special handling when we work out which services
go in the default bundle, but it works better for declaring
dependencies on triggered services because it means the
dependency runs after the triggered service comes up, not
just when the watcher-for-events starts
2024-06-09 22:37:45 +01:00

110 lines
2.7 KiB
Nix

{
stdenvNoCC
, s6-rc
, s6
, lib
, callPackage
, writeScript
, serviceFns
}:
let
inherit (builtins) concatStringsSep any map;
prefix = "/run/services/outputs";
output = service: name: "${prefix}/${service.name}/${name}";
serviceScript = commands : ''
#!/bin/sh
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
, producer-for ? null
, consumer-for ? null
, pipeline-name ? null
, timeout-up ? 30000 # milliseconds
, timeout-down ? 0
, dependencies ? []
, contents ? []
, buildInputs ? []
, isTrigger ? false
, controller ? null
} @ 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;
buildInputs = buildInputs ++ dependencies ++ contents ++ lib.optional (controller != null) controller;
dependencies = map (d: d.name) dependencies;
contents = map (d: d.name) contents;
inherit controller;
controllerName = if controller ? name then controller.name else null;
builder = ./builder.sh;
};
longrun = {
name
, run
, outputs ? []
, notification-fd ? null
, dependencies ? []
, buildInputs ? []
, ...
} @ 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 ];
serviceType = "longrun";
run = serviceScript run;
finish = cleanupScript name;
producer-for = "${name}-log";
});
oneshot = {
name
, up
, down ? ""
, outputs ? []
, dependencies ? []
, ...
} @ args : service (args // {
serviceType = "oneshot";
up = writeScript "${name}-up" (serviceScript up);
down = writeScript
"${name}-down"
"${serviceScript down}\n${cleanupScript name}";
});
bundle = {
name
, contents ? []
, dependencies ? []
, ...
} @ args: service (args // {
serviceType = "bundle";
inherit contents dependencies;
});
target = bundle;
in {
inherit target bundle oneshot longrun output;
}