@@ -0,0 +1,126 @@
1+{ config, lib, pkgs, ... }:
2+with lib;
3+let
4+ cfg = config.services.nomad;
5+ format = pkgs.formats.json { };
6+in
7+{
8+ ##### interface
9+ options = {
10+ services.nomad = {
11+ enable = mkEnableOption "Nomad, a distributed, highly available, datacenter-aware scheduler";
12+
13+ package = mkOption {
14+ type = types.package;
15+ default = pkgs.nomad;
16+ defaultText = "pkgs.nomad";
17+ description = ''
18+ The package used for the Nomad agent and CLI.
19+ '';
20+ };
21+
22+ extraPackages = mkOption {
23+ type = types.listOf types.package;
24+ default = [ ];
25+ description = ''
26+ Extra packages to add to <envar>PATH</envar> for the Nomad agent process.
27+ '';
28+ example = literalExample ''
29+ with pkgs; [ cni-plugins ]
30+ '';
31+ };
32+
33+ dropPrivileges = mkOption {
34+ type = types.bool;
35+ default = true;
36+ description = ''
37+ Whether the nomad agent should be run as a non-root nomad user.
38+ '';
39+ };
40+
41+ enableDocker = mkOption {
42+ type = types.bool;
43+ default = true;
44+ description = ''
45+ Enable Docker support. Needed for Nomad's docker driver.
46+
47+ Note that the docker group membership is effectively equivalent
48+ to being root, see https://github.com/moby/moby/issues/9976.
49+ '';
50+ };
51+
52+ settings = mkOption {
53+ type = format.type;
54+ default = {
55+ # Agrees with `StateDirectory = "nomad"` set below.
56+ data_dir = "/var/lib/nomad";
57+ };
58+ description = ''
59+ Configuration for Nomad. See the <link xlink:href="https://www.nomadproject.io/docs/configuration">documentation</link>
60+ for supported values.
61+ '';
62+ example = literalExample ''
63+ {
64+ # A minimal config example:
65+ server = {
66+ enabled = true;
67+ bootstrap_expect = 1; # for demo; no fault tolerance
68+ };
69+ client = {
70+ enabled = true;
71+ };
72+ }
73+ '';
74+ };
75+ };
76+ };
77+
78+ ##### implementation
79+ config = mkIf cfg.enable {
80+ environment = {
81+ etc."nomad.json".source = format.generate "nomad.json" cfg.settings;
82+ systemPackages = [ cfg.package ];
83+ };
84+
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 ];
91+
92+ path = cfg.extraPackages ++ (with pkgs; [
93+ # Client mode requires at least the following:
94+ coreutils
95+ iproute
96+ iptables
97+ ]);
98+
99+ serviceConfig = {
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";
109+ RestartSec = 2;
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
116+ });
117+ unitConfig = {
118+ StartLimitIntervalSec = 10;
119+ StartLimitBurst = 3;
120+ };
121+ };
122+
123+ # Docker support requires the Docker daemon to be running.
124+ virtualisation.docker.enable = mkIf cfg.enableDocker true;
125+ };
126+}