default.nix (3234B)
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 a = mapAttrsRecursive 65 (path: value: "writepath ${concatStringsSep "/" path} ${builtins.toString value}\n") 66 properties; 67 in collect builtins.isString a; 68 69 buildInputs = 70 buildInputs ++ dependencies ++ contents ++ lib.optional (controller != null) controller; 71 inherit controller dependencies contents; 72 builder = ./builder.sh; 73 }; 74 75 longrun = 76 { 77 name, 78 run, 79 finish ? null, 80 notification-fd ? null, 81 buildInputs ? [ ], 82 producer-for ? null, 83 ... 84 }@args: 85 let 86 logger = service { 87 serviceType = "longrun"; 88 name = "${name}-log"; 89 run = serviceScript "${s6}/bin/s6-log -d 10 -- p${name} 1"; 90 notification-fd = 10; 91 consumer-for = name; 92 pipeline-name = "${name}-pipeline"; 93 }; 94 in 95 service ( 96 args 97 // { 98 buildInputs = buildInputs ++ lib.optional (producer-for == null) logger; 99 serviceType = "longrun"; 100 run = serviceScript run; 101 finish = cleanupScript name finish; 102 producer-for = if producer-for != null then producer-for else "${name}-log"; 103 } 104 ); 105 106 oneshot = 107 { 108 name, 109 up, 110 down ? "", 111 ... 112 }@args: 113 service ( 114 args 115 // { 116 serviceType = "oneshot"; 117 up = writeScript "${name}-up" (serviceScript up); 118 down = writeScript "${name}-down" "${serviceScript down}\n${cleanupScript name null}"; 119 } 120 ); 121 bundle = 122 { 123 contents ? [ ], 124 dependencies ? [ ], 125 ... 126 }@args: 127 service ( 128 args 129 // { 130 serviceType = "bundle"; 131 inherit contents dependencies; 132 } 133 ); 134 target = bundle; 135 in 136 { 137 inherit 138 target 139 bundle 140 oneshot 141 output 142 ; 143 longrun = lib.makeOverridable longrun; 144 }