|
| 1 | +{ withSystem, ... }: |
| 2 | +{ |
| 3 | + flake.modules.nixos.mcl-commands = |
| 4 | + { |
| 5 | + lib, |
| 6 | + pkgs, |
| 7 | + flakeArgs, |
| 8 | + config, |
| 9 | + ... |
| 10 | + }: |
| 11 | + let |
| 12 | + cfg = config.programs.admin-cmds; |
| 13 | + |
| 14 | + makeSystemctlCommand = |
| 15 | + service: command: |
| 16 | + pkgs.writeShellApplication { |
| 17 | + name = "${service}-${command}"; |
| 18 | + text = "systemctl ${command} ${service}.service"; |
| 19 | + }; |
| 20 | + systemctlCommands = builtins.concatMap ( |
| 21 | + service: map (command: (makeSystemctlCommand service command)) cfg.systemctl-commands |
| 22 | + ) cfg.services; |
| 23 | + |
| 24 | + getPackageCommands = |
| 25 | + package: |
| 26 | + lib.pipe "${lib.getExe package}/.." [ |
| 27 | + builtins.readDir |
| 28 | + builtins.attrNames |
| 29 | + ]; |
| 30 | + |
| 31 | + server-help = pkgs.writeShellApplication { |
| 32 | + name = "server-help"; |
| 33 | + text = '' |
| 34 | + echo -e "There are a few sudo commands which:\n |
| 35 | + * Restart certain services\n |
| 36 | + * Get certain services status\n |
| 37 | + * Get certain services logs\n\n |
| 38 | +
|
| 39 | + Available commands:\n |
| 40 | + ${ |
| 41 | + lib.pipe systemctlCommands [ |
| 42 | + (map getPackageCommands) |
| 43 | + builtins.concatLists |
| 44 | + (builtins.concatStringsSep "\n") |
| 45 | + ] |
| 46 | + }" |
| 47 | + ''; |
| 48 | + }; |
| 49 | + in |
| 50 | + { |
| 51 | + options.programs.admin-cmds = with lib; { |
| 52 | + services = mkOption { |
| 53 | + type = types.listOf types.str; |
| 54 | + default = [ ]; |
| 55 | + example = [ |
| 56 | + "nginx" |
| 57 | + "grafana" |
| 58 | + "nimbus-eth2" |
| 59 | + ]; |
| 60 | + description = '' |
| 61 | + Services for which you have admin commands. |
| 62 | + ''; |
| 63 | + }; |
| 64 | + |
| 65 | + systemctl-commands = mkOption { |
| 66 | + type = types.listOf types.str; |
| 67 | + default = [ |
| 68 | + "restart" |
| 69 | + "status" |
| 70 | + "stop" |
| 71 | + ]; |
| 72 | + example = [ |
| 73 | + "restart" |
| 74 | + "start" |
| 75 | + "stop" |
| 76 | + ]; |
| 77 | + description = '' |
| 78 | + Systemd commands which you can use for services. |
| 79 | + ''; |
| 80 | + }; |
| 81 | + }; |
| 82 | + |
| 83 | + config = lib.mkIf (cfg.services != [ ]) { |
| 84 | + security.sudo.extraRules = [ |
| 85 | + { |
| 86 | + groups = [ "metacraft" ]; |
| 87 | + commands = [ |
| 88 | + (lib.pipe systemctlCommands [ |
| 89 | + (map getPackageCommands) |
| 90 | + builtins.concatLists |
| 91 | + (lib.concatMapStringsSep ", " (n: "/run/current-system/sw/bin/${n}")) |
| 92 | + ]) |
| 93 | + ]; |
| 94 | + } |
| 95 | + ]; |
| 96 | + |
| 97 | + environment.systemPackages = systemctlCommands ++ [ server-help ]; |
| 98 | + }; |
| 99 | + }; |
| 100 | +} |
0 commit comments