-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathservices.nix
More file actions
121 lines (119 loc) · 3.22 KB
/
services.nix
File metadata and controls
121 lines (119 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
{
config,
lib,
pkgs,
...
}:
let
inherit (lib)
mkOption
types
;
serviceOptions = {
name = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Name of this service. Defaults to attribute name in group services.
'';
};
command = mkOption {
type = types.str;
description = ''
Command to execute.
'';
};
};
groupOptions = {
name = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Name of the service group. Defaults to attribute name in groups.
'';
};
description = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Short description of the service group, shown in generated commands
'';
};
services = mkOption {
type = types.attrsOf (types.submodule { options = serviceOptions; });
default = { };
description = ''
Attrset of services that should be run in this group.
'';
};
};
groupToProcfile =
name: g:
pkgs.writeText "Procfile.${name}" (
lib.concatLines (
lib.mapAttrsToList (
sName: s: "${if s.name == null then sName else s.name}: ${s.command}"
) g.services
)
);
groupToCommands =
gName: g:
let
procfile = groupToProcfile gName g;
description = if g.description == null then gName else g.description;
in
[
{
name = "${gName}:start";
category = "service groups";
help = "Start ${description} services";
command =
(pkgs.writeShellScript "${gName}-services-start" ''
if [ -e "$PRJ_DATA_DIR/pids/${gName}.pid" ]; then
echo "Already running, refusing to start"
exit 1
fi
mkdir -p "$PRJ_DATA_DIR/pids/"
${pkgs.honcho}/bin/honcho start -f ${procfile} -d "$PRJ_ROOT" &
pid=$!
echo $pid > "$PRJ_DATA_DIR/pids/${gName}.pid"
on_stop() {
if ps -p $pid > /dev/null; then
kill -TERM $pid
fi
rm "$PRJ_DATA_DIR/pids/${gName}.pid"
wait $pid
}
trap "on_stop" SIGINT SIGTERM SIGHUP EXIT
wait $pid
'').outPath;
}
{
name = "${gName}:stop";
category = "service groups";
help = "Stop ${description} services";
command =
(pkgs.writeShellScript "${gName}-services-stop" ''
if [ -e "$PRJ_DATA_DIR/pids/${gName}.pid" ]; then
pid=$(${pkgs.coreutils}/bin/cat "$PRJ_DATA_DIR/pids/${gName}.pid")
kill -TERM $pid
rm "$PRJ_DATA_DIR/pids/${gName}.pid"
fi
'').outPath;
}
];
in
{
options.serviceGroups = mkOption {
type = types.attrsOf (types.submodule { options = groupOptions; });
default = { };
description = ''
Add services to the environment. Services can be used to group long-running processes.
'';
};
config.commands = lib.foldl (l: r: l ++ r) [ ] (
lib.mapAttrsToList (
gName: g: groupToCommands (if g.name == null then gName else g.name) g
) config.serviceGroups
);
}