2021-06-04 09:07:49 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.geoipupdate;
|
2022-07-14 12:49:19 +00:00
|
|
|
inherit (builtins) isAttrs isString isInt isList typeOf hashString;
|
2021-06-04 09:07:49 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
imports = [
|
|
|
|
(lib.mkRemovedOptionModule [ "services" "geoip-updater" ] "services.geoip-updater has been removed, use services.geoipupdate instead.")
|
|
|
|
];
|
|
|
|
|
|
|
|
options = {
|
|
|
|
services.geoipupdate = {
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = lib.mkEnableOption ''
|
|
|
|
periodic downloading of GeoIP databases using geoipupdate
|
|
|
|
'';
|
2021-06-04 09:07:49 +00:00
|
|
|
|
|
|
|
interval = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "weekly";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2021-06-04 09:07:49 +00:00
|
|
|
Update the GeoIP databases at this time / interval.
|
|
|
|
The format is described in
|
2022-08-21 13:32:41 +00:00
|
|
|
{manpage}`systemd.time(7)`.
|
2021-06-04 09:07:49 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
settings = lib.mkOption {
|
2022-07-14 12:49:19 +00:00
|
|
|
example = lib.literalExpression ''
|
|
|
|
{
|
|
|
|
AccountID = 200001;
|
|
|
|
DatabaseDirectory = "/var/lib/GeoIP";
|
|
|
|
LicenseKey = { _secret = "/run/keys/maxmind_license_key"; };
|
|
|
|
Proxy = "10.0.0.10:8888";
|
|
|
|
ProxyUserPassword = { _secret = "/run/keys/proxy_pass"; };
|
|
|
|
}
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-08-21 13:32:41 +00:00
|
|
|
geoipupdate configuration options. See
|
|
|
|
<https://github.com/maxmind/geoipupdate/blob/main/doc/GeoIP.conf.md>
|
2021-06-04 09:07:49 +00:00
|
|
|
for a full list of available options.
|
2022-07-14 12:49:19 +00:00
|
|
|
|
|
|
|
Settings containing secret data should be set to an
|
|
|
|
attribute set containing the attribute
|
2022-08-21 13:32:41 +00:00
|
|
|
`_secret` - a string pointing to a file
|
2022-07-14 12:49:19 +00:00
|
|
|
containing the value the option should be set to. See the
|
|
|
|
example to get a better picture of this: in the resulting
|
2022-08-21 13:32:41 +00:00
|
|
|
{file}`GeoIP.conf` file, the
|
|
|
|
`ProxyUserPassword` key will be set to the
|
2022-07-14 12:49:19 +00:00
|
|
|
contents of the
|
2022-08-21 13:32:41 +00:00
|
|
|
{file}`/run/keys/proxy_pass` file.
|
2021-06-04 09:07:49 +00:00
|
|
|
'';
|
|
|
|
type = lib.types.submodule {
|
|
|
|
freeformType =
|
|
|
|
with lib.types;
|
|
|
|
let
|
|
|
|
type = oneOf [str int bool];
|
|
|
|
in
|
|
|
|
attrsOf (either type (listOf type));
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
AccountID = lib.mkOption {
|
|
|
|
type = lib.types.int;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2021-06-04 09:07:49 +00:00
|
|
|
Your MaxMind account ID.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
EditionIDs = lib.mkOption {
|
|
|
|
type = with lib.types; listOf (either str int);
|
|
|
|
example = [
|
|
|
|
"GeoLite2-ASN"
|
|
|
|
"GeoLite2-City"
|
|
|
|
"GeoLite2-Country"
|
|
|
|
];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2021-06-04 09:07:49 +00:00
|
|
|
List of database edition IDs. This includes new string
|
2022-08-12 12:06:08 +00:00
|
|
|
IDs like `GeoIP2-City` and old
|
|
|
|
numeric IDs like `106`.
|
2021-06-04 09:07:49 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
LicenseKey = lib.mkOption {
|
2022-07-14 12:49:19 +00:00
|
|
|
type = with lib.types; either path (attrsOf path);
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-08-21 13:32:41 +00:00
|
|
|
A file containing the MaxMind license key.
|
2022-07-14 12:49:19 +00:00
|
|
|
|
|
|
|
Always handled as a secret whether the value is
|
2022-08-21 13:32:41 +00:00
|
|
|
wrapped in a `{ _secret = ...; }`
|
|
|
|
attrset or not (refer to [](#opt-services.geoipupdate.settings) for
|
2022-07-14 12:49:19 +00:00
|
|
|
details).
|
2021-06-04 09:07:49 +00:00
|
|
|
'';
|
2022-07-14 12:49:19 +00:00
|
|
|
apply = x: if isAttrs x then x else { _secret = x; };
|
2021-06-04 09:07:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DatabaseDirectory = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
|
|
|
default = "/var/lib/GeoIP";
|
|
|
|
example = "/run/GeoIP";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2021-06-04 09:07:49 +00:00
|
|
|
The directory to store the database files in. The
|
|
|
|
directory will be automatically created, the owner
|
2022-08-12 12:06:08 +00:00
|
|
|
changed to `geoip` and permissions
|
2021-06-04 09:07:49 +00:00
|
|
|
set to world readable. This applies if the directory
|
|
|
|
already exists as well, so don't use a directory with
|
|
|
|
sensitive contents.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
|
|
|
|
services.geoipupdate.settings = {
|
|
|
|
LockFile = "/run/geoipupdate/.lock";
|
|
|
|
};
|
|
|
|
|
2021-07-03 03:11:41 +00:00
|
|
|
systemd.services.geoipupdate-create-db-dir = {
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
script = ''
|
2022-07-14 12:49:19 +00:00
|
|
|
set -o errexit -o pipefail -o nounset -o errtrace
|
|
|
|
shopt -s inherit_errexit
|
|
|
|
|
2021-07-03 03:11:41 +00:00
|
|
|
mkdir -p ${cfg.settings.DatabaseDirectory}
|
|
|
|
chmod 0755 ${cfg.settings.DatabaseDirectory}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-06-04 09:07:49 +00:00
|
|
|
systemd.services.geoipupdate = {
|
|
|
|
description = "GeoIP Updater";
|
2021-07-03 03:11:41 +00:00
|
|
|
requires = [ "geoipupdate-create-db-dir.service" ];
|
|
|
|
after = [
|
|
|
|
"geoipupdate-create-db-dir.service"
|
|
|
|
"network-online.target"
|
|
|
|
"nss-lookup.target"
|
|
|
|
];
|
2022-07-14 12:49:19 +00:00
|
|
|
path = [ pkgs.replace-secret ];
|
2021-06-04 09:07:49 +00:00
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
startAt = cfg.interval;
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStartPre =
|
|
|
|
let
|
2022-07-14 12:49:19 +00:00
|
|
|
isSecret = v: isAttrs v && v ? _secret && isString v._secret;
|
2021-06-04 09:07:49 +00:00
|
|
|
geoipupdateKeyValue = lib.generators.toKeyValue {
|
|
|
|
mkKeyValue = lib.flip lib.generators.mkKeyValueDefault " " rec {
|
2022-07-14 12:49:19 +00:00
|
|
|
mkValueString = v:
|
2021-06-04 09:07:49 +00:00
|
|
|
if isInt v then toString v
|
|
|
|
else if isString v then v
|
|
|
|
else if true == v then "1"
|
|
|
|
else if false == v then "0"
|
|
|
|
else if isList v then lib.concatMapStringsSep " " mkValueString v
|
2022-07-14 12:49:19 +00:00
|
|
|
else if isSecret v then hashString "sha256" v._secret
|
2021-06-04 09:07:49 +00:00
|
|
|
else throw "unsupported type ${typeOf v}: ${(lib.generators.toPretty {}) v}";
|
|
|
|
};
|
|
|
|
};
|
2022-07-14 12:49:19 +00:00
|
|
|
secretPaths = lib.catAttrs "_secret" (lib.collect isSecret cfg.settings);
|
|
|
|
mkSecretReplacement = file: ''
|
|
|
|
replace-secret ${lib.escapeShellArgs [ (hashString "sha256" file) file "/run/geoipupdate/GeoIP.conf" ]}
|
|
|
|
'';
|
|
|
|
secretReplacements = lib.concatMapStrings mkSecretReplacement secretPaths;
|
2021-06-04 09:07:49 +00:00
|
|
|
|
2021-07-03 03:11:41 +00:00
|
|
|
geoipupdateConf = pkgs.writeText "geoipupdate.conf" (geoipupdateKeyValue cfg.settings);
|
2021-06-04 09:07:49 +00:00
|
|
|
|
|
|
|
script = ''
|
2022-07-14 12:49:19 +00:00
|
|
|
set -o errexit -o pipefail -o nounset -o errtrace
|
|
|
|
shopt -s inherit_errexit
|
|
|
|
|
2021-06-04 09:07:49 +00:00
|
|
|
chown geoip "${cfg.settings.DatabaseDirectory}"
|
|
|
|
|
|
|
|
cp ${geoipupdateConf} /run/geoipupdate/GeoIP.conf
|
2022-07-14 12:49:19 +00:00
|
|
|
${secretReplacements}
|
2021-06-04 09:07:49 +00:00
|
|
|
'';
|
|
|
|
in
|
|
|
|
"+${pkgs.writeShellScript "start-pre-full-privileges" script}";
|
|
|
|
ExecStart = "${pkgs.geoipupdate}/bin/geoipupdate -f /run/geoipupdate/GeoIP.conf";
|
|
|
|
User = "geoip";
|
|
|
|
DynamicUser = true;
|
|
|
|
ReadWritePaths = cfg.settings.DatabaseDirectory;
|
|
|
|
RuntimeDirectory = "geoipupdate";
|
2022-10-30 15:09:59 +00:00
|
|
|
RuntimeDirectoryMode = "0700";
|
2021-07-03 03:11:41 +00:00
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateMounts = true;
|
|
|
|
PrivateUsers = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
|
|
|
ProcSubset = "pid";
|
2022-10-30 15:09:59 +00:00
|
|
|
SystemCallFilter = [ "@system-service" "~@privileged" ];
|
2021-07-03 03:11:41 +00:00
|
|
|
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
LockPersonality = true;
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.timers.geoipupdate-initial-run = {
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
unitConfig.ConditionPathExists = "!${cfg.settings.DatabaseDirectory}";
|
|
|
|
timerConfig = {
|
|
|
|
Unit = "geoipupdate.service";
|
|
|
|
OnActiveSec = 0;
|
2021-06-04 09:07:49 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2021-07-03 03:11:41 +00:00
|
|
|
|
|
|
|
meta.maintainers = [ lib.maintainers.talyz ];
|
2021-06-04 09:07:49 +00:00
|
|
|
}
|