nixos/services.frr: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-28 21:19:08 +02:00 committed by Jörg Thalheim
parent fdcec053e6
commit 191b68cd26

View File

@ -1,7 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib;
let let
cfg = config.services.frr; cfg = config.services.frr;
@ -51,10 +48,10 @@ let
serviceOptions = service: serviceOptions = service:
{ {
enable = mkEnableOption "the FRR ${toUpper service} routing protocol"; enable = lib.mkEnableOption "the FRR ${lib.toUpper service} routing protocol";
configFile = mkOption { configFile = lib.mkOption {
type = types.nullOr types.path; type = lib.types.nullOr lib.types.path;
default = null; default = null;
example = "/etc/frr/${daemonName service}.conf"; example = "/etc/frr/${daemonName service}.conf";
description = '' description = ''
@ -63,8 +60,8 @@ let
''; '';
}; };
config = mkOption { config = lib.mkOption {
type = types.lines; type = lib.types.lines;
default = ""; default = "";
example = example =
let let
@ -91,24 +88,24 @@ let
''; '';
}; };
vtyListenAddress = mkOption { vtyListenAddress = lib.mkOption {
type = types.str; type = lib.types.str;
default = "localhost"; default = "localhost";
description = '' description = ''
Address to bind to for the VTY interface. Address to bind to for the VTY interface.
''; '';
}; };
vtyListenPort = mkOption { vtyListenPort = lib.mkOption {
type = types.nullOr types.int; type = lib.types.nullOr lib.types.int;
default = null; default = null;
description = '' description = ''
TCP Port to bind to for the VTY interface. TCP Port to bind to for the VTY interface.
''; '';
}; };
extraOptions = mkOption { extraOptions = lib.mkOption {
type = types.listOf types.str; type = lib.types.listOf lib.types.str;
default = []; default = [];
description = '' description = ''
Extra options for the daemon. Extra options for the daemon.
@ -125,9 +122,9 @@ in
{ {
options.services.frr = { options.services.frr = {
zebra = (serviceOptions "zebra") // { zebra = (serviceOptions "zebra") // {
enable = mkOption { enable = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = any isEnabled services; default = lib.any isEnabled services;
description = '' description = ''
Whether to enable the Zebra routing manager. Whether to enable the Zebra routing manager.
@ -137,8 +134,8 @@ in
}; };
}; };
mgmt = (serviceOptions "mgmt") // { mgmt = (serviceOptions "mgmt") // {
enable = mkOption { enable = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = isEnabled "static"; default = isEnabled "static";
defaultText = lib.literalExpression "config.services.frr.static.enable"; defaultText = lib.literalExpression "config.services.frr.static.enable";
description = '' description = ''
@ -152,12 +149,12 @@ in
}; };
}; };
} }
{ options.services.frr = (genAttrs services serviceOptions); } { options.services.frr = (lib.genAttrs services serviceOptions); }
]; ];
###### implementation ###### implementation
config = mkIf (any isEnabled allServices) { config = lib.mkIf (lib.any isEnabled allServices) {
environment.systemPackages = [ environment.systemPackages = [
pkgs.frr # for the vtysh tool pkgs.frr # for the vtysh tool
@ -182,7 +179,7 @@ in
}; };
in in
(builtins.listToAttrs (builtins.listToAttrs
(map mkEtcLink (filter isEnabled allServices))) // { (map mkEtcLink (lib.filter isEnabled allServices))) // {
"frr/vtysh.conf".text = ""; "frr/vtysh.conf".text = "";
}; };
@ -197,19 +194,19 @@ in
scfg = cfg.${service}; scfg = cfg.${service};
daemon = daemonName service; daemon = daemonName service;
in in
nameValuePair daemon ({ lib.nameValuePair daemon ({
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
after = [ "network-pre.target" "systemd-sysctl.service" ] ++ lib.optionals (service != "zebra") [ "zebra.service" ]; after = [ "network-pre.target" "systemd-sysctl.service" ] ++ lib.optionals (service != "zebra") [ "zebra.service" ];
bindsTo = lib.optionals (service != "zebra") [ "zebra.service" ]; bindsTo = lib.optionals (service != "zebra") [ "zebra.service" ];
wants = [ "network.target" ]; wants = [ "network.target" ];
description = if service == "zebra" then "FRR Zebra routing manager" description = if service == "zebra" then "FRR Zebra routing manager"
else "FRR ${toUpper service} routing daemon"; else "FRR ${lib.toUpper service} routing daemon";
unitConfig.Documentation = if service == "zebra" then "man:zebra(8)" unitConfig.Documentation = if service == "zebra" then "man:zebra(8)"
else "man:${daemon}(8) man:zebra(8)"; else "man:${daemon}(8) man:zebra(8)";
restartTriggers = mkIf (service != "mgmt") [ restartTriggers = lib.mkIf (service != "mgmt") [
(configFile service) (configFile service)
]; ];
reloadIfChanged = (service != "mgmt"); reloadIfChanged = (service != "mgmt");
@ -217,15 +214,15 @@ in
serviceConfig = { serviceConfig = {
PIDFile = "frr/${daemon}.pid"; PIDFile = "frr/${daemon}.pid";
ExecStart = "${pkgs.frr}/libexec/frr/${daemon}" ExecStart = "${pkgs.frr}/libexec/frr/${daemon}"
+ optionalString (scfg.vtyListenAddress != "") " -A ${scfg.vtyListenAddress}" + lib.optionalString (scfg.vtyListenAddress != "") " -A ${scfg.vtyListenAddress}"
+ optionalString (scfg.vtyListenPort != null) " -P ${toString scfg.vtyListenPort}" + lib.optionalString (scfg.vtyListenPort != null) " -P ${toString scfg.vtyListenPort}"
+ " " + (concatStringsSep " " scfg.extraOptions); + " " + (lib.concatStringsSep " " scfg.extraOptions);
ExecReload = mkIf (service != "mgmt") "${pkgs.python3.interpreter} ${pkgs.frr}/libexec/frr/frr-reload.py --reload --daemon ${daemon} --bindir ${pkgs.frr}/bin --rundir /run/frr /etc/frr/${daemon}.conf"; ExecReload = lib.mkIf (service != "mgmt") "${pkgs.python3.interpreter} ${pkgs.frr}/libexec/frr/frr-reload.py --reload --daemon ${daemon} --bindir ${pkgs.frr}/bin --rundir /run/frr /etc/frr/${daemon}.conf";
Restart = "on-abnormal"; Restart = "on-abnormal";
}; };
}); });
in in
listToAttrs (map frrService (filter isEnabled allServices)); lib.listToAttrs (map frrService (lib.filter isEnabled allServices));
}; };