2021-05-03 20:48:10 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.adguardhome;
|
|
|
|
|
|
|
|
args = concatStringsSep " " ([
|
|
|
|
"--no-check-update"
|
|
|
|
"--pidfile /run/AdGuardHome/AdGuardHome.pid"
|
|
|
|
"--work-dir /var/lib/AdGuardHome/"
|
|
|
|
"--config /var/lib/AdGuardHome/AdGuardHome.yaml"
|
|
|
|
] ++ cfg.extraArgs);
|
|
|
|
|
2022-01-07 04:07:37 +00:00
|
|
|
baseConfig = {
|
|
|
|
bind_host = cfg.host;
|
|
|
|
bind_port = cfg.port;
|
|
|
|
};
|
|
|
|
|
|
|
|
configFile = pkgs.writeTextFile {
|
|
|
|
name = "AdGuardHome.yaml";
|
|
|
|
text = builtins.toJSON (recursiveUpdate cfg.settings baseConfig);
|
|
|
|
checkPhase = "${pkgs.adguardhome}/bin/adguardhome -c $out --check-config";
|
|
|
|
};
|
|
|
|
|
|
|
|
in {
|
2021-05-03 20:48:10 +00:00
|
|
|
options.services.adguardhome = with types; {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "AdGuard Home network-wide ad blocker");
|
2021-05-03 20:48:10 +00:00
|
|
|
|
|
|
|
host = mkOption {
|
|
|
|
default = "0.0.0.0";
|
|
|
|
type = str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-05-03 20:48:10 +00:00
|
|
|
Host address to bind HTTP server to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
default = 3000;
|
|
|
|
type = port;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-05-03 20:48:10 +00:00
|
|
|
Port to serve HTTP pages on.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-05-03 20:48:10 +00:00
|
|
|
Open ports in the firewall for the AdGuard Home web interface. Does not
|
|
|
|
open the port needed to access the DNS resolver.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-01-07 04:07:37 +00:00
|
|
|
mutableSettings = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-01-07 04:07:37 +00:00
|
|
|
Allow changes made on the AdGuard Home web interface to persist between
|
|
|
|
service restarts.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
type = (pkgs.formats.yaml { }).type;
|
|
|
|
default = { };
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-01-07 04:07:37 +00:00
|
|
|
AdGuard Home configuration. Refer to
|
2022-09-09 14:08:57 +00:00
|
|
|
<https://github.com/AdguardTeam/AdGuardHome/wiki/Configuration#configuration-file>
|
2022-01-07 04:07:37 +00:00
|
|
|
for details on supported values.
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
::: {.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-01-07 04:07:37 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-05-03 20:48:10 +00:00
|
|
|
extraArgs = mkOption {
|
|
|
|
default = [ ];
|
|
|
|
type = listOf str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-05-03 20:48:10 +00:00
|
|
|
Extra command line parameters to be passed to the adguardhome binary.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2022-01-19 23:45:15 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.settings != { }
|
|
|
|
-> (hasAttrByPath [ "dns" "bind_host" ] cfg.settings)
|
|
|
|
|| (hasAttrByPath [ "dns" "bind_hosts" ] cfg.settings);
|
|
|
|
message =
|
|
|
|
"AdGuard setting dns.bind_host or dns.bind_hosts needs to be configured for a minimal working configuration";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = cfg.settings != { }
|
|
|
|
-> hasAttrByPath [ "dns" "bootstrap_dns" ] cfg.settings;
|
|
|
|
message =
|
|
|
|
"AdGuard setting dns.bootstrap_dns needs to be configured for a minimal working configuration";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-05-03 20:48:10 +00:00
|
|
|
systemd.services.adguardhome = {
|
|
|
|
description = "AdGuard Home: Network-level blocker";
|
2021-12-19 01:06:50 +00:00
|
|
|
after = [ "network.target" ];
|
2021-05-03 20:48:10 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
unitConfig = {
|
|
|
|
StartLimitIntervalSec = 5;
|
|
|
|
StartLimitBurst = 10;
|
|
|
|
};
|
2022-01-07 04:07:37 +00:00
|
|
|
|
2022-01-19 23:45:15 +00:00
|
|
|
preStart = optionalString (cfg.settings != { }) ''
|
2022-01-07 04:07:37 +00:00
|
|
|
if [ -e "$STATE_DIRECTORY/AdGuardHome.yaml" ] \
|
|
|
|
&& [ "${toString cfg.mutableSettings}" = "1" ]; then
|
|
|
|
# Writing directly to AdGuardHome.yaml results in empty file
|
|
|
|
${pkgs.yaml-merge}/bin/yaml-merge "$STATE_DIRECTORY/AdGuardHome.yaml" "${configFile}" > "$STATE_DIRECTORY/AdGuardHome.yaml.tmp"
|
|
|
|
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-05-03 20:48:10 +00:00
|
|
|
serviceConfig = {
|
|
|
|
DynamicUser = true;
|
|
|
|
ExecStart = "${pkgs.adguardhome}/bin/adguardhome ${args}";
|
|
|
|
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 10;
|
|
|
|
RuntimeDirectory = "AdGuardHome";
|
|
|
|
StateDirectory = "AdGuardHome";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];
|
|
|
|
};
|
|
|
|
}
|