9 cfg = config.services.nomad;
10 format = pkgs.formats.json { };
16 enable = mkEnableOption "Nomad, a distributed, highly available, datacenter-aware scheduler";
18 package = mkPackageOption pkgs "nomad" { };
20 extraPackages = mkOption {
21 type = types.listOf types.package;
24 Extra packages to add to {env}`PATH` for the Nomad agent process.
26 example = literalExpression ''
27 with pkgs; [ cni-plugins ]
31 dropPrivileges = mkOption {
35 Whether the nomad agent should be run as a non-root nomad user.
39 enableDocker = mkOption {
43 Enable Docker support. Needed for Nomad's docker driver.
45 Note that the docker group membership is effectively equivalent
46 to being root, see https://github.com/moby/moby/issues/9976.
50 extraSettingsPaths = mkOption {
51 type = types.listOf types.path;
54 Additional settings paths used to configure nomad. These can be files or directories.
56 example = literalExpression ''
57 [ "/etc/nomad-mutable.json" "/run/keys/nomad-with-secrets.json" "/etc/nomad/config.d" ]
61 extraSettingsPlugins = mkOption {
62 type = types.listOf (types.either types.package types.path);
65 Additional plugins dir used to configure nomad.
67 example = literalExpression ''
68 [ "<pluginDir>" pkgs.nomad-driver-nix pkgs.nomad-driver-podman ]
72 credentials = mkOption {
74 Credentials envs used to configure nomad secrets.
76 type = types.attrsOf types.str;
80 logs_remote_write_password = "/run/keys/nomad_write_password";
88 Configuration for Nomad. See the [documentation](https://www.nomadproject.io/docs/configuration)
91 Notes about `data_dir`:
93 If `data_dir` is set to a value other than the
94 default value of `"/var/lib/nomad"` it is the Nomad
95 cluster manager's responsibility to make sure that this directory
96 exists and has the appropriate permissions.
98 Additionally, if `dropPrivileges` is
99 `true` then `data_dir`
100 *cannot* be customized. Setting
101 `dropPrivileges` to `true` enables
102 the `DynamicUser` feature of systemd which directly
103 manages and operates on `StateDirectory`.
105 example = literalExpression ''
107 # A minimal config example:
110 bootstrap_expect = 1; # for demo; no fault tolerance
122 config = mkIf cfg.enable {
123 services.nomad.settings = {
124 # Agrees with `StateDirectory = "nomad"` set below.
125 data_dir = mkDefault "/var/lib/nomad";
129 etc."nomad.json".source = format.generate "nomad.json" cfg.settings;
130 systemPackages = [ cfg.package ];
133 systemd.services.nomad = {
134 description = "Nomad";
135 wantedBy = [ "multi-user.target" ];
136 wants = [ "network-online.target" ];
137 after = [ "network-online.target" ];
138 restartTriggers = [ config.environment.etc."nomad.json".source ];
143 # Client mode requires at least the following:
149 serviceConfig = mkMerge [
151 DynamicUser = cfg.dropPrivileges;
152 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
155 pluginsDir = pkgs.symlinkJoin {
156 name = "nomad-plugins";
157 paths = cfg.extraSettingsPlugins;
160 "${cfg.package}/bin/nomad agent -config=/etc/nomad.json -plugin-dir=${pluginsDir}/bin"
161 + concatMapStrings (path: " -config=${path}") cfg.extraSettingsPaths
162 + concatMapStrings (key: " -config=\${CREDENTIALS_DIRECTORY}/${key}") (
163 lib.attrNames cfg.credentials
165 KillMode = "process";
166 KillSignal = "SIGINT";
168 LimitNPROC = "infinity";
169 OOMScoreAdjust = -1000;
170 Restart = "on-failure";
172 TasksMax = "infinity";
173 LoadCredential = lib.mapAttrsToList (key: value: "${key}:${value}") cfg.credentials;
175 (mkIf cfg.enableDocker {
176 SupplementaryGroups = "docker"; # space-separated string
178 (mkIf (cfg.settings.data_dir == "/var/lib/nomad") {
179 StateDirectory = "nomad";
184 StartLimitIntervalSec = 10;
191 assertion = cfg.dropPrivileges -> cfg.settings.data_dir == "/var/lib/nomad";
192 message = "settings.data_dir must be equal to \"/var/lib/nomad\" if dropPrivileges is true";
196 # Docker support requires the Docker daemon to be running.
197 virtualisation.docker.enable = mkIf cfg.enableDocker true;