From e9f04931fa098a0feafd1102e7e26e32cd3573ed Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sun, 2 Oct 2022 16:35:55 +0100 Subject: [PATCH] improve syntax for accessing service outputs --- pkgs/liminix-tools/services/builder.sh | 7 ++++--- pkgs/liminix-tools/services/default.nix | 19 +++++++++++-------- tests/pppoe/configuration.nix | 4 ++-- tests/smoke/configuration.nix | 10 ++++------ tests/wlan/configuration.nix | 2 +- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/pkgs/liminix-tools/services/builder.sh b/pkgs/liminix-tools/services/builder.sh index 74172b4..6c7eef1 100644 --- a/pkgs/liminix-tools/services/builder.sh +++ b/pkgs/liminix-tools/services/builder.sh @@ -10,10 +10,11 @@ test -n "$contents" && for d in $contents; do mkdir -p $out/${name}/contents.d touch $out/${name}/contents.d/$d done -test -n "$run" && (echo -e "#!$shell\n$run" > $out/${name}/run) +test -n "$run" && (echo -e "$run" > $out/${name}/run) test -n "${notificationFd}" && (echo ${notificationFd} > $out/${name}/notification-fd) -test -n "$up" && (echo -e "#!$shell\n$up" > $out/${name}/up) -test -n "$down" && (echo -e "#!$shell\n$down" > $out/${name}/down) +test -n "$up" && (echo -e "$up" > $out/${name}/up) +test -n "$down" && (echo -e "$down" > $out/${name}/down) +( cd $out && ln -s /run/service-state/${name} ./.outputs ) for i in $out/${name}/{down,up,run} ; do test -f $i && chmod +x $i; done true # (echo $out/${name} && cd $out/${name} && find . -ls) diff --git a/pkgs/liminix-tools/services/default.nix b/pkgs/liminix-tools/services/default.nix index a3a5147..bbff407 100644 --- a/pkgs/liminix-tools/services/default.nix +++ b/pkgs/liminix-tools/services/default.nix @@ -4,12 +4,16 @@ , lib , busybox , callPackage -, writeAshScript +, writeScript }: let inherit (builtins) concatStringsSep; output = service: name: "/run/service-state/${service.name}/${name}"; - + serviceScript = commands : '' + #!${busybox}/bin/sh + output() { cat $1/.outputs/$2; } + ${commands} + ''; service = { name , serviceType @@ -21,14 +25,12 @@ let , dependencies ? [] , contents ? [] } @ args: stdenvNoCC.mkDerivation { - # stdenvNoCC is to avoid generating derivations with names + # we use stdenvNoCC to avoid generating derivations with names # like foo.service-mips-linux-musl - inherit name serviceType; - inherit run up down; + inherit name serviceType up down run; 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; }; @@ -41,6 +43,7 @@ let , dependencies ? [] } @ args: service (args //{ serviceType = "longrun"; + run = serviceScript run; }); oneshot = { name @@ -51,8 +54,8 @@ let , ... } @ args : service (args // { serviceType = "oneshot"; - up = writeAshScript "${name}-up" {} up; - down = writeAshScript "${name}-down" {} down; + up = writeScript "${name}-up" (serviceScript up); + down= writeScript "${name}-down" (serviceScript down); }); bundle = { name diff --git a/tests/pppoe/configuration.nix b/tests/pppoe/configuration.nix index 811aa68..f602c01 100644 --- a/tests/pppoe/configuration.nix +++ b/tests/pppoe/configuration.nix @@ -41,7 +41,7 @@ in rec { services.defaultroute4 = route { name = "defautlrote"; - via = "$(cat ${output services.pppoe "address"})"; + via = "$(output ${services.pppoe} address)"; target = "default"; dependencies = [ services.pppoe ]; }; @@ -49,7 +49,7 @@ in rec { services.packet_forwarding = let iface = services.pppoe; - filename = "/proc/sys/net/ipv4/conf/$(cat ${output iface "ifname"})/forwarding"; + filename = "/proc/sys/net/ipv4/conf/$(output ${iface} ifname)/forwarding"; in oneshot { name = "let-the-ip-flow"; up = "echo 1 > ${filename}"; diff --git a/tests/smoke/configuration.nix b/tests/smoke/configuration.nix index c59410a..9eab4a2 100644 --- a/tests/smoke/configuration.nix +++ b/tests/smoke/configuration.nix @@ -1,7 +1,7 @@ { config, pkgs, ... } : let inherit (pkgs.liminix.networking) interface address udhcpc odhcpc route; - inherit (pkgs.liminix.services) oneshot longrun bundle target output; + inherit (pkgs.liminix.services) oneshot longrun bundle target; in rec { services.loopback = let iface = interface { type = "loopback"; device = "lo";}; @@ -21,11 +21,9 @@ in rec { in odhcpc iface { uid = "e7"; }; services.ntp = longrun { - # the simplest approach at the consumer end is to require the - # producer to create a file per output variable. name = "ntp"; run = let inherit (services) dhcpv4 dhcpv6; - in "${pkgs.ntp}/bin/ntpd $(cat ${output dhcpv4 "ntp_servers"}) $(cat ${output dhcpv6 "NTP_IP"})"; + in "${pkgs.ntp}/bin/ntpd $(output ${dhcpv4} ntp_servers) $(output ${dhcpv6} NTP_IP})"; # I don't think it's possible to standardise the file names # generally, as different services have different outputs, but it @@ -42,7 +40,7 @@ in rec { services.defaultroute4 = route { name = "defautlrote"; - via = "$(cat ${output services.dhcpv4 "address"})"; + via = "$(output ${services.dhcpv4} address)"; target = "default"; dependencies = [ services.dhcpv4 ]; }; @@ -50,7 +48,7 @@ in rec { services.packet_forwarding = let iface = services.dhcpv4; - filename = "/proc/sys/net/ipv4/conf/$(cat ${output iface "ifname"})/forwarding"; + filename = "/proc/sys/net/ipv4/conf/$(output ${iface} ifname)/forwarding"; in oneshot { name = "let-the-ip-flow"; up = "echo 1 > ${filename}"; diff --git a/tests/wlan/configuration.nix b/tests/wlan/configuration.nix index 809725d..7f5f395 100644 --- a/tests/wlan/configuration.nix +++ b/tests/wlan/configuration.nix @@ -1,7 +1,7 @@ { config, pkgs, lib, ... } : let inherit (pkgs.liminix.networking) interface address hostapd route dnsmasq; - inherit (pkgs.liminix.services) oneshot longrun bundle target output; + inherit (pkgs.liminix.services) oneshot longrun bundle target; in rec { services.loopback = let iface = interface { type = "loopback"; device = "lo";};