2022-08-29 13:48:40 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.programs.rust-motd;
|
|
|
|
format = pkgs.formats.toml { };
|
2023-09-21 00:05:35 +02:00
|
|
|
|
2023-09-26 23:28:40 +02:00
|
|
|
# Order the sections in the TOML according to the order of sections
|
|
|
|
# in `cfg.order`.
|
2023-09-21 00:05:35 +02:00
|
|
|
motdConf = pkgs.runCommand "motd.conf"
|
2023-09-27 12:45:41 +02:00
|
|
|
{
|
2023-09-21 00:05:35 +02:00
|
|
|
__structuredAttrs = true;
|
2023-09-27 12:45:41 +02:00
|
|
|
inherit (cfg) order settings;
|
2023-09-21 00:05:35 +02:00
|
|
|
nativeBuildInputs = [ pkgs.remarshal pkgs.jq ];
|
2023-09-27 12:45:41 +02:00
|
|
|
}
|
2023-09-21 00:05:35 +02:00
|
|
|
''
|
2023-09-21 01:09:32 +02:00
|
|
|
cat "$NIX_ATTRS_JSON_FILE" \
|
2023-09-27 12:45:41 +02:00
|
|
|
| jq '.settings as $settings
|
|
|
|
| .order
|
|
|
|
| map({ key: ., value: $settings."\(.)" })
|
|
|
|
| from_entries' -r \
|
2023-09-21 00:05:35 +02:00
|
|
|
| json2toml /dev/stdin "$out"
|
|
|
|
'';
|
2022-08-29 13:48:40 +02:00
|
|
|
in {
|
|
|
|
options.programs.rust-motd = {
|
2024-04-13 14:54:15 +02:00
|
|
|
enable = mkEnableOption "rust-motd, a Message Of The Day (MOTD) generator";
|
2022-08-29 13:48:40 +02:00
|
|
|
enableMotdInSSHD = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2022-08-29 13:48:40 +02:00
|
|
|
Whether to let `openssh` print the
|
|
|
|
result when entering a new `ssh`-session.
|
|
|
|
By default either nothing or a static file defined via
|
|
|
|
[](#opt-users.motd) is printed. Because of that,
|
|
|
|
the latter option is incompatible with this module.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
refreshInterval = mkOption {
|
|
|
|
default = "*:0/5";
|
|
|
|
type = types.str;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2022-08-29 13:48:40 +02:00
|
|
|
Interval in which the {manpage}`motd(5)` file is refreshed.
|
|
|
|
For possible formats, please refer to {manpage}`systemd.time(7)`.
|
|
|
|
'';
|
|
|
|
};
|
2023-09-26 23:28:40 +02:00
|
|
|
order = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = attrNames cfg.settings;
|
|
|
|
defaultText = literalExpression "attrNames cfg.settings";
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2023-09-27 12:45:41 +02:00
|
|
|
The order of the sections in [](#opt-programs.rust-motd.settings).
|
|
|
|
By default they are ordered alphabetically.
|
|
|
|
|
|
|
|
Context: since attribute sets in Nix are always
|
2023-09-26 23:28:40 +02:00
|
|
|
ordered alphabetically internally this means that
|
2023-09-21 00:05:35 +02:00
|
|
|
|
2023-09-26 23:28:40 +02:00
|
|
|
```nix
|
|
|
|
{
|
|
|
|
uptime = { /* ... */ };
|
|
|
|
banner = { /* ... */ };
|
|
|
|
}
|
|
|
|
```
|
2023-09-21 00:05:35 +02:00
|
|
|
|
2023-09-26 23:28:40 +02:00
|
|
|
will still have `banner` displayed before `uptime`.
|
2023-09-21 00:05:35 +02:00
|
|
|
|
2023-09-26 23:28:40 +02:00
|
|
|
To work around that, this option can be used to define the order of all keys,
|
|
|
|
i.e.
|
2023-09-21 00:05:35 +02:00
|
|
|
|
2023-09-26 23:28:40 +02:00
|
|
|
```nix
|
|
|
|
{
|
|
|
|
order = [
|
|
|
|
"uptime"
|
|
|
|
"banner"
|
|
|
|
];
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
makes sure that `uptime` is placed before `banner` in the motd.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
settings = mkOption {
|
2023-09-27 12:45:41 +02:00
|
|
|
type = types.attrsOf format.type;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2022-08-29 13:48:40 +02:00
|
|
|
Settings on what to generate. Please read the
|
|
|
|
[upstream documentation](https://github.com/rust-motd/rust-motd/blob/main/README.md#configuration)
|
|
|
|
for further information.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
assertions = [
|
|
|
|
{ assertion = config.users.motd == null;
|
|
|
|
message = ''
|
|
|
|
`programs.rust-motd` is incompatible with `users.motd`!
|
|
|
|
'';
|
|
|
|
}
|
2023-09-27 12:45:41 +02:00
|
|
|
{ assertion = sort (a: b: a < b) cfg.order == attrNames cfg.settings;
|
2023-09-26 23:28:40 +02:00
|
|
|
message = ''
|
|
|
|
Please ensure that every section from `programs.rust-motd.settings` is present in
|
|
|
|
`programs.rust-motd.order`.
|
|
|
|
'';
|
|
|
|
}
|
2022-08-29 13:48:40 +02:00
|
|
|
];
|
|
|
|
systemd.services.rust-motd = {
|
|
|
|
path = with pkgs; [ bash ];
|
|
|
|
documentation = [ "https://github.com/rust-motd/rust-motd/blob/v${pkgs.rust-motd.version}/README.md" ];
|
|
|
|
description = "motd generator";
|
2023-09-20 22:16:03 +02:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-08-29 13:48:40 +02:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${pkgs.writeShellScript "update-motd" ''
|
2023-09-21 00:05:35 +02:00
|
|
|
${pkgs.rust-motd}/bin/rust-motd ${motdConf} > motd
|
2022-08-29 13:48:40 +02:00
|
|
|
''}";
|
|
|
|
CapabilityBoundingSet = [ "" ];
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectSystem = "full";
|
|
|
|
StateDirectory = "rust-motd";
|
2022-09-18 23:17:50 +02:00
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" ];
|
2022-08-29 13:48:40 +02:00
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
RemoveIPC = true;
|
|
|
|
WorkingDirectory = "/var/lib/rust-motd";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
systemd.timers.rust-motd = {
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig.OnCalendar = cfg.refreshInterval;
|
|
|
|
};
|
|
|
|
security.pam.services.sshd.text = mkIf cfg.enableMotdInSSHD (mkDefault (mkAfter ''
|
|
|
|
session optional ${pkgs.pam}/lib/security/pam_motd.so motd=/var/lib/rust-motd/motd
|
|
|
|
''));
|
|
|
|
services.openssh.extraConfig = mkIf (cfg.settings ? last_login && cfg.settings.last_login != {}) ''
|
|
|
|
PrintLastLog no
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
meta.maintainers = with maintainers; [ ma27 ];
|
|
|
|
}
|