2019-05-24 21:17:51 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.spacecookie;
|
2021-03-10 22:12:36 +01:00
|
|
|
|
|
|
|
spacecookieConfig = {
|
2021-03-10 22:34:22 +01:00
|
|
|
listen = {
|
|
|
|
inherit (cfg) port;
|
|
|
|
};
|
2021-03-10 22:12:36 +01:00
|
|
|
} // cfg.settings;
|
|
|
|
|
|
|
|
format = pkgs.formats.json {};
|
|
|
|
|
|
|
|
configFile = format.generate "spacecookie.json" spacecookieConfig;
|
|
|
|
|
2019-05-24 21:17:51 +02:00
|
|
|
in {
|
2021-03-10 22:12:36 +01:00
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [ "services" "spacecookie" "root" ] [ "services" "spacecookie" "settings" "root" ])
|
|
|
|
(mkRenamedOptionModule [ "services" "spacecookie" "hostname" ] [ "services" "spacecookie" "settings" "hostname" ])
|
|
|
|
];
|
2019-05-24 21:17:51 +02:00
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.spacecookie = {
|
|
|
|
|
|
|
|
enable = mkEnableOption "spacecookie";
|
|
|
|
|
2021-03-10 21:53:06 +01:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
2021-03-19 22:43:34 +01:00
|
|
|
default = pkgs.spacecookie;
|
|
|
|
defaultText = literalExample "pkgs.spacecookie";
|
|
|
|
example = literalExample "pkgs.haskellPackages.spacecookie";
|
2021-03-10 21:53:06 +01:00
|
|
|
description = ''
|
|
|
|
The spacecookie derivation to use. This can be used to
|
|
|
|
override the used package or to use another version.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-03-10 21:56:11 +01:00
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to open the necessary port in the firewall for spacecookie.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-05-24 21:17:51 +02:00
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 70;
|
2020-12-28 00:08:45 +01:00
|
|
|
description = ''
|
2021-03-10 21:56:11 +01:00
|
|
|
Port the gopher service should be exposed on.
|
2020-12-28 00:08:45 +01:00
|
|
|
'';
|
2019-05-24 21:17:51 +02:00
|
|
|
};
|
|
|
|
|
2021-03-10 22:07:27 +01:00
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "[::]";
|
|
|
|
description = ''
|
|
|
|
Address to listen on. Must be in the
|
|
|
|
<literal>ListenStream=</literal> syntax of
|
|
|
|
<link xlink:href="https://www.freedesktop.org/software/systemd/man/systemd.socket.html">systemd.socket(5)</link>.
|
|
|
|
'';
|
|
|
|
};
|
2021-03-10 22:12:36 +01:00
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
type = types.submodule {
|
|
|
|
freeformType = format.type;
|
|
|
|
|
|
|
|
options.hostname = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "localhost";
|
|
|
|
description = ''
|
|
|
|
The hostname the service is reachable via. Clients
|
|
|
|
will use this hostname for further requests after
|
|
|
|
loading the initial gopher menu.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
options.root = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/srv/gopher";
|
|
|
|
description = ''
|
|
|
|
The directory spacecookie should serve via gopher.
|
|
|
|
Files in there need to be world-readable since
|
|
|
|
the spacecookie service file sets
|
|
|
|
<literal>DynamicUser=true</literal>.
|
|
|
|
'';
|
|
|
|
};
|
2021-03-10 22:34:22 +01:00
|
|
|
|
|
|
|
options.log = {
|
|
|
|
enable = mkEnableOption "logging for spacecookie"
|
|
|
|
// { default = true; example = false; };
|
|
|
|
|
|
|
|
hide-ips = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
If enabled, spacecookie will hide personal
|
|
|
|
information of users like IP addresses from
|
|
|
|
log output.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
hide-time = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
# since we are starting with systemd anyways
|
|
|
|
# we deviate from the default behavior here:
|
|
|
|
# journald will add timestamps, so no need
|
|
|
|
# to double up.
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
If enabled, spacecookie will not print timestamps
|
|
|
|
at the beginning of every log line.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
level = mkOption {
|
|
|
|
type = types.enum [
|
|
|
|
"info"
|
|
|
|
"warn"
|
|
|
|
"error"
|
|
|
|
];
|
|
|
|
default = "info";
|
|
|
|
description = ''
|
|
|
|
Log level for the spacecookie service.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2021-03-10 22:12:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
description = ''
|
|
|
|
Settings for spacecookie. The settings set here are
|
|
|
|
directly translated to the spacecookie JSON config
|
2021-03-10 22:34:22 +01:00
|
|
|
file. See
|
|
|
|
<link xlink:href="https://sternenseemann.github.io/spacecookie/spacecookie.json.5.html">spacecookie.json(5)</link>
|
2021-03-10 22:12:36 +01:00
|
|
|
for explanations of all options.
|
|
|
|
'';
|
|
|
|
};
|
2019-05-24 21:17:51 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2021-03-10 22:12:36 +01:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = !(cfg.settings ? user);
|
|
|
|
message = ''
|
|
|
|
spacecookie is started as a normal user, so the setuid
|
|
|
|
feature doesn't work. If you want to run spacecookie as
|
|
|
|
a specific user, set:
|
|
|
|
systemd.services.spacecookie.serviceConfig = {
|
|
|
|
DynamicUser = false;
|
|
|
|
User = "youruser";
|
|
|
|
Group = "yourgroup";
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
{
|
2021-03-10 22:34:22 +01:00
|
|
|
assertion = !(cfg.settings ? listen || cfg.settings ? port);
|
2021-03-10 22:12:36 +01:00
|
|
|
message = ''
|
|
|
|
The NixOS spacecookie module uses socket activation,
|
2021-03-10 22:34:22 +01:00
|
|
|
so the listen options have no effect. Use the port
|
|
|
|
and address options in services.spacecookie instead.
|
2021-03-10 22:12:36 +01:00
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
2019-05-24 21:17:51 +02:00
|
|
|
|
|
|
|
systemd.sockets.spacecookie = {
|
|
|
|
description = "Socket for the Spacecookie Gopher Server";
|
|
|
|
wantedBy = [ "sockets.target" ];
|
2021-03-10 22:07:27 +01:00
|
|
|
listenStreams = [ "${cfg.address}:${toString cfg.port}" ];
|
2019-05-24 21:17:51 +02:00
|
|
|
socketConfig = {
|
|
|
|
BindIPv6Only = "both";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.spacecookie = {
|
|
|
|
description = "Spacecookie Gopher Server";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
requires = [ "spacecookie.socket" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "notify";
|
2021-03-10 21:53:06 +01:00
|
|
|
ExecStart = "${lib.getBin cfg.package}/bin/spacecookie ${configFile}";
|
2019-05-24 21:17:51 +02:00
|
|
|
FileDescriptorStoreMax = 1;
|
|
|
|
|
|
|
|
DynamicUser = true;
|
|
|
|
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ProtectHome = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateMounts = true;
|
|
|
|
PrivateUsers = true;
|
|
|
|
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
|
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
LockPersonality = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
|
|
|
|
# AF_UNIX for communication with systemd
|
|
|
|
# AF_INET replaced by BindIPv6Only=both
|
|
|
|
RestrictAddressFamilies = "AF_UNIX AF_INET6";
|
|
|
|
};
|
|
|
|
};
|
2021-03-10 21:56:11 +01:00
|
|
|
|
|
|
|
networking.firewall = mkIf cfg.openFirewall {
|
|
|
|
allowedTCPPorts = [ cfg.port ];
|
|
|
|
};
|
2019-05-24 21:17:51 +02:00
|
|
|
};
|
|
|
|
}
|