2021-04-28 15:56:06 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.geoipupdate;
|
2022-03-28 14:05:26 +01:00
|
|
|
inherit (builtins) isAttrs isString isInt isList typeOf hashString;
|
2021-04-28 15:56:06 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
imports = [
|
|
|
|
(lib.mkRemovedOptionModule [ "services" "geoip-updater" ] "services.geoip-updater has been removed, use services.geoipupdate instead.")
|
|
|
|
];
|
|
|
|
|
|
|
|
options = {
|
|
|
|
services.geoipupdate = {
|
2022-08-28 20:18:44 +01:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc ''
|
2022-08-13 10:52:50 +01:00
|
|
|
periodic downloading of GeoIP databases using geoipupdate.
|
2022-08-28 20:18:44 +01:00
|
|
|
'');
|
2021-04-28 15:56:06 +01:00
|
|
|
|
|
|
|
interval = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "weekly";
|
2022-08-05 18:39:00 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-04-28 15:56:06 +01:00
|
|
|
Update the GeoIP databases at this time / interval.
|
|
|
|
The format is described in
|
2022-08-05 18:39:00 +01:00
|
|
|
{manpage}`systemd.time(7)`.
|
2021-04-28 15:56:06 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
settings = lib.mkOption {
|
2022-03-28 14:05:26 +01: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"; };
|
|
|
|
}
|
|
|
|
'';
|
2022-08-15 06:16:25 +01:00
|
|
|
description = lib.mdDoc ''
|
2022-08-13 10:52:50 +01:00
|
|
|
geoipupdate configuration options. See
|
2022-08-15 06:16:25 +01:00
|
|
|
<https://github.com/maxmind/geoipupdate/blob/main/doc/GeoIP.conf.md>
|
2021-04-28 15:56:06 +01:00
|
|
|
for a full list of available options.
|
2022-03-28 14:05:26 +01:00
|
|
|
|
|
|
|
Settings containing secret data should be set to an
|
|
|
|
attribute set containing the attribute
|
2022-08-15 06:16:25 +01:00
|
|
|
`_secret` - a string pointing to a file
|
2022-03-28 14:05:26 +01: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-15 06:16:25 +01:00
|
|
|
{file}`GeoIP.conf` file, the
|
|
|
|
`ProxyUserPassword` key will be set to the
|
2022-03-28 14:05:26 +01:00
|
|
|
contents of the
|
2022-08-15 06:16:25 +01:00
|
|
|
{file}`/run/keys/proxy_pass` file.
|
2021-04-28 15:56:06 +01: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;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-04-28 15:56:06 +01:00
|
|
|
Your MaxMind account ID.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
EditionIDs = lib.mkOption {
|
|
|
|
type = with lib.types; listOf (either str int);
|
|
|
|
example = [
|
|
|
|
"GeoLite2-ASN"
|
|
|
|
"GeoLite2-City"
|
|
|
|
"GeoLite2-Country"
|
|
|
|
];
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-04-28 15:56:06 +01:00
|
|
|
List of database edition IDs. This includes new string
|
2022-07-28 22:19:15 +01:00
|
|
|
IDs like `GeoIP2-City` and old
|
|
|
|
numeric IDs like `106`.
|
2021-04-28 15:56:06 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
LicenseKey = lib.mkOption {
|
2022-03-28 14:05:26 +01:00
|
|
|
type = with lib.types; either path (attrsOf path);
|
2022-08-15 06:16:25 +01:00
|
|
|
description = lib.mdDoc ''
|
2022-08-13 10:52:50 +01:00
|
|
|
A file containing the MaxMind license key.
|
2022-03-28 14:05:26 +01:00
|
|
|
|
|
|
|
Always handled as a secret whether the value is
|
2022-08-15 06:16:25 +01:00
|
|
|
wrapped in a `{ _secret = ...; }`
|
|
|
|
attrset or not (refer to [](#opt-services.geoipupdate.settings) for
|
2022-03-28 14:05:26 +01:00
|
|
|
details).
|
2021-04-28 15:56:06 +01:00
|
|
|
'';
|
2022-03-28 14:05:26 +01:00
|
|
|
apply = x: if isAttrs x then x else { _secret = x; };
|
2021-04-28 15:56:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
DatabaseDirectory = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
|
|
|
default = "/var/lib/GeoIP";
|
|
|
|
example = "/run/GeoIP";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-04-28 15:56:06 +01:00
|
|
|
The directory to store the database files in. The
|
|
|
|
directory will be automatically created, the owner
|
2022-07-28 22:19:15 +01:00
|
|
|
changed to `geoip` and permissions
|
2021-04-28 15:56:06 +01: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-06-07 12:01:49 +01:00
|
|
|
systemd.services.geoipupdate-create-db-dir = {
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
script = ''
|
2022-03-28 14:04:31 +01:00
|
|
|
set -o errexit -o pipefail -o nounset -o errtrace
|
|
|
|
shopt -s inherit_errexit
|
|
|
|
|
2021-06-07 12:01:49 +01:00
|
|
|
mkdir -p ${cfg.settings.DatabaseDirectory}
|
|
|
|
chmod 0755 ${cfg.settings.DatabaseDirectory}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-04-28 15:56:06 +01:00
|
|
|
systemd.services.geoipupdate = {
|
|
|
|
description = "GeoIP Updater";
|
2021-06-07 12:01:49 +01:00
|
|
|
requires = [ "geoipupdate-create-db-dir.service" ];
|
|
|
|
after = [
|
|
|
|
"geoipupdate-create-db-dir.service"
|
|
|
|
"network-online.target"
|
|
|
|
"nss-lookup.target"
|
|
|
|
];
|
2022-03-28 14:05:26 +01:00
|
|
|
path = [ pkgs.replace-secret ];
|
2021-04-28 15:56:06 +01:00
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
startAt = cfg.interval;
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStartPre =
|
|
|
|
let
|
2022-03-28 14:05:26 +01:00
|
|
|
isSecret = v: isAttrs v && v ? _secret && isString v._secret;
|
2021-04-28 15:56:06 +01:00
|
|
|
geoipupdateKeyValue = lib.generators.toKeyValue {
|
|
|
|
mkKeyValue = lib.flip lib.generators.mkKeyValueDefault " " rec {
|
2022-03-28 14:05:26 +01:00
|
|
|
mkValueString = v:
|
2021-04-28 15:56:06 +01: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-03-28 14:05:26 +01:00
|
|
|
else if isSecret v then hashString "sha256" v._secret
|
2021-04-28 15:56:06 +01:00
|
|
|
else throw "unsupported type ${typeOf v}: ${(lib.generators.toPretty {}) v}";
|
|
|
|
};
|
|
|
|
};
|
2022-03-28 14:05:26 +01: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-04-28 15:56:06 +01:00
|
|
|
|
2021-06-07 08:29:21 +01:00
|
|
|
geoipupdateConf = pkgs.writeText "geoipupdate.conf" (geoipupdateKeyValue cfg.settings);
|
2021-04-28 15:56:06 +01:00
|
|
|
|
|
|
|
script = ''
|
2022-03-28 14:04:31 +01:00
|
|
|
set -o errexit -o pipefail -o nounset -o errtrace
|
|
|
|
shopt -s inherit_errexit
|
|
|
|
|
2021-04-28 15:56:06 +01:00
|
|
|
chown geoip "${cfg.settings.DatabaseDirectory}"
|
|
|
|
|
|
|
|
cp ${geoipupdateConf} /run/geoipupdate/GeoIP.conf
|
2022-03-28 14:05:26 +01:00
|
|
|
${secretReplacements}
|
2021-04-28 15:56:06 +01: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";
|
|
|
|
RuntimeDirectoryMode = 0700;
|
2021-06-07 12:14:44 +01: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-25 09:45:49 +01:00
|
|
|
SystemCallFilter = [ "@system-service" "~@privileged" ];
|
2021-06-07 12:14:44 +01:00
|
|
|
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
LockPersonality = true;
|
|
|
|
SystemCallArchitectures = "native";
|
2021-04-28 15:56:06 +01:00
|
|
|
};
|
|
|
|
};
|
2021-06-07 12:08:59 +01:00
|
|
|
|
|
|
|
systemd.timers.geoipupdate-initial-run = {
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
unitConfig.ConditionPathExists = "!${cfg.settings.DatabaseDirectory}";
|
|
|
|
timerConfig = {
|
|
|
|
Unit = "geoipupdate.service";
|
|
|
|
OnActiveSec = 0;
|
|
|
|
};
|
|
|
|
};
|
2021-04-28 15:56:06 +01:00
|
|
|
};
|
2021-06-07 08:44:05 +01:00
|
|
|
|
|
|
|
meta.maintainers = [ lib.maintainers.talyz ];
|
2021-04-28 15:56:06 +01:00
|
|
|
}
|