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 let
inherit (builtins) concatStringsSep; inherit (builtins) concatStringsSep;
output = service: name: "/run/service-state/${service.name}/${name}"; 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 = { longrun = {
name name
, run , run
, outputs ? [] , outputs ? []
, notification-fd ? null , notification-fd ? null
, dependencies ? [] , dependencies ? []
} @ args: stdenvNoCC.mkDerivation { } @ args: service (args //{
inherit name;
serviceType = "longrun"; serviceType = "longrun";
buildInputs = dependencies; });
dependencies = builtins.map (d: d.name) dependencies;
shell = "${busybox}/bin/sh";
inherit run;
notificationFd = notification-fd;
builder = ./builder.sh;
};
oneshot = { oneshot = {
name name
, up , up
@ -32,37 +49,21 @@ let
, outputs ? [] , outputs ? []
, dependencies ? [] , dependencies ? []
, ... , ...
} @ args: stdenvNoCC.mkDerivation { } @ args : service (args // {
# stdenvNoCC is to avoid generating derivations with names
# like foo.service-mips-linux-musl
inherit name;
serviceType = "oneshot"; 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; up = writeAshScript "${name}-up" {} up;
down = writeAshScript "${name}-down" {} down; down = writeAshScript "${name}-down" {} down;
dependencies = builtins.map (d: d.name) dependencies; });
builder = ./builder.sh; bundle = {
};
target = {
name name
, contents ? [] , contents ? []
, dependencies ? [] , dependencies ? []
, ... , ...
}: stdenvNoCC.mkDerivation { } @ args: service (args // {
inherit name;
serviceType = "bundle"; serviceType = "bundle";
contents = builtins.map (d: d.name) contents; inherit contents dependencies;
buildInputs = dependencies ++ contents; });
dependencies = builtins.map (d: d.name) dependencies; target = bundle;
shell = "${busybox}/bin/sh";
builder = ./builder.sh;
};
bundle = { name, ... } @args : target (args // { inherit name;});
in { in {
inherit target bundle oneshot longrun output; inherit target bundle oneshot longrun output;
} }