nix-rehash/systemd.nix

62 lines
1.8 KiB
Nix
Raw Normal View History

{ 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;
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
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
};
config = {
2014-02-17 03:31:11 +04:00
userNix.startScripts."1-systemd-oneshot" = concatMapStrings (name: "${configToCommand name (getAttr name oneShotServices)}\n") (attrNames oneShotServices);
supervisord.services = listToAttrs (map (name:
let
2014-02-17 03:31:11 +04:00
cfg = getAttr name services;
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"; });
};
}
) (attrNames runServices));
};
2014-02-17 03:31:11 +04:00
}