photoncloud-monorepo/nix/modules/flashdns.nix

148 lines
4.2 KiB
Nix

{ config, lib, pkgs, ... }:
let
cfg = config.services.flashdns;
tomlFormat = pkgs.formats.toml { };
generatedConfig = {
grpc_addr = "0.0.0.0:${toString cfg.port}";
dns_addr = "0.0.0.0:${toString cfg.dnsPort}";
log_level = "info";
metadata_backend = cfg.metadataBackend;
single_node = cfg.singleNode;
auth = {
iam_server_addr =
if cfg.iamAddr != null
then cfg.iamAddr
else "127.0.0.1:50080";
};
}
// lib.optionalAttrs (cfg.chainfireAddr != null) {
chainfire_endpoint = "http://${cfg.chainfireAddr}";
}
// lib.optionalAttrs (cfg.flaredbAddr != null) {
flaredb_endpoint = cfg.flaredbAddr;
}
// lib.optionalAttrs (cfg.databaseUrl != null) {
metadata_database_url = cfg.databaseUrl;
};
configFile = tomlFormat.generate "flashdns.toml" generatedConfig;
in
{
options.services.flashdns = {
enable = lib.mkEnableOption "flashdns service";
port = lib.mkOption {
type = lib.types.port;
default = 50084;
description = "Port for flashdns gRPC API";
};
dnsPort = lib.mkOption {
type = lib.types.port;
default = 5353;
description = "Port for flashdns DNS service (use 53 for production)";
};
chainfireAddr = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "ChainFire endpoint address (host:port) for cluster coordination only";
example = "10.0.0.1:2379";
};
flaredbAddr = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "FlareDB endpoint address (host:port) for metadata/user data";
example = "10.0.0.1:2479";
};
iamAddr = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "IAM service endpoint address (host:port)";
example = "10.0.0.1:50080";
};
metadataBackend = lib.mkOption {
type = lib.types.enum [ "flaredb" "postgres" "sqlite" ];
default = "flaredb";
description = "Metadata backend for FlashDNS.";
};
databaseUrl = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "SQL database URL for metadata (required when metadataBackend is postgres/sqlite).";
example = "postgres://flashdns:secret@10.0.0.10:5432/flashdns";
};
singleNode = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Enable single-node mode (required when metadata backend is SQLite)";
};
dataDir = lib.mkOption {
type = lib.types.path;
default = "/var/lib/flashdns";
description = "Data directory for flashdns";
};
settings = lib.mkOption {
type = lib.types.attrs;
default = {};
description = "Additional configuration settings";
};
package = lib.mkOption {
type = lib.types.package;
default = pkgs.flashdns-server or (throw "flashdns-server package not found");
description = "Package to use for flashdns";
};
};
config = lib.mkIf cfg.enable {
# Create system user
users.users.flashdns = {
isSystemUser = true;
group = "flashdns";
description = "FlashDNS service user";
home = cfg.dataDir;
};
users.groups.flashdns = {};
# Create systemd service
systemd.services.flashdns = {
description = "FlashDNS Distributed DNS Service";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" "prismnet.service" "flaredb.service" "chainfire.service" ];
wants = [ "prismnet.service" "flaredb.service" "chainfire.service" ];
serviceConfig = {
Type = "simple";
User = "flashdns";
Group = "flashdns";
Restart = "on-failure";
RestartSec = "10s";
# State directory management
StateDirectory = "flashdns";
StateDirectoryMode = "0750";
# Security hardening
NoNewPrivileges = true;
PrivateTmp = true;
ProtectSystem = "strict";
ProtectHome = true;
ReadWritePaths = [ cfg.dataDir ];
# DNS requires binding to privileged port 53
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
ExecStart = "${cfg.package}/bin/flashdns-server --config ${configFile}";
};
};
};
}