/ module.nix
module.nix
  1  {
  2    lib,
  3    config,
  4    pkgs,
  5    ...
  6  }:
  7  let
  8    cfg = config.services.mcchunkie;
  9  in
 10  {
 11    options = with lib; {
 12      services.mcchunkie = {
 13        enable = lib.mkEnableOption "Enable mcchunkie";
 14  
 15        user = mkOption {
 16          type =
 17            with types;
 18            oneOf [
 19              str
 20              int
 21            ];
 22          default = "mcchunkie";
 23          description = ''
 24            The user the service will use.
 25          '';
 26        };
 27  
 28        group = mkOption {
 29          type =
 30            with types;
 31            oneOf [
 32              str
 33              int
 34            ];
 35          default = "mcchunkie";
 36          description = ''
 37            The group the service will use.
 38          '';
 39        };
 40  
 41        dataDir = mkOption {
 42          type = types.path;
 43          default = "/var/lib/mcchunkie";
 44          description = "Path mcchunkie will use to store data";
 45        };
 46  
 47        disabledPlugins = mkOption {
 48          type = with types; listOf str;
 49          default = [ ];
 50          description = "Plugins to disable.";
 51        };
 52  
 53        disabledChats = mkOption {
 54          type = with types; listOf str;
 55          default = [ ];
 56          description = "Chat services to disable.";
 57        };
 58  
 59        package = mkOption {
 60          type = types.package;
 61          default = pkgs.mcchunkie;
 62          defaultText = literalExpression "pkgs.mcchunkie";
 63          description = "The package to use for mcchunkie";
 64        };
 65      };
 66    };
 67  
 68    config = lib.mkIf (cfg.enable) {
 69      users.groups.${cfg.group} = { };
 70      users.users.${cfg.user} = {
 71        description = "mcchunkie service user";
 72        isSystemUser = true;
 73        home = "${cfg.dataDir}";
 74        createHome = true;
 75        group = "${cfg.group}";
 76      };
 77  
 78      systemd.services.mcchunkie = {
 79        enable = true;
 80        description = "mcchunkie server";
 81        after = [ "network-online.target" ];
 82        wants = [ "network-online.target" ];
 83        wantedBy = [ "multi-user.target" ];
 84  
 85        environment = {
 86          HOME = "${cfg.dataDir}";
 87        };
 88  
 89        serviceConfig = {
 90          User = cfg.user;
 91          Group = cfg.group;
 92  
 93          ExecStart =
 94            let
 95              inherit (builtins) concatStringsSep;
 96              mkMcOpt =
 97                flag: list:
 98                let
 99                  realFlag = if list != [ ] then flag else "";
100                in
101                (concatStringsSep " " [
102                  realFlag
103                  (concatStringsSep "," list)
104                ]);
105              disabledPluginStr = if cfg.disabledPlugins != [ ] then (mkMcOpt "-dp" cfg.disabledPlugins) else "";
106              disabledChatStr = if cfg.disabledChats != [ ] then (mkMcOpt "-dc" cfg.disabledChats) else "";
107            in
108            ''
109              ${cfg.package}/bin/mcchunkie -db ${cfg.dataDir}/db ${disabledPluginStr} ${disabledChatStr}
110            '';
111        };
112      };
113    };
114  }