4 inherit (systemdUtils.lib)
31 checkService = checkUnitConfig "Service" [
32 (assertValueOneOf "Type" [
33 "exec" "simple" "forking" "oneshot" "dbus" "notify" "notify-reload" "idle"
35 (assertValueOneOf "Restart" [
36 "no" "on-success" "on-failure" "on-abnormal" "on-abort" "always"
42 unitOption = mkOptionType {
43 name = "systemd option";
46 defs' = filterOverrides defs;
48 if any (def: isList def.value) defs'
49 then concatMap (def: toList def.value) defs'
50 else mergeEqualOption loc defs';
59 If set to false, this unit will be a symlink to
60 /dev/null. This is primarily useful to prevent specific
62 (e.g. `serial-getty@ttyS0`) from being
63 started. Note that `enable=true` does not
64 make a unit start by default at boot; if you want that, see
72 The name of this systemd unit, including its extension.
73 This can be used to refer to this unit from other systemd units.
77 overrideStrategy = mkOption {
78 default = "asDropinIfExists";
79 type = types.enum [ "asDropinIfExists" "asDropin" ];
81 Defines how unit configuration is provided for systemd:
83 `asDropinIfExists` creates a unit file when no unit file is provided by the package
84 otherwise a drop-in file name `overrides.conf`.
86 `asDropin` creates a drop-in file named `overrides.conf`.
87 Mainly needed to define instances for systemd template units (e.g. `systemd-nspawn@mycontainer.service`).
89 See also {manpage}`systemd.unit(5)`.
93 requiredBy = mkOption {
95 type = types.listOf unitNameType;
97 Units that require (i.e. depend on and need to go down with) this unit.
98 As discussed in the `wantedBy` option description this also creates
99 `.requires` symlinks automatically.
103 upheldBy = mkOption {
105 type = types.listOf unitNameType;
107 Keep this unit running as long as the listed units are running. This is a continuously
108 enforced version of wantedBy.
112 wantedBy = mkOption {
114 type = types.listOf unitNameType;
116 Units that want (i.e. depend on) this unit. The default method for
117 starting a unit by default at boot time is to set this option to
118 `["multi-user.target"]` for system services. Likewise for user units
119 (`systemd.user.<name>.*`) set it to `["default.target"]` to make a unit
120 start by default when the user `<name>` logs on.
122 This option creates a `.wants` symlink in the given target that exists
123 statelessly without the need for running `systemctl enable`.
124 The `[Install]` section described in {manpage}`systemd.unit(5)` however is
125 not supported because it is a stateful process that does not fit well
126 into the NixOS design.
132 type = types.listOf unitNameType;
133 description = "Aliases of that unit.";
138 concreteUnitOptions = sharedOptions // {
141 type = types.nullOr types.str;
143 description = "Text of this systemd unit.";
148 description = "The generated unit.";
153 commonUnitOptions = {
154 options = sharedOptions // {
156 description = mkOption {
158 type = types.singleLineStr;
159 description = "Description of this unit used in systemd messages and progress indicators.";
162 documentation = mkOption {
164 type = types.listOf types.str;
165 description = "A list of URIs referencing documentation for this unit or its configuration.";
168 requires = mkOption {
170 type = types.listOf unitNameType;
172 Start the specified units when this unit is started, and stop
173 this unit when the specified units are stopped or fail.
179 type = types.listOf unitNameType;
181 Start the specified units when this unit is started.
187 type = types.listOf unitNameType;
189 Keeps the specified running while this unit is running. A continuous version of `wants`.
195 type = types.listOf unitNameType;
197 If the specified units are started at the same time as
198 this unit, delay this unit until they have started.
204 type = types.listOf unitNameType;
206 If the specified units are started at the same time as
207 this unit, delay them until this unit has started.
213 type = types.listOf unitNameType;
215 Like ‘requires’, but in addition, if the specified units
216 unexpectedly disappear, this unit will be stopped as well.
222 type = types.listOf unitNameType;
224 If the specified units are stopped or restarted, then this
225 unit is stopped or restarted as well.
229 conflicts = mkOption {
231 type = types.listOf unitNameType;
233 If the specified units are started, then this unit is stopped
238 requisite = mkOption {
240 type = types.listOf unitNameType;
242 Similar to requires. However if the units listed are not started,
243 they will not be started and the transaction will fail.
247 unitConfig = mkOption {
249 example = { RequiresMountsFor = "/data"; };
250 type = types.attrsOf unitOption;
252 Each attribute in this set specifies an option in the
253 `[Unit]` section of the unit. See
254 {manpage}`systemd.unit(5)` for details.
258 onFailure = mkOption {
260 type = types.listOf unitNameType;
262 A list of one or more units that are activated when
263 this unit enters the "failed" state.
267 onSuccess = mkOption {
269 type = types.listOf unitNameType;
271 A list of one or more units that are activated when
272 this unit enters the "inactive" state.
276 startLimitBurst = mkOption {
279 Configure unit start rate limiting. Units which are started
280 more than startLimitBurst times within an interval time
281 interval are not permitted to start any more.
285 startLimitIntervalSec = mkOption {
288 Configure unit start rate limiting. Units which are started
289 more than startLimitBurst times within an interval time
290 interval are not permitted to start any more.
297 stage2CommonUnitOptions = {
303 restartTriggers = mkOption {
305 type = types.listOf types.unspecified;
307 An arbitrary list of items such as derivations. If any item
308 in the list changes between reconfigurations, the service will
313 reloadTriggers = mkOption {
315 type = types.listOf unitOption;
317 An arbitrary list of items such as derivations. If any item
318 in the list changes between reconfigurations, the service will
319 be reloaded. If anything but a reload trigger changes in the
320 unit file, the unit will be restarted instead.
325 stage1CommonUnitOptions = commonUnitOptions;
327 serviceOptions = { name, config, ... }: {
330 environment = mkOption {
332 type = with types; attrsOf (nullOr (oneOf [ str path package ]));
333 example = { PATH = "/foo/bar/bin"; LANG = "nl_NL.UTF-8"; };
334 description = "Environment variables passed to the service's processes.";
339 type = with types; listOf (oneOf [ package str ]);
341 Packages added to the service's {env}`PATH`
342 environment variable. Both the {file}`bin`
343 and {file}`sbin` subdirectories of each
348 serviceConfig = mkOption {
353 type = types.addCheck (types.attrsOf unitOption) checkService;
355 Each attribute in this set specifies an option in the
356 `[Service]` section of the unit. See
357 {manpage}`systemd.service(5)` for details.
361 enableStrictShellChecks = mkOption {
363 description = "Enable running shellcheck on the generated scripts for this unit.";
364 # The default gets set in systemd-lib.nix because we don't have access to
365 # the full NixOS config here.
366 defaultText = literalExpression "config.systemd.enableStrictShellChecks";
372 description = "Shell commands executed as the service's main process.";
375 scriptArgs = mkOption {
380 Arguments passed to the main process script.
381 Can contain specifiers (`%` placeholders expanded by systemd, see {manpage}`systemd.unit(5)`).
385 preStart = mkOption {
389 Shell commands executed before the service's main process
394 postStart = mkOption {
398 Shell commands executed after the service's main process
407 Shell commands executed when the service's main process
416 Shell commands executed to stop the service.
420 postStop = mkOption {
424 Shell commands executed after the service's main process
429 jobScripts = mkOption {
430 type = with types; coercedTo path singleton (listOf path);
432 description = "A list of all job script derivations of this unit.";
439 (mkIf (config.preStart != "") rec {
440 jobScripts = makeJobScript {
441 name = "${name}-pre-start";
442 text = config.preStart;
443 inherit (config) enableStrictShellChecks;
445 serviceConfig.ExecStartPre = [ jobScripts ];
447 (mkIf (config.script != "") rec {
448 jobScripts = makeJobScript {
449 name = "${name}-start";
450 text = config.script;
451 inherit (config) enableStrictShellChecks;
453 serviceConfig.ExecStart = jobScripts + " " + config.scriptArgs;
455 (mkIf (config.postStart != "") rec {
456 jobScripts = makeJobScript {
457 name = "${name}-post-start";
458 text = config.postStart;
459 inherit (config) enableStrictShellChecks;
461 serviceConfig.ExecStartPost = [ jobScripts ];
463 (mkIf (config.reload != "") rec {
464 jobScripts = makeJobScript {
465 name = "${name}-reload";
466 text = config.reload;
467 inherit (config) enableStrictShellChecks;
469 serviceConfig.ExecReload = jobScripts;
471 (mkIf (config.preStop != "") rec {
472 jobScripts = makeJobScript {
473 name = "${name}-pre-stop";
474 text = config.preStop;
475 inherit (config) enableStrictShellChecks;
477 serviceConfig.ExecStop = jobScripts;
479 (mkIf (config.postStop != "") rec {
480 jobScripts = makeJobScript {
481 name = "${name}-post-stop";
482 text = config.postStop;
483 inherit (config) enableStrictShellChecks;
485 serviceConfig.ExecStopPost = jobScripts;
491 stage2ServiceOptions = {
493 stage2CommonUnitOptions
498 restartIfChanged = mkOption {
502 Whether the service should be restarted during a NixOS
503 configuration switch if its definition has changed.
507 reloadIfChanged = mkOption {
511 Whether the service should be reloaded during a NixOS
512 configuration switch if its definition has changed. If
513 enabled, the value of {option}`restartIfChanged` is
516 This option should not be used anymore in favor of
517 {option}`reloadTriggers` which allows more granular
518 control of when a service is reloaded and when a service
523 stopIfChanged = mkOption {
527 If set, a changed unit is restarted by calling
528 {command}`systemctl stop` in the old configuration,
529 then {command}`systemctl start` in the new one.
530 Otherwise, it is restarted in a single step using
531 {command}`systemctl restart` in the new configuration.
532 The latter is less correct because it runs the
533 `ExecStop` commands from the new
539 type = with types; either str (listOf str);
541 example = "Sun 14:00:00";
543 Automatically start this unit at the given date/time, which
544 must be in the format described in
545 {manpage}`systemd.time(7)`. This is equivalent
546 to adding a corresponding timer unit with
547 {option}`OnCalendar` set to the value given here.
549 apply = v: if isList v then v else [ v ];
554 stage1ServiceOptions = {
556 stage1CommonUnitOptions
565 listenStreams = mkOption {
567 type = types.listOf types.str;
568 example = [ "0.0.0.0:993" "/run/my-socket" ];
570 For each item in this list, a `ListenStream`
571 option in the `[Socket]` section will be created.
575 listenDatagrams = mkOption {
577 type = types.listOf types.str;
578 example = [ "0.0.0.0:993" "/run/my-socket" ];
580 For each item in this list, a `ListenDatagram`
581 option in the `[Socket]` section will be created.
585 socketConfig = mkOption {
587 example = { ListenStream = "/run/my-socket"; };
588 type = types.attrsOf unitOption;
590 Each attribute in this set specifies an option in the
591 `[Socket]` section of the unit. See
592 {manpage}`systemd.socket(5)` for details.
599 stage2SocketOptions = {
601 stage2CommonUnitOptions
606 stage1SocketOptions = {
608 stage1CommonUnitOptions
617 timerConfig = mkOption {
619 example = { OnCalendar = "Sun 14:00:00"; Unit = "foo.service"; };
620 type = types.attrsOf unitOption;
622 Each attribute in this set specifies an option in the
623 `[Timer]` section of the unit. See
624 {manpage}`systemd.timer(5)` and
625 {manpage}`systemd.time(7)` for details.
632 stage2TimerOptions = {
634 stage2CommonUnitOptions
639 stage1TimerOptions = {
641 stage1CommonUnitOptions
650 pathConfig = mkOption {
652 example = { PathChanged = "/some/path"; Unit = "changedpath.service"; };
653 type = types.attrsOf unitOption;
655 Each attribute in this set specifies an option in the
656 `[Path]` section of the unit. See
657 {manpage}`systemd.path(5)` for details.
664 stage2PathOptions = {
666 stage2CommonUnitOptions
671 stage1PathOptions = {
673 stage1CommonUnitOptions
683 example = "/dev/sda1";
685 description = "Absolute path of device node, file or other resource. (Mandatory)";
692 Absolute path of a directory of the mount point.
693 Will be created if it doesn't exist. (Mandatory)
701 description = "File system type.";
708 description = "Options used to mount the file system.";
711 mountConfig = mkOption {
713 example = { DirectoryMode = "0775"; };
714 type = types.attrsOf unitOption;
716 Each attribute in this set specifies an option in the
717 `[Mount]` section of the unit. See
718 {manpage}`systemd.mount(5)` for details.
725 stage2MountOptions = {
727 stage2CommonUnitOptions
732 stage1MountOptions = {
734 stage1CommonUnitOptions
746 Absolute path of a directory of the mount point.
747 Will be created if it doesn't exist. (Mandatory)
751 automountConfig = mkOption {
753 example = { DirectoryMode = "0775"; };
754 type = types.attrsOf unitOption;
756 Each attribute in this set specifies an option in the
757 `[Automount]` section of the unit. See
758 {manpage}`systemd.automount(5)` for details.
765 stage2AutomountOptions = {
767 stage2CommonUnitOptions
772 stage1AutomountOptions = {
774 stage1CommonUnitOptions
782 sliceConfig = mkOption {
784 example = { MemoryMax = "2G"; };
785 type = types.attrsOf unitOption;
787 Each attribute in this set specifies an option in the
788 `[Slice]` section of the unit. See
789 {manpage}`systemd.slice(5)` for details.
796 stage2SliceOptions = {
798 stage2CommonUnitOptions
803 stage1SliceOptions = {
805 stage1CommonUnitOptions