nixpkgs/nixos/modules/services/misc/redlib.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
2.8 KiB
Nix
Raw Normal View History

{
config,
lib,
pkgs,
...
}:
2021-04-19 01:05:25 +02:00
let
inherit (lib)
concatStringsSep
mkEnableOption
mkIf
mkOption
mkPackageOption
mkRenamedOptionModule
types
;
cfg = config.services.redlib;
2021-04-19 01:05:25 +02:00
args = concatStringsSep " " ([
"--port ${toString cfg.port}"
"--address ${cfg.address}"
]);
2021-04-19 01:05:25 +02:00
in
{
imports = [
(mkRenamedOptionModule
[
"services"
"libreddit"
]
[
"services"
"redlib"
]
)
];
2021-04-19 01:05:25 +02:00
options = {
services.redlib = {
2021-04-19 01:05:25 +02:00
enable = mkEnableOption "Private front-end for Reddit";
package = mkPackageOption pkgs "redlib" { };
2022-12-04 14:00:17 +01:00
2021-04-19 01:05:25 +02:00
address = mkOption {
default = "0.0.0.0";
example = "127.0.0.1";
type = types.str;
2021-04-19 01:05:25 +02:00
description = "The address to listen on";
};
port = mkOption {
default = 8080;
example = 8000;
type = types.port;
description = "The port to listen on";
};
openFirewall = mkOption {
type = types.bool;
default = false;
description = "Open ports in the firewall for the redlib web interface";
2021-04-19 01:05:25 +02:00
};
};
};
config = mkIf cfg.enable {
systemd.services.redlib = {
description = "Private front-end for Reddit";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
DynamicUser = true;
ExecStart = "${lib.getExe cfg.package} ${args}";
AmbientCapabilities = lib.mkIf (cfg.port < 1024) [ "CAP_NET_BIND_SERVICE" ];
Restart = "on-failure";
RestartSec = "2s";
# 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-04-19 01:05:25 +02:00
};
networking.firewall = mkIf cfg.openFirewall {
allowedTCPPorts = [ cfg.port ];
};
};
meta = {
maintainers = with lib.maintainers; [ Guanran928 ];
};
2021-04-19 01:05:25 +02:00
}