0
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-13 21:50:33 +03:00

nixos/services.adguardhome: remove with lib;

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

View file

@ -1,12 +1,9 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib;
let let
cfg = config.services.adguardhome; cfg = config.services.adguardhome;
settingsFormat = pkgs.formats.yaml { }; settingsFormat = pkgs.formats.yaml { };
args = concatStringsSep " " ([ args = lib.concatStringsSep " " ([
"--no-check-update" "--no-check-update"
"--pidfile /run/AdGuardHome/AdGuardHome.pid" "--pidfile /run/AdGuardHome/AdGuardHome.pid"
"--work-dir /var/lib/AdGuardHome/" "--work-dir /var/lib/AdGuardHome/"
@ -28,19 +25,19 @@ let
checkPhase = "${cfg.package}/bin/adguardhome -c $out --check-config"; checkPhase = "${cfg.package}/bin/adguardhome -c $out --check-config";
}); });
in { in {
options.services.adguardhome = with types; { options.services.adguardhome = with lib.types; {
enable = mkEnableOption "AdGuard Home network-wide ad blocker"; enable = lib.mkEnableOption "AdGuard Home network-wide ad blocker";
package = mkOption { package = lib.mkOption {
type = package; type = package;
default = pkgs.adguardhome; default = pkgs.adguardhome;
defaultText = literalExpression "pkgs.adguardhome"; defaultText = lib.literalExpression "pkgs.adguardhome";
description = '' description = ''
The package that runs adguardhome. The package that runs adguardhome.
''; '';
}; };
openFirewall = mkOption { openFirewall = lib.mkOption {
default = false; default = false;
type = bool; type = bool;
description = '' description = ''
@ -49,9 +46,9 @@ in {
''; '';
}; };
allowDHCP = mkOption { allowDHCP = lib.mkOption {
default = settings.dhcp.enabled or false; default = settings.dhcp.enabled or false;
defaultText = literalExpression "config.services.adguardhome.settings.dhcp.enabled or false"; defaultText = lib.literalExpression "config.services.adguardhome.settings.dhcp.enabled or false";
type = bool; type = bool;
description = '' description = ''
Allows AdGuard Home to open raw sockets (`CAP_NET_RAW`), which is Allows AdGuard Home to open raw sockets (`CAP_NET_RAW`), which is
@ -63,7 +60,7 @@ in {
''; '';
}; };
mutableSettings = mkOption { mutableSettings = lib.mkOption {
default = true; default = true;
type = bool; type = bool;
description = '' description = ''
@ -72,7 +69,7 @@ in {
''; '';
}; };
host = mkOption { host = lib.mkOption {
default = "0.0.0.0"; default = "0.0.0.0";
type = str; type = str;
description = '' description = ''
@ -80,7 +77,7 @@ in {
''; '';
}; };
port = mkOption { port = lib.mkOption {
default = 3000; default = 3000;
type = port; type = port;
description = '' description = ''
@ -88,14 +85,14 @@ in {
''; '';
}; };
settings = mkOption { settings = lib.mkOption {
default = null; default = null;
type = nullOr (submodule { type = nullOr (submodule {
freeformType = settingsFormat.type; freeformType = settingsFormat.type;
options = { options = {
schema_version = mkOption { schema_version = lib.mkOption {
default = cfg.package.schema_version; default = cfg.package.schema_version;
defaultText = literalExpression "cfg.package.schema_version"; defaultText = lib.literalExpression "cfg.package.schema_version";
type = int; type = int;
description = '' description = ''
Schema version for the configuration. Schema version for the configuration.
@ -121,7 +118,7 @@ in {
''; '';
}; };
extraArgs = mkOption { extraArgs = lib.mkOption {
default = [ ]; default = [ ];
type = listOf str; type = listOf str;
description = '' description = ''
@ -130,27 +127,27 @@ in {
}; };
}; };
config = mkIf cfg.enable { config = lib.mkIf cfg.enable {
assertions = [ assertions = [
{ {
assertion = cfg.settings != null assertion = cfg.settings != null
-> !(hasAttrByPath [ "bind_host" ] cfg.settings); -> !(lib.hasAttrByPath [ "bind_host" ] cfg.settings);
message = "AdGuard option `settings.bind_host' has been superseded by `services.adguardhome.host'"; message = "AdGuard option `settings.bind_host' has been superseded by `services.adguardhome.host'";
} }
{ {
assertion = cfg.settings != null assertion = cfg.settings != null
-> !(hasAttrByPath [ "bind_port" ] cfg.settings); -> !(lib.hasAttrByPath [ "bind_port" ] cfg.settings);
message = "AdGuard option `settings.bind_port' has been superseded by `services.adguardhome.port'"; message = "AdGuard option `settings.bind_port' has been superseded by `services.adguardhome.port'";
} }
{ {
assertion = settings != null -> cfg.mutableSettings assertion = settings != null -> cfg.mutableSettings
|| hasAttrByPath [ "dns" "bootstrap_dns" ] settings; || lib.hasAttrByPath [ "dns" "bootstrap_dns" ] settings;
message = "AdGuard setting dns.bootstrap_dns needs to be configured for a minimal working configuration"; message = "AdGuard setting dns.bootstrap_dns needs to be configured for a minimal working configuration";
} }
{ {
assertion = settings != null -> cfg.mutableSettings assertion = settings != null -> cfg.mutableSettings
|| hasAttrByPath [ "dns" "bootstrap_dns" ] settings || lib.hasAttrByPath [ "dns" "bootstrap_dns" ] settings
&& isList settings.dns.bootstrap_dns; && lib.isList settings.dns.bootstrap_dns;
message = "AdGuard setting dns.bootstrap_dns needs to be a list"; message = "AdGuard setting dns.bootstrap_dns needs to be a list";
} }
]; ];
@ -164,7 +161,7 @@ in {
StartLimitBurst = 10; StartLimitBurst = 10;
}; };
preStart = optionalString (settings != null) '' preStart = lib.optionalString (settings != null) ''
if [ -e "$STATE_DIRECTORY/AdGuardHome.yaml" ] \ if [ -e "$STATE_DIRECTORY/AdGuardHome.yaml" ] \
&& [ "${toString cfg.mutableSettings}" = "1" ]; then && [ "${toString cfg.mutableSettings}" = "1" ]; then
# First run a schema_version update on the existing configuration # First run a schema_version update on the existing configuration
@ -185,7 +182,7 @@ in {
DynamicUser = true; DynamicUser = true;
ExecStart = "${lib.getExe cfg.package} ${args}"; ExecStart = "${lib.getExe cfg.package} ${args}";
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ] AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ]
++ optionals cfg.allowDHCP [ "CAP_NET_RAW" ]; ++ lib.optionals cfg.allowDHCP [ "CAP_NET_RAW" ];
Restart = "always"; Restart = "always";
RestartSec = 10; RestartSec = 10;
RuntimeDirectory = "AdGuardHome"; RuntimeDirectory = "AdGuardHome";
@ -193,6 +190,6 @@ in {
}; };
}; };
networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ]; networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.port ];
}; };
} }