stockholm/krebs/3modules/htgen.nix

102 lines
2.3 KiB
Nix
Raw Normal View History

2017-03-16 19:56:28 +00:00
{ config, lib, pkgs, ... }:
2023-06-10 10:50:53 +00:00
with import ../../lib/pure.nix { inherit lib; };
2017-03-16 19:56:28 +00:00
let
2022-12-06 18:45:38 +00:00
optionalAttr = name: value:
if name != null then
{ ${name} = value; }
else
{};
2017-03-16 19:56:28 +00:00
cfg = config.krebs.htgen;
out = {
options.krebs.htgen = api;
config = imp;
};
api = mkOption {
2017-03-16 21:12:11 +00:00
default = {};
2017-03-16 19:56:28 +00:00
type = types.attrsOf (types.submodule ({ config, ... }: {
options = {
2021-11-08 03:30:33 +00:00
enable = mkEnableOption "krebs.htgen-${config._module.args.name}";
2017-03-16 19:56:28 +00:00
name = mkOption {
type = types.username;
default = config._module.args.name;
};
2021-02-02 21:28:25 +00:00
package = mkOption {
default = pkgs.htgen;
type = types.package;
};
2017-03-16 19:56:28 +00:00
port = mkOption {
type = types.uint;
};
script = mkOption {
2022-12-06 18:45:38 +00:00
type = types.nullOr types.str;
default = null;
};
scriptFile = mkOption {
type = types.nullOr (types.either types.package types.pathname);
2022-12-06 18:45:38 +00:00
default = null;
2017-03-16 19:56:28 +00:00
};
2022-12-06 18:45:38 +00:00
2017-03-16 19:56:28 +00:00
user = mkOption {
type = types.user;
default = {
name = "htgen-${config.name}";
home = "/var/lib/htgen-${config.name}";
};
2021-11-08 03:30:33 +00:00
defaultText = {
name = "htgen-name";
home = "/var/lib/htgen-name";
};
2017-03-16 19:56:28 +00:00
};
};
}));
};
imp = {
systemd.services = mapAttrs' (name: htgen:
nameValuePair "htgen-${name}" {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
environment = {
HTGEN_PORT = toString htgen.port;
2022-12-06 18:45:38 +00:00
}
// optionalAttr "HTGEN_SCRIPT" htgen.script
// optionalAttr "HTGEN_SCRIPT_FILE" htgen.scriptFile
;
2017-03-16 19:56:28 +00:00
serviceConfig = {
SyslogIdentifier = "htgen";
User = htgen.user.name;
PrivateTmp = true;
Restart = "always";
2021-02-02 21:28:25 +00:00
ExecStart = "${htgen.package}/bin/htgen --serve";
2017-03-16 19:56:28 +00:00
};
}
) cfg;
users.users = mapAttrs' (name: htgen:
nameValuePair htgen.user.name {
inherit (htgen.user) home name uid;
2021-12-01 16:55:22 +00:00
group = htgen.user.name;
2017-03-16 19:56:28 +00:00
createHome = true;
isSystemUser = true;
2017-03-16 19:56:28 +00:00
}
) cfg;
2022-01-03 13:56:44 +00:00
users.groups = mapAttrs' (name: htgen:
nameValuePair htgen.user.name {
name = htgen.user.name;
gid = htgen.user.uid;
}
) cfg;
2021-12-01 16:55:22 +00:00
2017-03-16 19:56:28 +00:00
};
in out