@@ -0,0 +1,126 @@
1+{ config, lib, pkgs, ... }:
4+ cfg = config.services.nomad;
5+ format = pkgs.formats.json { };
11+ enable = mkEnableOption "Nomad, a distributed, highly available, datacenter-aware scheduler";
14+ type = types.package;
15+ default = pkgs.nomad;
16+ defaultText = "pkgs.nomad";
18+ The package used for the Nomad agent and CLI.
22+ extraPackages = mkOption {
23+ type = types.listOf types.package;
26+ Extra packages to add to <envar>PATH</envar> for the Nomad agent process.
28+ example = literalExample ''
29+ with pkgs; [ cni-plugins ]
33+ dropPrivileges = mkOption {
37+ Whether the nomad agent should be run as a non-root nomad user.
41+ enableDocker = mkOption {
45+ Enable Docker support. Needed for Nomad's docker driver.
47+ Note that the docker group membership is effectively equivalent
48+ to being root, see https://github.com/moby/moby/issues/9976.
52+ settings = mkOption {
55+ # Agrees with `StateDirectory = "nomad"` set below.
56+ data_dir = "/var/lib/nomad";
59+ Configuration for Nomad. See the <link xlink:href="https://www.nomadproject.io/docs/configuration">documentation</link>
60+ for supported values.
62+ example = literalExample ''
64+ # A minimal config example:
67+ bootstrap_expect = 1; # for demo; no fault tolerance
79+ config = mkIf cfg.enable {
81+ etc."nomad.json".source = format.generate "nomad.json" cfg.settings;
82+ systemPackages = [ cfg.package ];
85+ systemd.services.nomad = {
86+ description = "Nomad";
87+ wantedBy = [ "multi-user.target" ];
88+ wants = [ "network-online.target" ];
89+ after = [ "network-online.target" ];
90+ restartTriggers = [ config.environment.etc."nomad.json".source ];
92+ path = cfg.extraPackages ++ (with pkgs; [
93+ # Client mode requires at least the following:
100+ DynamicUser = cfg.dropPrivileges;
101+ ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
102+ ExecStart = "${cfg.package}/bin/nomad agent -config=/etc/nomad.json";
103+ KillMode = "process";
104+ KillSignal = "SIGINT";
105+ LimitNOFILE = 65536;
106+ LimitNPROC = "infinity";
107+ OOMScoreAdjust = -1000;
108+ Restart = "on-failure";
110+ # Agrees with the default `data_dir = "/var/lib/nomad"` in `settings` above.
111+ StateDirectory = "nomad";
112+ TasksMax = "infinity";
113+ User = optionalString cfg.dropPrivileges "nomad";
114+ } // (optionalAttrs cfg.enableDocker {
115+ SupplementaryGroups = "docker"; # space-separated string
118+ StartLimitIntervalSec = 10;
119+ StartLimitBurst = 3;
123+ # Docker support requires the Docker daemon to be running.
124+ virtualisation.docker.enable = mkIf cfg.enableDocker true;