2021-04-30 18:55:31 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.adguardhome;
|
2024-04-19 16:37:19 +00:00
|
|
|
settingsFormat = pkgs.formats.yaml { };
|
2021-04-30 18:55:31 +00:00
|
|
|
|
|
|
|
args = concatStringsSep " " ([
|
|
|
|
"--no-check-update"
|
|
|
|
"--pidfile /run/AdGuardHome/AdGuardHome.pid"
|
|
|
|
"--work-dir /var/lib/AdGuardHome/"
|
|
|
|
"--config /var/lib/AdGuardHome/AdGuardHome.yaml"
|
|
|
|
] ++ cfg.extraArgs);
|
|
|
|
|
2024-04-19 16:37:19 +00:00
|
|
|
settings = if (cfg.settings != null) then
|
|
|
|
cfg.settings // (if cfg.settings.schema_version < 23 then {
|
|
|
|
bind_host = cfg.host;
|
|
|
|
bind_port = cfg.port;
|
|
|
|
} else {
|
|
|
|
http.address = "${cfg.host}:${toString cfg.port}";
|
|
|
|
})
|
|
|
|
else
|
|
|
|
null;
|
|
|
|
|
|
|
|
configFile =
|
|
|
|
(settingsFormat.generate "AdGuardHome.yaml" settings).overrideAttrs (_: {
|
|
|
|
checkPhase = "${cfg.package}/bin/adguardhome -c $out --check-config";
|
|
|
|
});
|
|
|
|
in {
|
2022-06-07 11:35:22 +00:00
|
|
|
options.services.adguardhome = with types; {
|
|
|
|
enable = mkEnableOption "AdGuard Home network-wide ad blocker";
|
2021-04-30 18:55:31 +00:00
|
|
|
|
2024-04-19 16:37:19 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = package;
|
|
|
|
default = pkgs.adguardhome;
|
|
|
|
defaultText = literalExpression "pkgs.adguardhome";
|
|
|
|
description = ''
|
|
|
|
The package that runs adguardhome.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-04-30 18:55:31 +00:00
|
|
|
openFirewall = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = bool;
|
|
|
|
description = ''
|
|
|
|
Open ports in the firewall for the AdGuard Home web interface. Does not
|
|
|
|
open the port needed to access the DNS resolver.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-01-04 17:32:27 +00:00
|
|
|
allowDHCP = mkOption {
|
2024-04-19 16:37:19 +00:00
|
|
|
default = settings.dhcp.enabled or false;
|
|
|
|
defaultText = literalExpression "config.services.adguardhome.settings.dhcp.enabled or false";
|
2023-01-04 17:32:27 +00:00
|
|
|
type = bool;
|
|
|
|
description = ''
|
|
|
|
Allows AdGuard Home to open raw sockets (`CAP_NET_RAW`), which is
|
|
|
|
required for the integrated DHCP server.
|
|
|
|
|
|
|
|
The default enables this conditionally if the declarative configuration
|
|
|
|
enables the integrated DHCP server. Manually setting this option is only
|
|
|
|
required for non-declarative setups.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-12-24 17:52:39 +00:00
|
|
|
mutableSettings = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = bool;
|
|
|
|
description = ''
|
|
|
|
Allow changes made on the AdGuard Home web interface to persist between
|
|
|
|
service restarts.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-04-19 16:37:19 +00:00
|
|
|
host = mkOption {
|
|
|
|
default = "0.0.0.0";
|
|
|
|
type = str;
|
|
|
|
description = ''
|
|
|
|
Host address to bind HTTP server to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
default = 3000;
|
|
|
|
type = port;
|
|
|
|
description = ''
|
|
|
|
Port to serve HTTP pages on.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-12-24 17:52:39 +00:00
|
|
|
settings = mkOption {
|
2022-10-30 17:34:01 +00:00
|
|
|
default = null;
|
|
|
|
type = nullOr (submodule {
|
2024-04-19 16:37:19 +00:00
|
|
|
freeformType = settingsFormat.type;
|
2022-06-07 11:35:22 +00:00
|
|
|
options = {
|
|
|
|
schema_version = mkOption {
|
2024-04-19 16:37:19 +00:00
|
|
|
default = cfg.package.schema_version;
|
|
|
|
defaultText = literalExpression "cfg.package.schema_version";
|
2022-06-07 11:35:22 +00:00
|
|
|
type = int;
|
|
|
|
description = ''
|
|
|
|
Schema version for the configuration.
|
2024-04-19 16:37:19 +00:00
|
|
|
Defaults to the `schema_version` supplied by `cfg.package`.
|
2022-06-07 11:35:22 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2022-10-30 17:34:01 +00:00
|
|
|
});
|
2021-12-24 17:52:39 +00:00
|
|
|
description = ''
|
|
|
|
AdGuard Home configuration. Refer to
|
|
|
|
<https://github.com/AdguardTeam/AdGuardHome/wiki/Configuration#configuration-file>
|
|
|
|
for details on supported values.
|
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
On start and if {option}`mutableSettings` is `true`,
|
|
|
|
these options are merged into the configuration file on start, taking
|
|
|
|
precedence over configuration changes made on the web interface.
|
2022-10-30 17:34:01 +00:00
|
|
|
|
|
|
|
Set this to `null` (default) for a non-declarative configuration without any
|
|
|
|
Nix-supplied values.
|
2024-04-19 16:37:19 +00:00
|
|
|
Declarative configurations are supplied with a default `schema_version`, and `http.address`.
|
2022-08-30 00:30:04 +00:00
|
|
|
:::
|
2021-12-24 17:52:39 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-04-30 18:55:31 +00:00
|
|
|
extraArgs = mkOption {
|
|
|
|
default = [ ];
|
|
|
|
type = listOf str;
|
|
|
|
description = ''
|
|
|
|
Extra command line parameters to be passed to the adguardhome binary.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2022-01-12 18:19:00 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
2024-04-19 16:37:19 +00:00
|
|
|
assertion = cfg.settings != null
|
|
|
|
-> !(hasAttrByPath [ "bind_host" ] cfg.settings);
|
|
|
|
message = "AdGuard option `settings.bind_host' has been superseded by `services.adguardhome.host'";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = cfg.settings != null
|
|
|
|
-> !(hasAttrByPath [ "bind_port" ] cfg.settings);
|
2024-06-02 19:01:11 +00:00
|
|
|
message = "AdGuard option `settings.bind_port' has been superseded by `services.adguardhome.port'";
|
2024-04-19 16:37:19 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = settings != null -> cfg.mutableSettings
|
|
|
|
|| hasAttrByPath [ "dns" "bootstrap_dns" ] settings;
|
|
|
|
message = "AdGuard setting dns.bootstrap_dns needs to be configured for a minimal working configuration";
|
2022-01-12 18:19:00 +00:00
|
|
|
}
|
|
|
|
{
|
2024-04-19 16:37:19 +00:00
|
|
|
assertion = settings != null -> cfg.mutableSettings
|
|
|
|
|| hasAttrByPath [ "dns" "bootstrap_dns" ] settings
|
|
|
|
&& isList settings.dns.bootstrap_dns;
|
|
|
|
message = "AdGuard setting dns.bootstrap_dns needs to be a list";
|
2022-01-12 18:19:00 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-04-30 18:55:31 +00:00
|
|
|
systemd.services.adguardhome = {
|
|
|
|
description = "AdGuard Home: Network-level blocker";
|
2021-12-08 22:17:24 +00:00
|
|
|
after = [ "network.target" ];
|
2021-04-30 18:55:31 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
unitConfig = {
|
|
|
|
StartLimitIntervalSec = 5;
|
|
|
|
StartLimitBurst = 10;
|
|
|
|
};
|
2021-12-24 17:52:39 +00:00
|
|
|
|
2024-04-19 16:37:19 +00:00
|
|
|
preStart = optionalString (settings != null) ''
|
2021-12-24 17:52:39 +00:00
|
|
|
if [ -e "$STATE_DIRECTORY/AdGuardHome.yaml" ] \
|
|
|
|
&& [ "${toString cfg.mutableSettings}" = "1" ]; then
|
2024-04-30 20:47:52 +00:00
|
|
|
# First run a schema_version update on the existing configuration
|
|
|
|
# This ensures that both the new config and the existing one have the same schema_version
|
|
|
|
# Note: --check-config has the side effect of modifying the file at rest!
|
|
|
|
${lib.getExe cfg.package} -c "$STATE_DIRECTORY/AdGuardHome.yaml" --check-config
|
|
|
|
|
2021-12-24 17:52:39 +00:00
|
|
|
# Writing directly to AdGuardHome.yaml results in empty file
|
2024-04-30 20:47:52 +00:00
|
|
|
${lib.getExe pkgs.yaml-merge} "$STATE_DIRECTORY/AdGuardHome.yaml" "${configFile}" > "$STATE_DIRECTORY/AdGuardHome.yaml.tmp"
|
2021-12-24 17:52:39 +00:00
|
|
|
mv "$STATE_DIRECTORY/AdGuardHome.yaml.tmp" "$STATE_DIRECTORY/AdGuardHome.yaml"
|
|
|
|
else
|
|
|
|
cp --force "${configFile}" "$STATE_DIRECTORY/AdGuardHome.yaml"
|
|
|
|
chmod 600 "$STATE_DIRECTORY/AdGuardHome.yaml"
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
2021-04-30 18:55:31 +00:00
|
|
|
serviceConfig = {
|
|
|
|
DynamicUser = true;
|
2024-04-30 20:47:52 +00:00
|
|
|
ExecStart = "${lib.getExe cfg.package} ${args}";
|
2024-04-19 16:37:19 +00:00
|
|
|
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ]
|
|
|
|
++ optionals cfg.allowDHCP [ "CAP_NET_RAW" ];
|
2021-04-30 18:55:31 +00:00
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 10;
|
|
|
|
RuntimeDirectory = "AdGuardHome";
|
|
|
|
StateDirectory = "AdGuardHome";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-04-19 16:37:19 +00:00
|
|
|
networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];
|
2021-04-30 18:55:31 +00:00
|
|
|
};
|
|
|
|
}
|