2013-11-04 20:32:40 +04:00
|
|
|
{ pkgs, config, ... }:
|
2014-03-07 20:59:44 +04:00
|
|
|
with pkgs.lib;
|
|
|
|
with import <nixpkgs/nixos/modules/system/boot/systemd-unit-options.nix> { inherit config pkgs; };
|
2013-11-04 20:32:40 +04:00
|
|
|
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 = {};
|
2014-03-07 20:59:44 +04:00
|
|
|
type = types.attrsOf types.optionSet;
|
|
|
|
options = [ serviceOptions ];
|
2013-11-05 19:26:41 +04:00
|
|
|
}; # 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);
|
2014-03-07 20:57:45 +04:00
|
|
|
environment = cfg.environment;
|
|
|
|
path = cfg.path;
|
2014-02-18 02:15:41 +04:00
|
|
|
stopsignal = if hasAttr "KillSignal" cfg.serviceConfig then
|
|
|
|
substring 3 (stringLength cfg.serviceConfig.KillSignal) cfg.serviceConfig.KillSignal
|
|
|
|
else "TERM";
|
2013-11-04 20:32:40 +04:00
|
|
|
};
|
|
|
|
}
|
2014-03-07 21:00:16 +04:00
|
|
|
) (attrNames (filterAttrs (n: v: v.enable) runServices)));
|
2013-11-04 20:32:40 +04:00
|
|
|
};
|
2014-02-17 03:31:11 +04:00
|
|
|
}
|