liminix

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

default.nix (3260B)


      1 {
      2   stdenvNoCC,
      3   s6,
      4   lib,
      5   writeScript,
      6   serviceFns,
      7 }:
      8 let
      9   prefix = "/run/services/outputs";
     10   output = service: name: "${prefix}/${service.name}/${name}";
     11   inherit (lib.attrsets) mapAttrsRecursive collect;
     12   inherit (lib.strings) concatStringsSep;
     13   serviceScript = commands: ''
     14     #!/bin/sh
     15     exec 2>&1
     16     . ${serviceFns}
     17     ${commands}
     18   '';
     19   cleanupScript = name: cmds: ''
     20     #!/bin/sh
     21     ${if cmds != null then cmds else ""}
     22     if test -d ${prefix}/${name} ; then rm -rf ${prefix}/${name} ; fi
     23   '';
     24   service =
     25     {
     26       name,
     27       serviceType,
     28       run ? null,
     29       up ? null,
     30       down ? null,
     31       finish ? null,
     32       notification-fd ? null,
     33       producer-for ? null,
     34       consumer-for ? null,
     35       pipeline-name ? null,
     36       timeout-up ? 30000, # milliseconds
     37       timeout-down ? 0,
     38       dependencies ? [ ],
     39       contents ? [ ],
     40       buildInputs ? [ ],
     41       restart-on-upgrade ? false,
     42       controller ? null,
     43       properties ? { },
     44     }:
     45     stdenvNoCC.mkDerivation {
     46       # we use stdenvNoCC to avoid generating derivations with names
     47       # like foo.service-mips-linux-musl
     48       inherit
     49         name
     50         serviceType
     51         up
     52         down
     53         run
     54         finish
     55         notification-fd
     56         producer-for
     57         consumer-for
     58         pipeline-name
     59         timeout-up
     60         timeout-down
     61         restart-on-upgrade
     62         ;
     63       propertiesText =
     64         let
     65           a = mapAttrsRecursive (
     66             path: value: "writepath ${concatStringsSep "/" path} ${builtins.toString value}\n"
     67           ) properties;
     68         in
     69         collect builtins.isString a;
     70 
     71       buildInputs =
     72         buildInputs ++ dependencies ++ contents ++ lib.optional (controller != null) controller;
     73       inherit controller dependencies contents;
     74       builder = ./builder.sh;
     75     };
     76 
     77   longrun =
     78     {
     79       name,
     80       run,
     81       finish ? null,
     82       notification-fd ? null,
     83       buildInputs ? [ ],
     84       producer-for ? null,
     85       ...
     86     }@args:
     87     let
     88       logger = service {
     89         serviceType = "longrun";
     90         name = "${name}-log";
     91         run = serviceScript "${s6}/bin/s6-log -d 10 -- p${name} 1";
     92         notification-fd = 10;
     93         consumer-for = name;
     94         pipeline-name = "${name}-pipeline";
     95       };
     96     in
     97     service (
     98       args
     99       // {
    100         buildInputs = buildInputs ++ lib.optional (producer-for == null) logger;
    101         serviceType = "longrun";
    102         run = serviceScript run;
    103         finish = cleanupScript name finish;
    104         producer-for = if producer-for != null then producer-for else "${name}-log";
    105       }
    106     );
    107 
    108   oneshot =
    109     {
    110       name,
    111       up,
    112       down ? "",
    113       ...
    114     }@args:
    115     service (
    116       args
    117       // {
    118         serviceType = "oneshot";
    119         up = writeScript "${name}-up" (serviceScript up);
    120         down = writeScript "${name}-down" "${serviceScript down}\n${cleanupScript name null}";
    121       }
    122     );
    123   bundle =
    124     {
    125       contents ? [ ],
    126       dependencies ? [ ],
    127       ...
    128     }@args:
    129     service (
    130       args
    131       // {
    132         serviceType = "bundle";
    133         inherit contents dependencies;
    134       }
    135     );
    136   target = bundle;
    137 in
    138 {
    139   inherit
    140     target
    141     bundle
    142     oneshot
    143     output
    144     ;
    145   longrun = lib.makeOverridable longrun;
    146 }