2020-12-03 01:05:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.nomad;
|
|
|
|
format = pkgs.formats.json { };
|
|
|
|
in
|
|
|
|
{
|
|
|
|
##### interface
|
|
|
|
options = {
|
|
|
|
services.nomad = {
|
|
|
|
enable = mkEnableOption "Nomad, a distributed, highly available, datacenter-aware scheduler";
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.nomad;
|
2021-10-03 17:06:03 +01:00
|
|
|
defaultText = literalExpression "pkgs.nomad";
|
2020-12-03 01:05:48 +00:00
|
|
|
description = ''
|
|
|
|
The package used for the Nomad agent and CLI.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraPackages = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [ ];
|
|
|
|
description = ''
|
|
|
|
Extra packages to add to <envar>PATH</envar> for the Nomad agent process.
|
|
|
|
'';
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''
|
2020-12-03 01:05:48 +00:00
|
|
|
with pkgs; [ cni-plugins ]
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dropPrivileges = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether the nomad agent should be run as a non-root nomad user.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
enableDocker = mkOption {
|
|
|
|
type = types.bool;
|
2021-01-18 02:54:06 +00:00
|
|
|
default = true;
|
2020-12-03 01:05:48 +00:00
|
|
|
description = ''
|
|
|
|
Enable Docker support. Needed for Nomad's docker driver.
|
|
|
|
|
|
|
|
Note that the docker group membership is effectively equivalent
|
|
|
|
to being root, see https://github.com/moby/moby/issues/9976.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-01-23 22:52:19 +00:00
|
|
|
extraSettingsPaths = mkOption {
|
|
|
|
type = types.listOf types.path;
|
2021-11-17 23:42:05 +00:00
|
|
|
default = [ ];
|
2021-01-23 22:52:19 +00:00
|
|
|
description = ''
|
|
|
|
Additional settings paths used to configure nomad. These can be files or directories.
|
|
|
|
'';
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''
|
2021-01-23 22:52:19 +00:00
|
|
|
[ "/etc/nomad-mutable.json" "/run/keys/nomad-with-secrets.json" "/etc/nomad/config.d" ]
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-11-17 23:42:05 +00:00
|
|
|
extraSettingsPlugins = mkOption {
|
|
|
|
type = types.listOf (types.either types.package types.path);
|
|
|
|
default = [ ];
|
|
|
|
description = ''
|
|
|
|
Additional plugins dir used to configure nomad.
|
|
|
|
'';
|
|
|
|
example = literalExpression ''
|
|
|
|
[ "<pluginDir>" "pkgs.<plugins-name>"]
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-12-03 01:05:48 +00:00
|
|
|
settings = mkOption {
|
|
|
|
type = format.type;
|
2021-11-17 23:42:05 +00:00
|
|
|
default = { };
|
2020-12-03 01:05:48 +00:00
|
|
|
description = ''
|
|
|
|
Configuration for Nomad. See the <link xlink:href="https://www.nomadproject.io/docs/configuration">documentation</link>
|
|
|
|
for supported values.
|
2021-01-18 14:03:52 +00:00
|
|
|
|
2021-01-19 12:21:31 +00:00
|
|
|
Notes about <literal>data_dir</literal>:
|
2021-01-18 14:03:52 +00:00
|
|
|
|
2021-01-19 12:21:31 +00:00
|
|
|
If <literal>data_dir</literal> is set to a value other than the
|
|
|
|
default value of <literal>"/var/lib/nomad"</literal> it is the Nomad
|
|
|
|
cluster manager's responsibility to make sure that this directory
|
|
|
|
exists and has the appropriate permissions.
|
2021-01-18 14:03:52 +00:00
|
|
|
|
2021-01-19 12:21:31 +00:00
|
|
|
Additionally, if <literal>dropPrivileges</literal> is
|
|
|
|
<literal>true</literal> then <literal>data_dir</literal>
|
|
|
|
<emphasis>cannot</emphasis> be customized. Setting
|
|
|
|
<literal>dropPrivileges</literal> to <literal>true</literal> enables
|
|
|
|
the <literal>DynamicUser</literal> feature of systemd which directly
|
|
|
|
manages and operates on <literal>StateDirectory</literal>.
|
2020-12-03 01:05:48 +00:00
|
|
|
'';
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''
|
2020-12-03 01:05:48 +00:00
|
|
|
{
|
|
|
|
# A minimal config example:
|
|
|
|
server = {
|
|
|
|
enabled = true;
|
|
|
|
bootstrap_expect = 1; # for demo; no fault tolerance
|
|
|
|
};
|
|
|
|
client = {
|
|
|
|
enabled = true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
##### implementation
|
|
|
|
config = mkIf cfg.enable {
|
2021-01-23 17:44:23 +00:00
|
|
|
services.nomad.settings = {
|
|
|
|
# Agrees with `StateDirectory = "nomad"` set below.
|
|
|
|
data_dir = mkDefault "/var/lib/nomad";
|
|
|
|
};
|
|
|
|
|
2020-12-03 01:05:48 +00:00
|
|
|
environment = {
|
|
|
|
etc."nomad.json".source = format.generate "nomad.json" cfg.settings;
|
|
|
|
systemPackages = [ cfg.package ];
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.nomad = {
|
|
|
|
description = "Nomad";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
restartTriggers = [ config.environment.etc."nomad.json".source ];
|
|
|
|
|
|
|
|
path = cfg.extraPackages ++ (with pkgs; [
|
|
|
|
# Client mode requires at least the following:
|
|
|
|
coreutils
|
2021-04-08 12:33:09 +01:00
|
|
|
iproute2
|
2020-12-03 01:05:48 +00:00
|
|
|
iptables
|
|
|
|
]);
|
|
|
|
|
2021-01-18 14:05:59 +00:00
|
|
|
serviceConfig = mkMerge [
|
|
|
|
{
|
|
|
|
DynamicUser = cfg.dropPrivileges;
|
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
|
|
|
ExecStart = "${cfg.package}/bin/nomad agent -config=/etc/nomad.json" +
|
2021-11-17 23:42:05 +00:00
|
|
|
concatMapStrings (path: " -config=${path}") cfg.extraSettingsPaths +
|
|
|
|
concatMapStrings (path: " -plugin-dir=${path}/bin") cfg.extraSettingsPlugins;
|
2021-01-18 14:05:59 +00:00
|
|
|
KillMode = "process";
|
|
|
|
KillSignal = "SIGINT";
|
|
|
|
LimitNOFILE = 65536;
|
|
|
|
LimitNPROC = "infinity";
|
|
|
|
OOMScoreAdjust = -1000;
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = 2;
|
|
|
|
TasksMax = "infinity";
|
|
|
|
}
|
2021-01-18 14:07:56 +00:00
|
|
|
(mkIf cfg.enableDocker {
|
|
|
|
SupplementaryGroups = "docker"; # space-separated string
|
|
|
|
})
|
2021-01-23 23:04:26 +00:00
|
|
|
(mkIf (cfg.settings.data_dir == "/var/lib/nomad") {
|
|
|
|
StateDirectory = "nomad";
|
|
|
|
})
|
2021-01-18 14:05:59 +00:00
|
|
|
];
|
2021-01-23 17:43:43 +00:00
|
|
|
|
2020-12-03 01:05:48 +00:00
|
|
|
unitConfig = {
|
|
|
|
StartLimitIntervalSec = 10;
|
|
|
|
StartLimitBurst = 3;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-01-18 14:02:05 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.dropPrivileges -> cfg.settings.data_dir == "/var/lib/nomad";
|
|
|
|
message = "settings.data_dir must be equal to \"/var/lib/nomad\" if dropPrivileges is true";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2020-12-03 01:05:48 +00:00
|
|
|
# Docker support requires the Docker daemon to be running.
|
|
|
|
virtualisation.docker.enable = mkIf cfg.enableDocker true;
|
|
|
|
};
|
|
|
|
}
|