stockholm/krebs/3modules/htgen.nix

102 lines
2.3 KiB
Nix
Raw Normal View History

2017-03-16 20:56:28 +01:00
{ config, lib, pkgs, ... }:
with import <stockholm/lib>;
let
2022-12-06 19:45:38 +01:00
optionalAttr = name: value:
if name != null then
{ ${name} = value; }
else
{};
2017-03-16 20:56:28 +01:00
cfg = config.krebs.htgen;
out = {
options.krebs.htgen = api;
config = imp;
};
api = mkOption {
2017-03-16 22:12:11 +01:00
default = {};
2017-03-16 20:56:28 +01:00
type = types.attrsOf (types.submodule ({ config, ... }: {
options = {
2021-11-08 04:30:33 +01:00
enable = mkEnableOption "krebs.htgen-${config._module.args.name}";
2017-03-16 20:56:28 +01:00
name = mkOption {
type = types.username;
default = config._module.args.name;
};
2021-02-02 22:28:25 +01:00
package = mkOption {
default = pkgs.htgen;
type = types.package;
};
2017-03-16 20:56:28 +01:00
port = mkOption {
type = types.uint;
};
script = mkOption {
2022-12-06 19:45:38 +01:00
type = types.nullOr types.str;
default = null;
};
scriptFile = mkOption {
type = types.nullOr types.str;
default = null;
2017-03-16 20:56:28 +01:00
};
2022-12-06 19:45:38 +01:00
2017-03-16 20:56:28 +01:00
user = mkOption {
type = types.user;
default = {
name = "htgen-${config.name}";
home = "/var/lib/htgen-${config.name}";
};
2021-11-08 04:30:33 +01:00
defaultText = {
name = "htgen-name";
home = "/var/lib/htgen-name";
};
2017-03-16 20:56:28 +01: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 19:45:38 +01:00
}
// optionalAttr "HTGEN_SCRIPT" htgen.script
// optionalAttr "HTGEN_SCRIPT_FILE" htgen.scriptFile
;
2017-03-16 20:56:28 +01:00
serviceConfig = {
SyslogIdentifier = "htgen";
User = htgen.user.name;
PrivateTmp = true;
Restart = "always";
2021-02-02 22:28:25 +01:00
ExecStart = "${htgen.package}/bin/htgen --serve";
2017-03-16 20:56:28 +01:00
};
}
) cfg;
users.users = mapAttrs' (name: htgen:
nameValuePair htgen.user.name {
inherit (htgen.user) home name uid;
2021-12-01 17:55:22 +01:00
group = htgen.user.name;
2017-03-16 20:56:28 +01:00
createHome = true;
isSystemUser = true;
2017-03-16 20:56:28 +01:00
}
) cfg;
2022-01-03 14:56:44 +01:00
users.groups = mapAttrs' (name: htgen:
nameValuePair htgen.user.name {
name = htgen.user.name;
gid = htgen.user.uid;
}
) cfg;
2021-12-01 17:55:22 +01:00
2017-03-16 20:56:28 +01:00
};
in out