2013-11-04 20:32:40 +04:00
|
|
|
{ pkgs, config, ... }:
|
|
|
|
with pkgs.lib;
|
|
|
|
let
|
|
|
|
services = config.systemd.services;
|
|
|
|
|
|
|
|
isOneShot = cfg: hasAttr "Type" cfg.serviceConfig && cfg.serviceConfig.Type == "oneshot";
|
|
|
|
|
|
|
|
runServices = filterAttrs (name: cfg: !(isOneShot cfg)) services;
|
|
|
|
|
|
|
|
oneShotServices = filterAttrs (name: cfg: isOneShot cfg) services;
|
|
|
|
|
2014-02-17 03:31:11 +04:00
|
|
|
filterCommand = cmd:
|
|
|
|
let
|
|
|
|
filtered = substring 1 (stringLength cmd -2) cmd;
|
|
|
|
splitted = pkgs.lib.splitString " " filtered;
|
|
|
|
in if eqStrings (substring 0 1 cmd) "@" then
|
|
|
|
traceVal (head splitted) + concatStringsSep " " (drop 2 splitted)
|
|
|
|
else cmd;
|
|
|
|
|
2013-11-04 20:32:40 +04:00
|
|
|
configToCommand = name: cfg: ''
|
|
|
|
#!/bin/sh -e
|
|
|
|
${if hasAttr "preStart" cfg then cfg.preStart else ""}
|
|
|
|
${if hasAttr "ExecStart" cfg.serviceConfig then
|
2014-02-17 03:31:11 +04:00
|
|
|
filterCommand cfg.serviceConfig.ExecStart
|
2013-11-04 20:32:40 +04:00
|
|
|
else if hasAttr "script" cfg then
|
|
|
|
cfg.script
|
|
|
|
else
|
|
|
|
""
|
|
|
|
}
|
|
|
|
${if hasAttr "postStart" cfg then cfg.postStart else ""}
|
|
|
|
'';
|
|
|
|
|
|
|
|
in {
|
|
|
|
|
|
|
|
options = {
|
2013-11-05 19:26:41 +04:00
|
|
|
systemd.services = mkOption {
|
|
|
|
default = {};
|
|
|
|
}; # TODO make more specific
|
2013-11-04 20:32:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
2014-02-17 03:31:11 +04:00
|
|
|
userNix.startScripts."1-systemd-oneshot" = concatMapStrings (name: "${configToCommand name (getAttr name oneShotServices)}\n") (attrNames oneShotServices);
|
2013-11-04 20:32:40 +04:00
|
|
|
|
|
|
|
supervisord.services = listToAttrs (map (name:
|
|
|
|
let
|
2014-02-17 03:31:11 +04:00
|
|
|
cfg = getAttr name services;
|
2013-11-04 20:32:40 +04:00
|
|
|
in
|
|
|
|
{
|
|
|
|
name = name;
|
|
|
|
value = {
|
|
|
|
command = pkgs.writeScript "${name}-run" (configToCommand name cfg);
|
|
|
|
environment = (if hasAttr "environment" cfg then cfg.environment else {}) //
|
|
|
|
(if hasAttr "path" cfg then
|
2014-02-17 03:31:11 +04:00
|
|
|
{ PATH = "%(ENV_PATH)s:" + concatStringsSep ":" (map (prg: "${prg}/bin") cfg.path); }
|
|
|
|
else {
|
|
|
|
PATH="%(ENV_PATH)s"; });
|
2013-11-04 20:32:40 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
) (attrNames runServices));
|
|
|
|
};
|
2014-02-17 03:31:11 +04:00
|
|
|
}
|