mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-11 12:15:34 +03:00

Add a module for pihole-ftl, which allows declaratively defining the pihole.toml config file. Also provide options for adlists to use, which can be added through the pihole script (packaged as "pihole"). Other state such as clients and groups require complex database operations, which is normally performed by the pihole webapp (packaged as "pihole-web"). Extend the dnsmasq module to avoid duplication, since pihole-ftl is a soft-fork of dnsmasq which maintains compatibility. Provide the pihole script in `environment.systemPackages` so pihole-ftl can be easily administrated.
82 lines
1.8 KiB
Nix
82 lines
1.8 KiB
Nix
{
|
|
cfg,
|
|
config,
|
|
lib,
|
|
pkgs,
|
|
}:
|
|
|
|
let
|
|
pihole = pkgs.pihole;
|
|
makePayload =
|
|
list:
|
|
builtins.toJSON {
|
|
inherit (list) type enabled;
|
|
address = list.url;
|
|
comment = list.description;
|
|
};
|
|
payloads = map makePayload cfg.lists;
|
|
in
|
|
''
|
|
# Can't use -u (unset) because api.sh uses API_URL before it is set
|
|
set -eo pipefail
|
|
pihole="${lib.getExe pihole}"
|
|
jq="${lib.getExe pkgs.jq}"
|
|
|
|
# If the database doesn't exist, it needs to be created with gravity.sh
|
|
if [ ! -f '${cfg.stateDirectory}'/gravity.db ]; then
|
|
$pihole -g
|
|
# Send SIGRTMIN to FTL, which makes it reload the database, opening the newly created one
|
|
${pkgs.procps}/bin/kill -s SIGRTMIN $(systemctl show --property MainPID --value ${config.systemd.services.pihole-ftl.name})
|
|
fi
|
|
|
|
source ${pihole}/usr/share/pihole/advanced/Scripts/api.sh
|
|
source ${pihole}/usr/share/pihole/advanced/Scripts/utils.sh
|
|
|
|
any_failed=0
|
|
|
|
addList() {
|
|
local payload="$1"
|
|
|
|
echo "Adding list: $payload"
|
|
local result=$(PostFTLData "lists" "$payload")
|
|
|
|
local error="$($jq '.error' <<< "$result")"
|
|
if [[ "$error" != "null" ]]; then
|
|
echo "Error: $error"
|
|
any_failed=1
|
|
return
|
|
fi
|
|
|
|
id="$($jq '.lists.[].id?' <<< "$result")"
|
|
if [[ "$id" == "null" ]]; then
|
|
any_failed=1
|
|
error="$($jq '.processed.errors.[].error' <<< "$result")"
|
|
echo "Error: $error"
|
|
return
|
|
fi
|
|
|
|
echo "Added list ID $id: $result"
|
|
}
|
|
|
|
for i in 1 2 3; do
|
|
(TestAPIAvailability) && break
|
|
echo "Retrying API shortly..."
|
|
${pkgs.coreutils}/bin/sleep .5s
|
|
done;
|
|
|
|
LoginAPI
|
|
|
|
${builtins.concatStringsSep "\n" (
|
|
map (
|
|
payload:
|
|
lib.pipe payload [
|
|
lib.strings.escapeShellArg
|
|
(payload: "addList ${payload}")
|
|
]
|
|
) payloads
|
|
)}
|
|
|
|
# Run gravity.sh to load any new lists
|
|
$pihole -g
|
|
exit $any_failed
|
|
''
|