{nixos,nixosTests}/redlib: format with nixfmt

This commit is contained in:
Guanran Wang 2024-10-01 17:39:13 +08:00
parent d78d09350a
commit e286b91ebc
No known key found for this signature in database
GPG Key ID: 91F97D9ED12639CF
2 changed files with 90 additions and 57 deletions

View File

@ -1,8 +1,21 @@
{ config, lib, pkgs, ... }: {
config,
with lib; lib,
pkgs,
...
}:
let let
inherit (lib)
concatStringsSep
mkEnableOption
mkIf
mkOption
mkPackageOption
mkRenamedOptionModule
types
;
cfg = config.services.redlib; cfg = config.services.redlib;
args = concatStringsSep " " ([ args = concatStringsSep " " ([
@ -12,7 +25,16 @@ let
in in
{ {
imports = [ imports = [
(mkRenamedOptionModule [ "services" "libreddit" ] [ "services" "redlib" ]) (mkRenamedOptionModule
[
"services"
"libreddit"
]
[
"services"
"redlib"
]
)
]; ];
options = { options = {
@ -24,7 +46,7 @@ in
address = mkOption { address = mkOption {
default = "0.0.0.0"; default = "0.0.0.0";
example = "127.0.0.1"; example = "127.0.0.1";
type = types.str; type = types.str;
description = "The address to listen on"; description = "The address to listen on";
}; };
@ -46,41 +68,49 @@ in
config = mkIf cfg.enable { config = mkIf cfg.enable {
systemd.services.redlib = { systemd.services.redlib = {
description = "Private front-end for Reddit"; description = "Private front-end for Reddit";
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
after = [ "network.target" ]; after = [ "network.target" ];
serviceConfig = { serviceConfig = {
DynamicUser = true; DynamicUser = true;
ExecStart = "${lib.getExe cfg.package} ${args}"; ExecStart = "${lib.getExe cfg.package} ${args}";
AmbientCapabilities = lib.mkIf (cfg.port < 1024) [ "CAP_NET_BIND_SERVICE" ]; AmbientCapabilities = lib.mkIf (cfg.port < 1024) [ "CAP_NET_BIND_SERVICE" ];
Restart = "on-failure"; Restart = "on-failure";
RestartSec = "2s"; RestartSec = "2s";
# Hardening
CapabilityBoundingSet = if (cfg.port < 1024) then [ "CAP_NET_BIND_SERVICE" ] else [ "" ]; # Hardening
DeviceAllow = [ "" ]; CapabilityBoundingSet = if (cfg.port < 1024) then [ "CAP_NET_BIND_SERVICE" ] else [ "" ];
LockPersonality = true; DeviceAllow = [ "" ];
MemoryDenyWriteExecute = true; LockPersonality = true;
PrivateDevices = true; MemoryDenyWriteExecute = true;
# A private user cannot have process capabilities on the host's user PrivateDevices = true;
# namespace and thus CAP_NET_BIND_SERVICE has no effect. # A private user cannot have process capabilities on the host's user
PrivateUsers = (cfg.port >= 1024); # namespace and thus CAP_NET_BIND_SERVICE has no effect.
ProcSubset = "pid"; PrivateUsers = (cfg.port >= 1024);
ProtectClock = true; ProcSubset = "pid";
ProtectControlGroups = true; ProtectClock = true;
ProtectHome = true; ProtectControlGroups = true;
ProtectHostname = true; ProtectHome = true;
ProtectKernelLogs = true; ProtectHostname = true;
ProtectKernelModules = true; ProtectKernelLogs = true;
ProtectKernelTunables = true; ProtectKernelModules = true;
ProtectProc = "invisible"; ProtectKernelTunables = true;
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ]; ProtectProc = "invisible";
RestrictNamespaces = true; RestrictAddressFamilies = [
RestrictRealtime = true; "AF_INET"
RestrictSUIDSGID = true; "AF_INET6"
SystemCallArchitectures = "native"; ];
SystemCallFilter = [ "@system-service" "~@privileged" "~@resources" ]; RestrictNamespaces = true;
UMask = "0077"; RestrictRealtime = true;
}; RestrictSUIDSGID = true;
SystemCallArchitectures = "native";
SystemCallFilter = [
"@system-service"
"~@privileged"
"~@resources"
];
UMask = "0077";
};
}; };
networking.firewall = mkIf cfg.openFirewall { networking.firewall = mkIf cfg.openFirewall {

View File

@ -1,20 +1,23 @@
import ./make-test-python.nix ({ lib, pkgs, ... }: { import ./make-test-python.nix (
name = "redlib"; { lib, pkgs, ... }:
meta.maintainers = with lib.maintainers; [ soispha ]; {
name = "redlib";
meta.maintainers = with lib.maintainers; [ soispha ];
nodes.machine = { nodes.machine = {
services.redlib = { services.redlib = {
package = pkgs.redlib; package = pkgs.redlib;
enable = true; enable = true;
# Test CAP_NET_BIND_SERVICE # Test CAP_NET_BIND_SERVICE
port = 80; port = 80;
};
}; };
};
testScript = '' testScript = ''
machine.wait_for_unit("redlib.service") machine.wait_for_unit("redlib.service")
machine.wait_for_open_port(80) machine.wait_for_open_port(80)
# Query a page that does not require Internet access # Query a page that does not require Internet access
machine.succeed("curl --fail http://localhost:80/settings") machine.succeed("curl --fail http://localhost:80/settings")
''; '';
}) }
)