lol

Merge pull request #256407 from Ma27/motd-ordering

nixos/rust-motd: allow ordering sections

authored by

Maximilian Bosch and committed by
GitHub
4305d167 6e8e41ec

+61 -4
+61 -4
nixos/modules/programs/rust-motd.nix
··· 5 5 let 6 6 cfg = config.programs.rust-motd; 7 7 format = pkgs.formats.toml { }; 8 + 9 + # Order the sections in the TOML according to the order of sections 10 + # in `cfg.order`. 11 + motdConf = pkgs.runCommand "motd.conf" 12 + { 13 + __structuredAttrs = true; 14 + inherit (cfg) order settings; 15 + nativeBuildInputs = [ pkgs.remarshal pkgs.jq ]; 16 + } 17 + '' 18 + cat "$NIX_ATTRS_JSON_FILE" \ 19 + | jq '.settings as $settings 20 + | .order 21 + | map({ key: ., value: $settings."\(.)" }) 22 + | from_entries' -r \ 23 + | json2toml /dev/stdin "$out" 24 + ''; 8 25 in { 9 26 options.programs.rust-motd = { 10 27 enable = mkEnableOption (lib.mdDoc "rust-motd"); ··· 27 44 For possible formats, please refer to {manpage}`systemd.time(7)`. 28 45 ''; 29 46 }; 47 + order = mkOption { 48 + type = types.listOf types.str; 49 + default = attrNames cfg.settings; 50 + defaultText = literalExpression "attrNames cfg.settings"; 51 + description = mdDoc '' 52 + The order of the sections in [](#opt-programs.rust-motd.settings). 53 + By default they are ordered alphabetically. 54 + 55 + Context: since attribute sets in Nix are always 56 + ordered alphabetically internally this means that 57 + 58 + ```nix 59 + { 60 + uptime = { /* ... */ }; 61 + banner = { /* ... */ }; 62 + } 63 + ``` 64 + 65 + will still have `banner` displayed before `uptime`. 66 + 67 + To work around that, this option can be used to define the order of all keys, 68 + i.e. 69 + 70 + ```nix 71 + { 72 + order = [ 73 + "uptime" 74 + "banner" 75 + ]; 76 + } 77 + ``` 78 + 79 + makes sure that `uptime` is placed before `banner` in the motd. 80 + ''; 81 + }; 30 82 settings = mkOption { 31 - type = types.submodule { 32 - freeformType = format.type; 33 - }; 83 + type = types.attrsOf format.type; 34 84 description = mdDoc '' 35 85 Settings on what to generate. Please read the 36 86 [upstream documentation](https://github.com/rust-motd/rust-motd/blob/main/README.md#configuration) ··· 45 95 `programs.rust-motd` is incompatible with `users.motd`! 46 96 ''; 47 97 } 98 + { assertion = sort (a: b: a < b) cfg.order == attrNames cfg.settings; 99 + message = '' 100 + Please ensure that every section from `programs.rust-motd.settings` is present in 101 + `programs.rust-motd.order`. 102 + ''; 103 + } 48 104 ]; 49 105 systemd.services.rust-motd = { 50 106 path = with pkgs; [ bash ]; 51 107 documentation = [ "https://github.com/rust-motd/rust-motd/blob/v${pkgs.rust-motd.version}/README.md" ]; 52 108 description = "motd generator"; 109 + wantedBy = [ "multi-user.target" ]; 53 110 serviceConfig = { 54 111 ExecStart = "${pkgs.writeShellScript "update-motd" '' 55 - ${pkgs.rust-motd}/bin/rust-motd ${format.generate "motd.conf" cfg.settings} > motd 112 + ${pkgs.rust-motd}/bin/rust-motd ${motdConf} > motd 56 113 ''}"; 57 114 CapabilityBoundingSet = [ "" ]; 58 115 LockPersonality = true;