liminix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

subscriber.nix (1359B)


      1 {
      2   liminix,
      3   lib,
      4   lim,
      5   s6,
      6   s6-rc,
      7   watch-outputs,
      8   s6-rc-up-tree,
      9 }:
     10 {
     11   watch,
     12   service,
     13   action,
     14 }:
     15 let
     16   inherit (liminix.services) longrun;
     17   inherit (builtins) map;
     18   inherit (lib) optionals optional concatStringsSep;
     19   inherit (service) name;
     20 
     21   restart-flag =
     22     {
     23       restart = "-r";
     24       restart-all = "-R";
     25       "hup" = "-s 1";
     26       "int" = "-s 2";
     27       "quit" = "-s 3";
     28       "kill" = "-s 9";
     29       "term" = "-s 15";
     30       "winch" = "-s 20";
     31       "usr1" = "-s usr1";
     32       "usr2" = "-s usr2";
     33     }
     34     .${action};
     35 
     36   watcher =
     37     let
     38       name' = "restart-${name}";
     39       refs = concatStringsSep " " (map (s: "${s "service"}:${s "path"}") watch);
     40     in
     41     longrun {
     42       name = name';
     43       run = ''
     44         dir=/run/service/${name}
     45         echo waiting for $dir
     46         if test -e $dir/notification-fd; then flag="-U"; else flag="-u"; fi
     47         ${s6}/bin/s6-svwait $flag /run/service/${name} || exit
     48         PATH=${s6-rc}/bin:${s6}/bin:${s6-rc-up-tree}/bin:$PATH
     49         ${watch-outputs}/bin/watch-outputs ${restart-flag} ${name} ${refs}
     50       '';
     51     };
     52 in
     53 service.overrideAttrs (o: {
     54   buildInputs = (lim.orEmpty o.buildInputs) ++ optional (watch != [ ]) watcher;
     55   dependencies =
     56     (lim.orEmpty o.dependencies)
     57     ++ optional (watch != [ ]) watcher
     58     ++ optionals (watch != [ ]) (map (s: s "service") watch);
     59 })