2022-08-29 14:48:40 +03:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.programs.rust-motd;
|
|
|
|
format = pkgs.formats.toml { };
|
2023-09-21 01:05:35 +03:00
|
|
|
|
2023-09-27 00:28:40 +03:00
|
|
|
# Order the sections in the TOML according to the order of sections
|
|
|
|
# in `cfg.order`.
|
2023-09-21 01:05:35 +03:00
|
|
|
motdConf = pkgs.runCommand "motd.conf"
|
2023-09-27 13:45:41 +03:00
|
|
|
{
|
2023-09-21 01:05:35 +03:00
|
|
|
__structuredAttrs = true;
|
2023-09-27 13:45:41 +03:00
|
|
|
inherit (cfg) order settings;
|
2023-09-21 01:05:35 +03:00
|
|
|
nativeBuildInputs = [ pkgs.remarshal pkgs.jq ];
|
2023-09-27 13:45:41 +03:00
|
|
|
}
|
2023-09-21 01:05:35 +03:00
|
|
|
''
|
2023-09-21 02:09:32 +03:00
|
|
|
cat "$NIX_ATTRS_JSON_FILE" \
|
2023-09-27 13:45:41 +03:00
|
|
|
| jq '.settings as $settings
|
|
|
|
| .order
|
|
|
|
| map({ key: ., value: $settings."\(.)" })
|
|
|
|
| from_entries' -r \
|
2023-09-21 01:05:35 +03:00
|
|
|
| json2toml /dev/stdin "$out"
|
|
|
|
'';
|
2022-08-29 14:48:40 +03:00
|
|
|
in {
|
|
|
|
options.programs.rust-motd = {
|
2022-09-01 20:44:36 +03:00
|
|
|
enable = mkEnableOption (lib.mdDoc "rust-motd");
|
2022-08-29 14:48:40 +03:00
|
|
|
enableMotdInSSHD = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
|
|
|
description = mdDoc ''
|
|
|
|
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;
|
|
|
|
description = mdDoc ''
|
|
|
|
Interval in which the {manpage}`motd(5)` file is refreshed.
|
|
|
|
For possible formats, please refer to {manpage}`systemd.time(7)`.
|
|
|
|
'';
|
|
|
|
};
|
2023-09-27 00:28:40 +03:00
|
|
|
order = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = attrNames cfg.settings;
|
|
|
|
defaultText = literalExpression "attrNames cfg.settings";
|
|
|
|
description = mdDoc ''
|
2023-09-27 13:45:41 +03: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-27 00:28:40 +03:00
|
|
|
ordered alphabetically internally this means that
|
2023-09-21 01:05:35 +03:00
|
|
|
|
2023-09-27 00:28:40 +03:00
|
|
|
```nix
|
|
|
|
{
|
|
|
|
uptime = { /* ... */ };
|
|
|
|
banner = { /* ... */ };
|
|
|
|
}
|
|
|
|
```
|
2023-09-21 01:05:35 +03:00
|
|
|
|
2023-09-27 00:28:40 +03:00
|
|
|
will still have `banner` displayed before `uptime`.
|
2023-09-21 01:05:35 +03:00
|
|
|
|
2023-09-27 00:28:40 +03:00
|
|
|
To work around that, this option can be used to define the order of all keys,
|
|
|
|
i.e.
|
2023-09-21 01:05:35 +03:00
|
|
|
|
2023-09-27 00:28:40 +03:00
|
|
|
```nix
|
|
|
|
{
|
|
|
|
order = [
|
|
|
|
"uptime"
|
|
|
|
"banner"
|
|
|
|
];
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
makes sure that `uptime` is placed before `banner` in the motd.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
settings = mkOption {
|
2023-09-27 13:45:41 +03:00
|
|
|
type = types.attrsOf format.type;
|
2022-08-29 14:48:40 +03:00
|
|
|
description = mdDoc ''
|
|
|
|
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 13:45:41 +03:00
|
|
|
{ assertion = sort (a: b: a < b) cfg.order == attrNames cfg.settings;
|
2023-09-27 00:28:40 +03:00
|
|
|
message = ''
|
|
|
|
Please ensure that every section from `programs.rust-motd.settings` is present in
|
|
|
|
`programs.rust-motd.order`.
|
|
|
|
'';
|
|
|
|
}
|
2022-08-29 14:48:40 +03: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 23:16:03 +03:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-08-29 14:48:40 +03:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${pkgs.writeShellScript "update-motd" ''
|
2023-09-21 01:05:35 +03:00
|
|
|
${pkgs.rust-motd}/bin/rust-motd ${motdConf} > motd
|
2022-08-29 14:48:40 +03: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-19 00:17:50 +03:00
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" ];
|
2022-08-29 14:48:40 +03: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 ];
|
|
|
|
}
|