7 checkService = checkUnitConfig "Service" [
8 (assertValueOneOf "Type" [
9 "exec" "simple" "forking" "oneshot" "dbus" "notify" "idle"
11 (assertValueOneOf "Restart" [
12 "no" "on-success" "on-failure" "on-abnormal" "on-abort" "always"
18 unitOption = mkOptionType {
19 name = "systemd option";
22 defs' = filterOverrides defs;
24 if isList (head defs').value
26 if builtins.typeOf def.value == "list"
29 throw "The definitions for systemd unit options should be either all lists, representing repeatable options, or all non-lists, but for the option ${showOption loc}, the definitions are a mix of list and non-list ${lib.options.showDefs defs'}"
32 else mergeEqualOption loc defs';
40 description = lib.mdDoc ''
41 If set to false, this unit will be a symlink to
42 /dev/null. This is primarily useful to prevent specific
44 (e.g. `serial-getty@ttyS0`) from being
45 started. Note that `enable=true` does not
46 make a unit start by default at boot; if you want that, see
51 overrideStrategy = mkOption {
52 default = "asDropinIfExists";
53 type = types.enum [ "asDropinIfExists" "asDropin" ];
54 description = lib.mdDoc ''
55 Defines how unit configuration is provided for systemd:
57 `asDropinIfExists` creates a unit file when no unit file is provided by the package
58 otherwise a drop-in file name `overrides.conf`.
60 `asDropin` creates a drop-in file named `overrides.conf`.
61 Mainly needed to define instances for systemd template units (e.g. `systemd-nspawn@mycontainer.service`).
63 See also {manpage}`systemd.unit(5)`.
67 requiredBy = mkOption {
69 type = types.listOf unitNameType;
70 description = lib.mdDoc ''
71 Units that require (i.e. depend on and need to go down with) this unit.
72 As discussed in the `wantedBy` option description this also creates
73 `.requires` symlinks automatically.
79 type = types.listOf unitNameType;
80 description = lib.mdDoc ''
81 Units that want (i.e. depend on) this unit. The default method for
82 starting a unit by default at boot time is to set this option to
83 `["multi-user.target"]` for system services. Likewise for user units
84 (`systemd.user.<name>.*`) set it to `["default.target"]` to make a unit
85 start by default when the user `<name>` logs on.
87 This option creates a `.wants` symlink in the given target that exists
88 statelessly without the need for running `systemctl enable`.
89 The `[Install]` section described in {manpage}`systemd.unit(5)` however is
90 not supported because it is a stateful process that does not fit well
91 into the NixOS design.
97 type = types.listOf unitNameType;
98 description = lib.mdDoc "Aliases of that unit.";
103 concreteUnitOptions = sharedOptions // {
106 type = types.nullOr types.str;
108 description = lib.mdDoc "Text of this systemd unit.";
113 description = lib.mdDoc "The generated unit.";
118 commonUnitOptions = {
119 options = sharedOptions // {
121 description = mkOption {
123 type = types.singleLineStr;
124 description = lib.mdDoc "Description of this unit used in systemd messages and progress indicators.";
127 documentation = mkOption {
129 type = types.listOf types.str;
130 description = lib.mdDoc "A list of URIs referencing documentation for this unit or its configuration.";
133 requires = mkOption {
135 type = types.listOf unitNameType;
136 description = lib.mdDoc ''
137 Start the specified units when this unit is started, and stop
138 this unit when the specified units are stopped or fail.
144 type = types.listOf unitNameType;
145 description = lib.mdDoc ''
146 Start the specified units when this unit is started.
152 type = types.listOf unitNameType;
153 description = lib.mdDoc ''
154 If the specified units are started at the same time as
155 this unit, delay this unit until they have started.
161 type = types.listOf unitNameType;
162 description = lib.mdDoc ''
163 If the specified units are started at the same time as
164 this unit, delay them until this unit has started.
170 type = types.listOf unitNameType;
171 description = lib.mdDoc ''
172 Like ‘requires’, but in addition, if the specified units
173 unexpectedly disappear, this unit will be stopped as well.
179 type = types.listOf unitNameType;
180 description = lib.mdDoc ''
181 If the specified units are stopped or restarted, then this
182 unit is stopped or restarted as well.
186 conflicts = mkOption {
188 type = types.listOf unitNameType;
189 description = lib.mdDoc ''
190 If the specified units are started, then this unit is stopped
195 requisite = mkOption {
197 type = types.listOf unitNameType;
198 description = lib.mdDoc ''
199 Similar to requires. However if the units listed are not started,
200 they will not be started and the transaction will fail.
204 unitConfig = mkOption {
206 example = { RequiresMountsFor = "/data"; };
207 type = types.attrsOf unitOption;
208 description = lib.mdDoc ''
209 Each attribute in this set specifies an option in the
210 `[Unit]` section of the unit. See
211 {manpage}`systemd.unit(5)` for details.
215 onFailure = mkOption {
217 type = types.listOf unitNameType;
218 description = lib.mdDoc ''
219 A list of one or more units that are activated when
220 this unit enters the "failed" state.
224 onSuccess = mkOption {
226 type = types.listOf unitNameType;
227 description = lib.mdDoc ''
228 A list of one or more units that are activated when
229 this unit enters the "inactive" state.
233 startLimitBurst = mkOption {
235 description = lib.mdDoc ''
236 Configure unit start rate limiting. Units which are started
237 more than startLimitBurst times within an interval time
238 interval are not permitted to start any more.
242 startLimitIntervalSec = mkOption {
244 description = lib.mdDoc ''
245 Configure unit start rate limiting. Units which are started
246 more than startLimitBurst times within an interval time
247 interval are not permitted to start any more.
254 stage2CommonUnitOptions = {
260 restartTriggers = mkOption {
262 type = types.listOf types.unspecified;
263 description = lib.mdDoc ''
264 An arbitrary list of items such as derivations. If any item
265 in the list changes between reconfigurations, the service will
270 reloadTriggers = mkOption {
272 type = types.listOf unitOption;
273 description = lib.mdDoc ''
274 An arbitrary list of items such as derivations. If any item
275 in the list changes between reconfigurations, the service will
276 be reloaded. If anything but a reload trigger changes in the
277 unit file, the unit will be restarted instead.
282 stage1CommonUnitOptions = commonUnitOptions;
284 serviceOptions = { name, config, ... }: {
287 environment = mkOption {
289 type = with types; attrsOf (nullOr (oneOf [ str path package ]));
290 example = { PATH = "/foo/bar/bin"; LANG = "nl_NL.UTF-8"; };
291 description = lib.mdDoc "Environment variables passed to the service's processes.";
296 type = with types; listOf (oneOf [ package str ]);
297 description = lib.mdDoc ''
298 Packages added to the service's {env}`PATH`
299 environment variable. Both the {file}`bin`
300 and {file}`sbin` subdirectories of each
305 serviceConfig = mkOption {
310 type = types.addCheck (types.attrsOf unitOption) checkService;
311 description = lib.mdDoc ''
312 Each attribute in this set specifies an option in the
313 `[Service]` section of the unit. See
314 {manpage}`systemd.service(5)` for details.
321 description = lib.mdDoc "Shell commands executed as the service's main process.";
324 scriptArgs = mkOption {
328 description = lib.mdDoc ''
329 Arguments passed to the main process script.
330 Can contain specifiers (`%` placeholders expanded by systemd, see {manpage}`systemd.unit(5)`).
334 preStart = mkOption {
337 description = lib.mdDoc ''
338 Shell commands executed before the service's main process
343 postStart = mkOption {
346 description = lib.mdDoc ''
347 Shell commands executed after the service's main process
355 description = lib.mdDoc ''
356 Shell commands executed when the service's main process
364 description = lib.mdDoc ''
365 Shell commands executed to stop the service.
369 postStop = mkOption {
372 description = lib.mdDoc ''
373 Shell commands executed after the service's main process
378 jobScripts = mkOption {
379 type = with types; coercedTo path singleton (listOf path);
381 description = lib.mdDoc "A list of all job script derivations of this unit.";
388 (mkIf (config.preStart != "") rec {
389 jobScripts = makeJobScript "${name}-pre-start" config.preStart;
390 serviceConfig.ExecStartPre = [ jobScripts ];
392 (mkIf (config.script != "") rec {
393 jobScripts = makeJobScript "${name}-start" config.script;
394 serviceConfig.ExecStart = jobScripts + " " + config.scriptArgs;
396 (mkIf (config.postStart != "") rec {
397 jobScripts = (makeJobScript "${name}-post-start" config.postStart);
398 serviceConfig.ExecStartPost = [ jobScripts ];
400 (mkIf (config.reload != "") rec {
401 jobScripts = makeJobScript "${name}-reload" config.reload;
402 serviceConfig.ExecReload = jobScripts;
404 (mkIf (config.preStop != "") rec {
405 jobScripts = makeJobScript "${name}-pre-stop" config.preStop;
406 serviceConfig.ExecStop = jobScripts;
408 (mkIf (config.postStop != "") rec {
409 jobScripts = makeJobScript "${name}-post-stop" config.postStop;
410 serviceConfig.ExecStopPost = jobScripts;
416 stage2ServiceOptions = {
418 stage2CommonUnitOptions
423 restartIfChanged = mkOption {
426 description = lib.mdDoc ''
427 Whether the service should be restarted during a NixOS
428 configuration switch if its definition has changed.
432 reloadIfChanged = mkOption {
435 description = lib.mdDoc ''
436 Whether the service should be reloaded during a NixOS
437 configuration switch if its definition has changed. If
438 enabled, the value of {option}`restartIfChanged` is
441 This option should not be used anymore in favor of
442 {option}`reloadTriggers` which allows more granular
443 control of when a service is reloaded and when a service
448 stopIfChanged = mkOption {
451 description = lib.mdDoc ''
452 If set, a changed unit is restarted by calling
453 {command}`systemctl stop` in the old configuration,
454 then {command}`systemctl start` in the new one.
455 Otherwise, it is restarted in a single step using
456 {command}`systemctl restart` in the new configuration.
457 The latter is less correct because it runs the
458 `ExecStop` commands from the new
464 type = with types; either str (listOf str);
466 example = "Sun 14:00:00";
467 description = lib.mdDoc ''
468 Automatically start this unit at the given date/time, which
469 must be in the format described in
470 {manpage}`systemd.time(7)`. This is equivalent
471 to adding a corresponding timer unit with
472 {option}`OnCalendar` set to the value given here.
474 apply = v: if isList v then v else [ v ];
479 stage1ServiceOptions = {
481 stage1CommonUnitOptions
490 listenStreams = mkOption {
492 type = types.listOf types.str;
493 example = [ "0.0.0.0:993" "/run/my-socket" ];
494 description = lib.mdDoc ''
495 For each item in this list, a `ListenStream`
496 option in the `[Socket]` section will be created.
500 listenDatagrams = mkOption {
502 type = types.listOf types.str;
503 example = [ "0.0.0.0:993" "/run/my-socket" ];
504 description = lib.mdDoc ''
505 For each item in this list, a `ListenDatagram`
506 option in the `[Socket]` section will be created.
510 socketConfig = mkOption {
512 example = { ListenStream = "/run/my-socket"; };
513 type = types.attrsOf unitOption;
514 description = lib.mdDoc ''
515 Each attribute in this set specifies an option in the
516 `[Socket]` section of the unit. See
517 {manpage}`systemd.socket(5)` for details.
524 stage2SocketOptions = {
526 stage2CommonUnitOptions
531 stage1SocketOptions = {
533 stage1CommonUnitOptions
542 timerConfig = mkOption {
544 example = { OnCalendar = "Sun 14:00:00"; Unit = "foo.service"; };
545 type = types.attrsOf unitOption;
546 description = lib.mdDoc ''
547 Each attribute in this set specifies an option in the
548 `[Timer]` section of the unit. See
549 {manpage}`systemd.timer(5)` and
550 {manpage}`systemd.time(7)` for details.
557 stage2TimerOptions = {
559 stage2CommonUnitOptions
564 stage1TimerOptions = {
566 stage1CommonUnitOptions
575 pathConfig = mkOption {
577 example = { PathChanged = "/some/path"; Unit = "changedpath.service"; };
578 type = types.attrsOf unitOption;
579 description = lib.mdDoc ''
580 Each attribute in this set specifies an option in the
581 `[Path]` section of the unit. See
582 {manpage}`systemd.path(5)` for details.
589 stage2PathOptions = {
591 stage2CommonUnitOptions
596 stage1PathOptions = {
598 stage1CommonUnitOptions
608 example = "/dev/sda1";
610 description = lib.mdDoc "Absolute path of device node, file or other resource. (Mandatory)";
616 description = lib.mdDoc ''
617 Absolute path of a directory of the mount point.
618 Will be created if it doesn't exist. (Mandatory)
626 description = lib.mdDoc "File system type.";
633 description = lib.mdDoc "Options used to mount the file system.";
636 mountConfig = mkOption {
638 example = { DirectoryMode = "0775"; };
639 type = types.attrsOf unitOption;
640 description = lib.mdDoc ''
641 Each attribute in this set specifies an option in the
642 `[Mount]` section of the unit. See
643 {manpage}`systemd.mount(5)` for details.
650 stage2MountOptions = {
652 stage2CommonUnitOptions
657 stage1MountOptions = {
659 stage1CommonUnitOptions
670 description = lib.mdDoc ''
671 Absolute path of a directory of the mount point.
672 Will be created if it doesn't exist. (Mandatory)
676 automountConfig = mkOption {
678 example = { DirectoryMode = "0775"; };
679 type = types.attrsOf unitOption;
680 description = lib.mdDoc ''
681 Each attribute in this set specifies an option in the
682 `[Automount]` section of the unit. See
683 {manpage}`systemd.automount(5)` for details.
690 stage2AutomountOptions = {
692 stage2CommonUnitOptions
697 stage1AutomountOptions = {
699 stage1CommonUnitOptions
707 sliceConfig = mkOption {
709 example = { MemoryMax = "2G"; };
710 type = types.attrsOf unitOption;
711 description = lib.mdDoc ''
712 Each attribute in this set specifies an option in the
713 `[Slice]` section of the unit. See
714 {manpage}`systemd.slice(5)` for details.
721 stage2SliceOptions = {
723 stage2CommonUnitOptions
728 stage1SliceOptions = {
730 stage1CommonUnitOptions