···
1
+
{ config, lib, pkgs, ... }:
7
+
cfg = config.services.frr;
28
+
allServices = services ++ [ "zebra" ];
30
+
isEnabled = service: cfg.${service}.enable;
32
+
daemonName = service: if service == "zebra" then service else "${service}d";
34
+
configFile = service:
36
+
scfg = cfg.${service};
38
+
if scfg.configFile != null then scfg.configFile
39
+
else pkgs.writeText "${daemonName service}.conf"
41
+
! FRR ${daemonName service} configuration
43
+
hostname ${config.networking.hostName}
45
+
service password-encryption
52
+
serviceOptions = service:
54
+
enable = mkEnableOption "the FRR ${toUpper service} routing protocol";
56
+
configFile = mkOption {
57
+
type = types.nullOr types.path;
59
+
example = "/etc/frr/${daemonName service}.conf";
61
+
Configuration file to use for FRR ${daemonName service}.
62
+
By default the NixOS generated files are used.
79
+
network 10.0.0.0/8 area 0
84
+
neighbor 10.0.0.1 remote-as 65001
88
+
examples.${service} or "";
90
+
${daemonName service} configuration statements.
94
+
vtyListenAddress = mkOption {
96
+
default = "localhost";
98
+
Address to bind to for the VTY interface.
102
+
vtyListenPort = mkOption {
103
+
type = types.nullOr types.int;
106
+
TCP Port to bind to for the VTY interface.
118
+
options.services.frr = {
119
+
zebra = (serviceOptions "zebra") // {
120
+
enable = mkOption {
122
+
default = any isEnabled services;
124
+
Whether to enable the Zebra routing manager.
126
+
The Zebra routing manager is automatically enabled
127
+
if any routing protocols are configured.
133
+
{ options.services.frr = (genAttrs services serviceOptions); }
136
+
###### implementation
138
+
config = mkIf (any isEnabled allServices) {
140
+
environment.systemPackages = [
141
+
pkgs.frr # for the vtysh tool
144
+
users.users.frr = {
145
+
description = "FRR daemon user";
146
+
isSystemUser = true;
152
+
# Members of the frrvty group can use vtysh to inspect the FRR daemons
153
+
frrvty = { members = [ "frr" ]; };
156
+
environment.etc = let
157
+
mkEtcLink = service: {
158
+
name = "frr/${service}.conf";
159
+
value.source = configFile service;
162
+
(builtins.listToAttrs
163
+
(map mkEtcLink (filter isEnabled allServices))) // {
164
+
"frr/vtysh.conf".text = "";
167
+
systemd.tmpfiles.rules = [
168
+
"d /run/frr 0750 frr frr -"
173
+
frrService = service:
175
+
scfg = cfg.${service};
176
+
daemon = daemonName service;
178
+
nameValuePair daemon ({
179
+
wantedBy = [ "multi-user.target" ];
180
+
after = [ "network-pre.target" "systemd-sysctl.service" ] ++ lib.optionals (service != "zebra") [ "zebra.service" ];
181
+
bindsTo = lib.optionals (service != "zebra") [ "zebra.service" ];
182
+
wants = [ "network.target" ];
184
+
description = if service == "zebra" then "FRR Zebra routing manager"
185
+
else "FRR ${toUpper service} routing daemon";
187
+
unitConfig.Documentation = if service == "zebra" then "man:zebra(8)"
188
+
else "man:${daemon}(8) man:zebra(8)";
190
+
restartTriggers = [
191
+
(configFile service)
193
+
reloadIfChanged = true;
196
+
PIDFile = "frr/${daemon}.pid";
197
+
ExecStart = "${pkgs.frr}/libexec/frr/${daemon} -f /etc/frr/${service}.conf"
198
+
+ optionalString (scfg.vtyListenAddress != "") " -A ${scfg.vtyListenAddress}"
199
+
+ optionalString (scfg.vtyListenPort != null) " -P ${toString scfg.vtyListenPort}";
200
+
ExecReload = "${pkgs.python3.interpreter} ${pkgs.frr}/libexec/frr/frr-reload.py --reload --daemon ${daemonName service} --bindir ${pkgs.frr}/bin --rundir /run/frr /etc/frr/${service}.conf";
201
+
Restart = "on-abnormal";
205
+
listToAttrs (map frrService (filter isEnabled allServices));
209
+
meta.maintainers = with lib.maintainers; [ woffs ];