2019-05-01 04:38:58 +08:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
homu = pkgs.callPackage ./pkg.nix {};
|
|
|
|
|
|
|
|
toToml = key: value:
|
|
|
|
let valueString =
|
|
|
|
if builtins.isString value
|
|
|
|
then "\"" + (builtins.replaceStrings ["\"" "\\"] ["\\\"" "\\\\"] value) + "\""
|
|
|
|
else toString value;
|
|
|
|
in "${key} = ${valueString}\n";
|
|
|
|
|
|
|
|
defaultConfig = {
|
|
|
|
db = {
|
|
|
|
file = "/var/db/homu/main.db";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
cfg = config.services.homu;
|
|
|
|
homuConfig = defaultConfig // cfg.config;
|
|
|
|
configFilter = f:
|
|
|
|
filterAttrs (key: value: f value) homuConfig;
|
|
|
|
topLevelConfig =
|
|
|
|
configFilter (value: ! builtins.isAttrs value);
|
|
|
|
configSections =
|
|
|
|
configFilter (value: builtins.isAttrs value);
|
|
|
|
|
|
|
|
configFile = builtins.toFile "config.toml" (
|
|
|
|
builtins.concatStringsSep "" (
|
|
|
|
(attrsets.mapAttrsToList toToml topLevelConfig) ++
|
|
|
|
(builtins.concatLists (attrsets.mapAttrsToList
|
|
|
|
(sectionName: sectionConfig:
|
|
|
|
[ "[${sectionName}]\n" ] ++
|
|
|
|
(attrsets.mapAttrsToList toToml sectionConfig)
|
|
|
|
) configSections)
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2019-05-01 04:50:26 +08:00
|
|
|
dbDir = dirOf homuConfig.db.file;
|
2019-05-01 04:38:58 +08:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options.services.homu = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Enable the bot";
|
|
|
|
};
|
|
|
|
config = mkOption {
|
|
|
|
description = "Structured data for config.toml";
|
|
|
|
type = with types; attrsOf unspecified;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2019-05-01 04:50:26 +08:00
|
|
|
users.users.homu = {
|
|
|
|
group = "homu";
|
|
|
|
home = dbDir;
|
|
|
|
createHome = true;
|
2019-05-01 04:38:58 +08:00
|
|
|
};
|
2019-05-01 04:50:26 +08:00
|
|
|
users.groups.homu = {};
|
|
|
|
|
2019-05-01 04:38:58 +08:00
|
|
|
systemd.services.homu = {
|
|
|
|
description = "Homu bot";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
|
|
|
ExecStart = "${homu}/bin/homu -c ${configFile}";
|
|
|
|
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = "5sec";
|
|
|
|
|
2019-05-01 04:50:26 +08:00
|
|
|
User = "homu";
|
|
|
|
Group = "homu";
|
2019-05-01 04:38:58 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|