extract "service" function to reduce duplicated code

This commit is contained in:
Daniel Barlow 2022-10-02 14:44:29 +01:00
parent 7f280b5d6a
commit f030efbd49

View file

@ -9,22 +9,39 @@
let
inherit (builtins) concatStringsSep;
output = service: name: "/run/service-state/${service.name}/${name}";
service = {
name
, serviceType
, run ? null
, up ? null
, down ? null
, outputs ? []
, notification-fd ? null
, dependencies ? []
, contents ? []
} @ args: stdenvNoCC.mkDerivation {
# stdenvNoCC is to avoid generating derivations with names
# like foo.service-mips-linux-musl
inherit name serviceType;
inherit run up down;
buildInputs = dependencies ++ contents;
dependencies = builtins.map (d: d.name) dependencies;
contents = builtins.map (d: d.name) contents;
shell = "${busybox}/bin/sh";
notificationFd = notification-fd;
builder = ./builder.sh;
};
longrun = {
name
, run
, outputs ? []
, notification-fd ? null
, dependencies ? []
} @ args: stdenvNoCC.mkDerivation {
inherit name;
} @ args: service (args //{
serviceType = "longrun";
buildInputs = dependencies;
dependencies = builtins.map (d: d.name) dependencies;
shell = "${busybox}/bin/sh";
inherit run;
notificationFd = notification-fd;
builder = ./builder.sh;
};
});
oneshot = {
name
, up
@ -32,37 +49,21 @@ let
, outputs ? []
, dependencies ? []
, ...
} @ args: stdenvNoCC.mkDerivation {
# stdenvNoCC is to avoid generating derivations with names
# like foo.service-mips-linux-musl
inherit name;
} @ args : service (args // {
serviceType = "oneshot";
# does this suffice to make sure dependencies are included
# even though the built output has no references to their
# store directories?
buildInputs = dependencies;
shell = "${busybox}/bin/sh";
# up and down for oneshots are pathnames not scripts
up = writeAshScript "${name}-up" {} up;
down = writeAshScript "${name}-down" {} down;
dependencies = builtins.map (d: d.name) dependencies;
builder = ./builder.sh;
};
target = {
});
bundle = {
name
, contents ? []
, dependencies ? []
, ...
}: stdenvNoCC.mkDerivation {
inherit name;
} @ args: service (args // {
serviceType = "bundle";
contents = builtins.map (d: d.name) contents;
buildInputs = dependencies ++ contents;
dependencies = builtins.map (d: d.name) dependencies;
shell = "${busybox}/bin/sh";
builder = ./builder.sh;
};
bundle = { name, ... } @args : target (args // { inherit name;});
inherit contents dependencies;
});
target = bundle;
in {
inherit target bundle oneshot longrun output;
}