1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.slurm;
8 # configuration file can be generated by http://slurm.schedmd.com/configurator.html
9 configFile = pkgs.writeText "slurm.conf"
10 ''
11 ${optionalString (cfg.controlMachine != null) ''controlMachine=${cfg.controlMachine}''}
12 ${optionalString (cfg.controlAddr != null) ''controlAddr=${cfg.controlAddr}''}
13 ${optionalString (cfg.nodeName != null) ''nodeName=${cfg.nodeName}''}
14 ${optionalString (cfg.partitionName != null) ''partitionName=${cfg.partitionName}''}
15 PlugStackConfig=${plugStackConfig}
16 ${cfg.extraConfig}
17 '';
18
19 plugStackConfig = pkgs.writeText "plugstack.conf"
20 ''
21 ${optionalString cfg.enableSrunX11 ''optional ${pkgs.slurm-spank-x11}/lib/x11.so''}
22 '';
23in
24
25{
26
27 ###### interface
28
29 options = {
30
31 services.slurm = {
32
33 server = {
34 enable = mkEnableOption "slurm control daemon";
35
36 };
37
38 client = {
39 enable = mkEnableOption "slurm rlient daemon";
40
41 };
42
43 package = mkOption {
44 type = types.package;
45 default = pkgs.slurm;
46 defaultText = "pkgs.slurm";
47 example = literalExample "pkgs.slurm-full";
48 description = ''
49 The package to use for slurm binaries.
50 '';
51 };
52
53 controlMachine = mkOption {
54 type = types.nullOr types.str;
55 default = null;
56 example = null;
57 description = ''
58 The short hostname of the machine where SLURM control functions are
59 executed (i.e. the name returned by the command "hostname -s", use "tux001"
60 rather than "tux001.my.com").
61 '';
62 };
63
64 controlAddr = mkOption {
65 type = types.nullOr types.str;
66 default = cfg.controlMachine;
67 example = null;
68 description = ''
69 Name that ControlMachine should be referred to in establishing a
70 communications path.
71 '';
72 };
73
74 nodeName = mkOption {
75 type = types.nullOr types.str;
76 default = null;
77 example = "linux[1-32] CPUs=1 State=UNKNOWN";
78 description = ''
79 Name that SLURM uses to refer to a node (or base partition for BlueGene
80 systems). Typically this would be the string that "/bin/hostname -s"
81 returns. Note that now you have to write node's parameters after the name.
82 '';
83 };
84
85 partitionName = mkOption {
86 type = types.nullOr types.str;
87 default = null;
88 example = "debug Nodes=linux[1-32] Default=YES MaxTime=INFINITE State=UP";
89 description = ''
90 Name by which the partition may be referenced. Note that now you have
91 to write patrition's parameters after the name.
92 '';
93 };
94
95 enableSrunX11 = mkOption {
96 default = false;
97 type = types.bool;
98 description = ''
99 If enabled srun will accept the option "--x11" to allow for X11 forwarding
100 from within an interactive session or a batch job. This activates the
101 slurm-spank-x11 module. Note that this requires 'services.openssh.forwardX11'
102 to be enabled on the compute nodes.
103 '';
104 };
105
106 extraConfig = mkOption {
107 default = "";
108 type = types.lines;
109 description = ''
110 Extra configuration options that will be added verbatim at
111 the end of the slurm configuration file.
112 '';
113 };
114 };
115
116 };
117
118
119 ###### implementation
120
121 config =
122 let
123 wrappedSlurm = pkgs.stdenv.mkDerivation {
124 name = "wrappedSlurm";
125
126 propagatedBuildInputs = [ cfg.package configFile ];
127
128 builder = pkgs.writeText "builder.sh" ''
129 source $stdenv/setup
130 mkdir -p $out/bin
131 find ${getBin cfg.package}/bin -type f -executable | while read EXE
132 do
133 exename="$(basename $EXE)"
134 wrappername="$out/bin/$exename"
135 cat > "$wrappername" <<EOT
136 #!/bin/sh
137 if [ -z "$SLURM_CONF" ]
138 then
139 SLURM_CONF="${configFile}" "$EXE" "\$@"
140 else
141 "$EXE" "\$0"
142 fi
143 EOT
144 chmod +x "$wrappername"
145 done
146 '';
147 };
148
149 in mkIf (cfg.client.enable || cfg.server.enable) {
150
151 environment.systemPackages = [ wrappedSlurm ];
152
153 systemd.services.slurmd = mkIf (cfg.client.enable) {
154 path = with pkgs; [ wrappedSlurm coreutils ]
155 ++ lib.optional cfg.enableSrunX11 slurm-spank-x11;
156
157 wantedBy = [ "multi-user.target" ];
158 after = [ "systemd-tmpfiles-clean.service" ];
159
160 serviceConfig = {
161 Type = "forking";
162 ExecStart = "${wrappedSlurm}/bin/slurmd";
163 PIDFile = "/run/slurmd.pid";
164 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
165 };
166
167 preStart = ''
168 mkdir -p /var/spool
169 '';
170 };
171
172 systemd.services.slurmctld = mkIf (cfg.server.enable) {
173 path = with pkgs; [ wrappedSlurm munge coreutils ]
174 ++ lib.optional cfg.enableSrunX11 slurm-spank-x11;
175
176 wantedBy = [ "multi-user.target" ];
177 after = [ "network.target" "munged.service" ];
178 requires = [ "munged.service" ];
179
180 serviceConfig = {
181 Type = "forking";
182 ExecStart = "${wrappedSlurm}/bin/slurmctld";
183 PIDFile = "/run/slurmctld.pid";
184 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
185 };
186 };
187
188 };
189
190}