2021-02-05 17:12:51 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.nomad;
|
|
|
|
format = pkgs.formats.json { };
|
|
|
|
in
|
|
|
|
{
|
|
|
|
##### interface
|
|
|
|
options = {
|
|
|
|
services.nomad = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "Nomad, a distributed, highly available, datacenter-aware scheduler");
|
2021-02-05 17:12:51 +00:00
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
package = mkPackageOption pkgs "nomad" { };
|
2021-02-05 17:12:51 +00:00
|
|
|
|
|
|
|
extraPackages = mkOption {
|
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [ ];
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Extra packages to add to {env}`PATH` for the Nomad agent process.
|
2021-02-05 17:12:51 +00:00
|
|
|
'';
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression ''
|
2021-02-05 17:12:51 +00:00
|
|
|
with pkgs; [ cni-plugins ]
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dropPrivileges = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-02-05 17:12:51 +00:00
|
|
|
Whether the nomad agent should be run as a non-root nomad user.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
enableDocker = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-02-05 17:12:51 +00:00
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraSettingsPaths = mkOption {
|
|
|
|
type = types.listOf types.path;
|
2021-12-06 16:07:01 +00:00
|
|
|
default = [ ];
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-02-05 17:12:51 +00:00
|
|
|
Additional settings paths used to configure nomad. These can be files or directories.
|
|
|
|
'';
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression ''
|
2021-02-05 17:12:51 +00:00
|
|
|
[ "/etc/nomad-mutable.json" "/run/keys/nomad-with-secrets.json" "/etc/nomad/config.d" ]
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
extraSettingsPlugins = mkOption {
|
|
|
|
type = types.listOf (types.either types.package types.path);
|
|
|
|
default = [ ];
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-12-06 16:07:01 +00:00
|
|
|
Additional plugins dir used to configure nomad.
|
|
|
|
'';
|
|
|
|
example = literalExpression ''
|
2023-01-20 10:41:00 +00:00
|
|
|
[ "<pluginDir>" pkgs.nomad-driver-nix pkgs.nomad-driver-podman ]
|
2021-12-06 16:07:01 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-02-02 18:25:31 +00:00
|
|
|
credentials = mkOption {
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Credentials envs used to configure nomad secrets.
|
|
|
|
'';
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
default = { };
|
|
|
|
|
|
|
|
example = {
|
|
|
|
logs_remote_write_password = "/run/keys/nomad_write_password";
|
|
|
|
};
|
|
|
|
};
|
2021-12-06 16:07:01 +00:00
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
settings = mkOption {
|
|
|
|
type = format.type;
|
2021-12-06 16:07:01 +00:00
|
|
|
default = { };
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Configuration for Nomad. See the [documentation](https://www.nomadproject.io/docs/configuration)
|
2021-02-05 17:12:51 +00:00
|
|
|
for supported values.
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
Notes about `data_dir`:
|
2021-02-05 17:12:51 +00:00
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
If `data_dir` is set to a value other than the
|
|
|
|
default value of `"/var/lib/nomad"` it is the Nomad
|
2021-02-05 17:12:51 +00:00
|
|
|
cluster manager's responsibility to make sure that this directory
|
|
|
|
exists and has the appropriate permissions.
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
Additionally, if `dropPrivileges` is
|
|
|
|
`true` then `data_dir`
|
|
|
|
*cannot* be customized. Setting
|
|
|
|
`dropPrivileges` to `true` enables
|
|
|
|
the `DynamicUser` feature of systemd which directly
|
|
|
|
manages and operates on `StateDirectory`.
|
2021-02-05 17:12:51 +00:00
|
|
|
'';
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression ''
|
2021-02-05 17:12:51 +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 {
|
|
|
|
services.nomad.settings = {
|
|
|
|
# Agrees with `StateDirectory = "nomad"` set below.
|
|
|
|
data_dir = mkDefault "/var/lib/nomad";
|
|
|
|
};
|
|
|
|
|
|
|
|
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 16:26:57 +00:00
|
|
|
iproute2
|
2021-02-05 17:12:51 +00:00
|
|
|
iptables
|
|
|
|
]);
|
|
|
|
|
|
|
|
serviceConfig = mkMerge [
|
|
|
|
{
|
|
|
|
DynamicUser = cfg.dropPrivileges;
|
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
2023-01-20 10:41:00 +00:00
|
|
|
ExecStart =
|
|
|
|
let
|
|
|
|
pluginsDir = pkgs.symlinkJoin
|
|
|
|
{
|
|
|
|
name = "nomad-plugins";
|
|
|
|
paths = cfg.extraSettingsPlugins;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
"${cfg.package}/bin/nomad agent -config=/etc/nomad.json -plugin-dir=${pluginsDir}/bin" +
|
2023-02-02 18:25:31 +00:00
|
|
|
concatMapStrings (path: " -config=${path}") cfg.extraSettingsPaths +
|
|
|
|
concatMapStrings (key: " -config=\${CREDENTIALS_DIRECTORY}/${key}") (lib.attrNames cfg.credentials);
|
2021-02-05 17:12:51 +00:00
|
|
|
KillMode = "process";
|
|
|
|
KillSignal = "SIGINT";
|
|
|
|
LimitNOFILE = 65536;
|
|
|
|
LimitNPROC = "infinity";
|
|
|
|
OOMScoreAdjust = -1000;
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = 2;
|
|
|
|
TasksMax = "infinity";
|
2023-02-02 18:25:31 +00:00
|
|
|
LoadCredential = lib.mapAttrsToList (key: value: "${key}:${value}") cfg.credentials;
|
2021-02-05 17:12:51 +00:00
|
|
|
}
|
|
|
|
(mkIf cfg.enableDocker {
|
|
|
|
SupplementaryGroups = "docker"; # space-separated string
|
|
|
|
})
|
|
|
|
(mkIf (cfg.settings.data_dir == "/var/lib/nomad") {
|
|
|
|
StateDirectory = "nomad";
|
|
|
|
})
|
|
|
|
];
|
|
|
|
|
|
|
|
unitConfig = {
|
|
|
|
StartLimitIntervalSec = 10;
|
|
|
|
StartLimitBurst = 3;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
# Docker support requires the Docker daemon to be running.
|
|
|
|
virtualisation.docker.enable = mkIf cfg.enableDocker true;
|
|
|
|
};
|
|
|
|
}
|