2022-08-29 11:48:40 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.programs.rust-motd;
|
|
|
|
format = pkgs.formats.toml { };
|
2023-09-20 22:05:35 +00:00
|
|
|
|
2023-09-26 21:28:40 +00:00
|
|
|
# Order the sections in the TOML according to the order of sections
|
|
|
|
# in `cfg.order`.
|
2023-09-20 22:05:35 +00:00
|
|
|
motdConf = pkgs.runCommand "motd.conf"
|
2023-09-27 10:45:41 +00:00
|
|
|
{
|
2023-09-20 22:05:35 +00:00
|
|
|
__structuredAttrs = true;
|
2023-09-27 10:45:41 +00:00
|
|
|
inherit (cfg) order settings;
|
2023-09-20 22:05:35 +00:00
|
|
|
nativeBuildInputs = [ pkgs.remarshal pkgs.jq ];
|
2023-09-27 10:45:41 +00:00
|
|
|
}
|
2023-09-20 22:05:35 +00:00
|
|
|
''
|
2023-09-20 23:09:32 +00:00
|
|
|
cat "$NIX_ATTRS_JSON_FILE" \
|
2023-09-27 10:45:41 +00:00
|
|
|
| jq '.settings as $settings
|
|
|
|
| .order
|
|
|
|
| map({ key: ., value: $settings."\(.)" })
|
|
|
|
| from_entries' -r \
|
2023-09-20 22:05:35 +00:00
|
|
|
| json2toml /dev/stdin "$out"
|
|
|
|
'';
|
2022-08-29 11:48:40 +00:00
|
|
|
in {
|
|
|
|
options.programs.rust-motd = {
|
2024-04-17 11:37:58 +00:00
|
|
|
enable = lib.mkEnableOption "rust-motd, a Message Of The Day (MOTD) generator";
|
|
|
|
enableMotdInSSHD = lib.mkOption {
|
2022-08-29 11:48:40 +00:00
|
|
|
default = true;
|
2024-04-17 11:37:58 +00:00
|
|
|
type = lib.types.bool;
|
2022-08-29 11:48:40 +00:00
|
|
|
description = ''
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
2024-04-17 11:37:58 +00:00
|
|
|
refreshInterval = lib.mkOption {
|
2022-08-29 11:48:40 +00:00
|
|
|
default = "*:0/5";
|
2024-04-17 11:37:58 +00:00
|
|
|
type = lib.types.str;
|
2022-08-29 11:48:40 +00:00
|
|
|
description = ''
|
|
|
|
Interval in which the {manpage}`motd(5)` file is refreshed.
|
|
|
|
For possible formats, please refer to {manpage}`systemd.time(7)`.
|
|
|
|
'';
|
|
|
|
};
|
2024-04-17 11:37:58 +00:00
|
|
|
order = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
default = builtins.attrNames cfg.settings;
|
|
|
|
defaultText = lib.literalExpression "attrNames cfg.settings";
|
2023-09-26 21:28:40 +00:00
|
|
|
description = ''
|
2023-09-27 10:45:41 +00: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 21:28:40 +00:00
|
|
|
ordered alphabetically internally this means that
|
2023-09-20 22:05:35 +00:00
|
|
|
|
2023-09-26 21:28:40 +00:00
|
|
|
```nix
|
|
|
|
{
|
|
|
|
uptime = { /* ... */ };
|
|
|
|
banner = { /* ... */ };
|
|
|
|
}
|
|
|
|
```
|
2023-09-20 22:05:35 +00:00
|
|
|
|
2023-09-26 21:28:40 +00:00
|
|
|
will still have `banner` displayed before `uptime`.
|
2023-09-20 22:05:35 +00:00
|
|
|
|
2023-09-26 21:28:40 +00:00
|
|
|
To work around that, this option can be used to define the order of all keys,
|
|
|
|
i.e.
|
2023-09-20 22:05:35 +00:00
|
|
|
|
2023-09-26 21:28:40 +00:00
|
|
|
```nix
|
|
|
|
{
|
|
|
|
order = [
|
|
|
|
"uptime"
|
|
|
|
"banner"
|
|
|
|
];
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
makes sure that `uptime` is placed before `banner` in the motd.
|
|
|
|
'';
|
|
|
|
};
|
2024-04-17 11:37:58 +00:00
|
|
|
settings = lib.mkOption {
|
|
|
|
type = lib.types.attrsOf format.type;
|
2022-08-29 11:48:40 +00:00
|
|
|
description = ''
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2024-04-17 11:37:58 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
2022-08-29 11:48:40 +00:00
|
|
|
assertions = [
|
2024-08-29 11:31:49 +00:00
|
|
|
{ assertion = config.users.motd == "";
|
2022-08-29 11:48:40 +00:00
|
|
|
message = ''
|
|
|
|
`programs.rust-motd` is incompatible with `users.motd`!
|
|
|
|
'';
|
|
|
|
}
|
2024-04-17 11:37:58 +00:00
|
|
|
{ assertion = builtins.sort (a: b: a < b) cfg.order == builtins.attrNames cfg.settings;
|
2023-09-26 21:28:40 +00:00
|
|
|
message = ''
|
|
|
|
Please ensure that every section from `programs.rust-motd.settings` is present in
|
|
|
|
`programs.rust-motd.order`.
|
|
|
|
'';
|
|
|
|
}
|
2022-08-29 11:48:40 +00: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 20:16:03 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-08-29 11:48:40 +00:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${pkgs.writeShellScript "update-motd" ''
|
2023-09-20 22:05:35 +00:00
|
|
|
${pkgs.rust-motd}/bin/rust-motd ${motdConf} > motd
|
2022-08-29 11:48:40 +00: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 21:17:50 +00:00
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" ];
|
2022-08-29 11:48:40 +00: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;
|
|
|
|
};
|
2024-04-17 11:37:58 +00:00
|
|
|
security.pam.services.sshd.text = lib.mkIf cfg.enableMotdInSSHD (lib.mkDefault (lib.mkAfter ''
|
2022-08-29 11:48:40 +00:00
|
|
|
session optional ${pkgs.pam}/lib/security/pam_motd.so motd=/var/lib/rust-motd/motd
|
|
|
|
''));
|
2024-04-17 11:37:58 +00:00
|
|
|
services.openssh.extraConfig = lib.mkIf (cfg.settings ? last_login && cfg.settings.last_login != {}) ''
|
2022-08-29 11:48:40 +00:00
|
|
|
PrintLastLog no
|
|
|
|
'';
|
|
|
|
};
|
2024-04-17 11:37:58 +00:00
|
|
|
meta.maintainers = with lib.maintainers; [ ma27 ];
|
2022-08-29 11:48:40 +00:00
|
|
|
}
|