From f030efbd494413bfcdf48dd0d90839459ac38c7e Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sun, 2 Oct 2022 14:44:29 +0100 Subject: [PATCH] extract "service" function to reduce duplicated code --- pkgs/liminix-tools/services/default.nix | 65 +++++++++++++------------ 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/pkgs/liminix-tools/services/default.nix b/pkgs/liminix-tools/services/default.nix index 265d532..a3a5147 100644 --- a/pkgs/liminix-tools/services/default.nix +++ b/pkgs/liminix-tools/services/default.nix @@ -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; }