2021-08-05 21:33:18 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
2022-06-16 17:23:12 +00:00
|
|
|
let
|
|
|
|
cfg = config.services.libreddit;
|
2021-08-05 21:33:18 +00:00
|
|
|
|
2022-06-16 17:23:12 +00:00
|
|
|
args = concatStringsSep " " ([
|
|
|
|
"--port ${toString cfg.port}"
|
|
|
|
"--address ${cfg.address}"
|
|
|
|
]);
|
2021-08-05 21:33:18 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.libreddit = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "Private front-end for Reddit");
|
2021-08-05 21:33:18 +00:00
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
package = mkPackageOption pkgs "libreddit" { };
|
2022-12-17 10:02:37 +00:00
|
|
|
|
2021-08-05 21:33:18 +00:00
|
|
|
address = mkOption {
|
|
|
|
default = "0.0.0.0";
|
|
|
|
example = "127.0.0.1";
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The address to listen on";
|
2021-08-05 21:33:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
default = 8080;
|
|
|
|
example = 8000;
|
|
|
|
type = types.port;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The port to listen on";
|
2021-08-05 21:33:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Open ports in the firewall for the libreddit web interface";
|
2021-08-05 21:33:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.libreddit = {
|
|
|
|
description = "Private front-end for Reddit";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
DynamicUser = true;
|
2022-12-17 10:02:37 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/libreddit ${args}";
|
2021-08-05 21:33:18 +00:00
|
|
|
AmbientCapabilities = lib.mkIf (cfg.port < 1024) [ "CAP_NET_BIND_SERVICE" ];
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "2s";
|
2022-06-16 17:23:12 +00:00
|
|
|
# Hardening
|
|
|
|
CapabilityBoundingSet = if (cfg.port < 1024) then [ "CAP_NET_BIND_SERVICE" ] else [ "" ];
|
|
|
|
DeviceAllow = [ "" ];
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
# A private user cannot have process capabilities on the host's user
|
|
|
|
# namespace and thus CAP_NET_BIND_SERVICE has no effect.
|
|
|
|
PrivateUsers = (cfg.port >= 1024);
|
|
|
|
ProcSubset = "pid";
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
|
|
|
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
SystemCallFilter = [ "@system-service" "~@privileged" "~@resources" ];
|
|
|
|
UMask = "0077";
|
2021-08-05 21:33:18 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
networking.firewall = mkIf cfg.openFirewall {
|
|
|
|
allowedTCPPorts = [ cfg.port ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|