blob: ac83da920638689faf948bf0fe34f5a712c2aa5b (
plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.quagga;
services = [ "babel" "bgp" "isis" "ospf6" "ospf" "pim" "rip" "ripng" ];
allServices = services ++ [ "zebra" ];
isEnabled = service: cfg.${service}.enable;
daemonName = service: if service == "zebra" then service else "${service}d";
configFile = service:
let
scfg = cfg.${service};
in
if scfg.configFile != null then scfg.configFile
else pkgs.writeText "${daemonName service}.conf"
''
! Quagga ${daemonName service} configuration
!
hostname ${config.networking.hostName}
log syslog
service password-encryption
!
${scfg.config}
!
end
'';
serviceOptions = service:
{
enable = mkEnableOption "the Quagga ${toUpper service} routing protocol";
configFile = mkOption {
type = types.nullOr types.path;
default = null;
example = "/etc/quagga/${daemonName service}.conf";
description = ''
Configuration file to use for Quagga ${daemonName service}.
By default the NixOS generated files are used.
'';
};
config = mkOption {
type = types.lines;
default = "";
example =
let
examples = {
rip = ''
router rip
network 10.0.0.0/8
'';
ospf = ''
router ospf
network 10.0.0.0/8 area 0
'';
bgp = ''
router bgp 65001
neighbor 10.0.0.1 remote-as 65001
'';
};
in
examples.${service} or "";
description = ''
${daemonName service} configuration statements.
'';
};
vtyListenAddress = mkOption {
type = types.str;
default = "127.0.0.1";
description = ''
Address to bind to for the VTY interface.
'';
};
vtyListenPort = mkOption {
type = types.nullOr types.int;
default = null;
description = ''
TCP Port to bind to for the VTY interface.
'';
};
};
in
{
###### interface
options.services.quagga =
{
zebra = (serviceOptions "zebra") // {
enable = mkOption {
type = types.bool;
default = any isEnabled services;
example = true;
description = ''
Whether to enable the Zebra routing manager.
The Zebra routing manager is automatically enabled
if any routing protocols are configured.
'';
};
};
} // (genAttrs services serviceOptions);
###### implementation
config = mkIf (any isEnabled allServices) {
environment.systemPackages = [
pkgs.quagga # for the vtysh tool
];
users.users.quagga = {
description = "Quagga daemon user";
isSystemUser = true;
group = "quagga";
};
users.groups = {
quagga = {};
# Members of the quaggavty group can use vtysh to inspect the Quagga daemons
quaggavty = {};
};
systemd.services =
let
quaggaService = service:
let
scfg = cfg.${service};
daemon = daemonName service;
in
nameValuePair daemon ({
wantedBy = [ "multi-user.target" ];
restartTriggers = [ (configFile service) ];
serviceConfig = {
Type = "forking";
PIDFile = "/run/quagga/${daemon}.pid";
ExecStart = "@${pkgs.quagga}/libexec/quagga/${daemon} ${daemon} -d -f ${configFile service}"
+ optionalString (scfg.vtyListenAddress != "") " -A ${scfg.vtyListenAddress}"
+ optionalString (scfg.vtyListenPort != null) " -P ${toString scfg.vtyListenPort}";
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
Restart = "on-abort";
};
} // (
if service == "zebra" then
{
description = "Quagga Zebra routing manager";
unitConfig.Documentation = "man:zebra(8)";
after = [ "network.target" ];
preStart = ''
install -m 0755 -o quagga -g quagga -d /run/quagga
${pkgs.iproute}/bin/ip route flush proto zebra
'';
}
else
{
description = "Quagga ${toUpper service} routing daemon";
unitConfig.Documentation = "man:${daemon}(8) man:zebra(8)";
bindsTo = [ "zebra.service" ];
after = [ "network.target" "zebra.service" ];
}
));
in
listToAttrs (map quaggaService (filter isEnabled allServices));
};
meta.maintainers = with lib.maintainers; [ tavyc ];
}
|