···
1
-
<chapter xmlns="http://docbook.org/ns/docbook"
2
-
xmlns:xlink="http://www.w3.org/1999/xlink"
3
-
xmlns:xi="http://www.w3.org/2001/XInclude"
5
-
xml:id="sec-systemctl">
6
-
<title>Service Management</title>
8
-
In NixOS, all system services are started and monitored using the systemd
9
-
program. systemd is the “init” process of the system (i.e. PID 1), the
10
-
parent of all other processes. It manages a set of so-called “units”,
11
-
which can be things like system services (programs), but also mount points,
12
-
swap files, devices, targets (groups of units) and more. Units can have
13
-
complex dependencies; for instance, one unit can require that another unit
14
-
must be successfully started before the first unit can be started. When the
15
-
system boots, it starts a unit named <literal>default.target</literal>; the
16
-
dependencies of this unit cause all system services to be started, file
17
-
systems to be mounted, swap files to be activated, and so on.
19
-
<section xml:id="sect-nixos-systemd-general">
20
-
<title>Interacting with a running systemd</title>
22
-
The command <command>systemctl</command> is the main way to interact with
23
-
<command>systemd</command>. The following paragraphs demonstrate ways to
24
-
interact with any OS running systemd as init system. NixOS is of no
25
-
exception. The <link xlink:href="#sect-nixos-systemd-nixos">next section
26
-
</link> explains NixOS specific things worth knowing.
29
-
Without any arguments, <literal>systmctl</literal> the status of active units:
31
-
<prompt>$ </prompt>systemctl
32
-
-.mount loaded active mounted /
33
-
swapfile.swap loaded active active /swapfile
34
-
sshd.service loaded active running SSH Daemon
35
-
graphical.target loaded active active Graphical Interface
36
-
<replaceable>...</replaceable>
40
-
You can ask for detailed status information about a unit, for instance, the
41
-
PostgreSQL database service:
43
-
<prompt>$ </prompt>systemctl status postgresql.service
44
-
postgresql.service - PostgreSQL Server
45
-
Loaded: loaded (/nix/store/pn3q73mvh75gsrl8w7fdlfk3fq5qm5mw-unit/postgresql.service)
46
-
Active: active (running) since Mon, 2013-01-07 15:55:57 CET; 9h ago
47
-
Main PID: 2390 (postgres)
48
-
CGroup: name=systemd:/system/postgresql.service
50
-
├─2418 postgres: writer process
51
-
├─2419 postgres: wal writer process
52
-
├─2420 postgres: autovacuum launcher process
53
-
├─2421 postgres: stats collector process
54
-
└─2498 postgres: zabbix zabbix [local] idle
56
-
Jan 07 15:55:55 hagbard postgres[2394]: [1-1] LOG: database system was shut down at 2013-01-07 15:55:05 CET
57
-
Jan 07 15:55:57 hagbard postgres[2390]: [1-1] LOG: database system is ready to accept connections
58
-
Jan 07 15:55:57 hagbard postgres[2420]: [1-1] LOG: autovacuum launcher started
59
-
Jan 07 15:55:57 hagbard systemd[1]: Started PostgreSQL Server.
61
-
Note that this shows the status of the unit (active and running), all the
62
-
processes belonging to the service, as well as the most recent log messages
66
-
Units can be stopped, started or restarted:
68
-
<prompt># </prompt>systemctl stop postgresql.service
69
-
<prompt># </prompt>systemctl start postgresql.service
70
-
<prompt># </prompt>systemctl restart postgresql.service
72
-
These operations are synchronous: they wait until the service has finished
73
-
starting or stopping (or has failed). Starting a unit will cause the
74
-
dependencies of that unit to be started as well (if necessary).
76
-
<!-- TODO: document cgroups, draft:
77
-
each service and user session is a cgroup
79
-
- cgroup resource management -->
81
-
<section xml:id="sect-nixos-systemd-nixos">
82
-
<title>systemd in NixOS</title>
84
-
Packages in Nixpkgs sometimes provide systemd units with them, usually in
85
-
e.g <literal>#pkg-out#/lib/systemd/</literal>. Putting such a package in
86
-
<literal>environment.systemPackages</literal> doesn't make the service
87
-
available to users or the system.
90
-
In order to enable a systemd <emphasis>system</emphasis> service with
91
-
provided upstream package, use (e.g):
93
-
<xref linkend="opt-systemd.packages"/> = [ pkgs.packagekit ];
97
-
Usually NixOS modules written by the community do the above, plus take care of
98
-
other details. If a module was written for a service you are interested in,
99
-
you'd probably need only to use
100
-
<literal>services.#name#.enable = true;</literal>. These services are defined
102
-
<link xlink:href="https://github.com/NixOS/nixpkgs/tree/master/nixos/modules">
103
-
<literal>nixos/modules/</literal> directory </link>. In case the service is
104
-
simple enough, the above method should work, and start the service on boot.
107
-
<emphasis>User</emphasis> systemd services on the other hand, should be
108
-
treated differently. Given a package that has a systemd unit file at
109
-
<literal>#pkg-out#/lib/systemd/user/</literal>, using
110
-
<xref linkend="opt-systemd.packages"/> will make you able to start the service via
111
-
<literal>systemctl --user start</literal>, but it won't start automatically on login.
112
-
<!-- TODO: Document why systemd.packages doesn't work for user services or fix this.
113
-
https://github.com/NixOS/nixpkgs/blob/2cd6594a8710a801038af2b72348658f732ce84a/nixos/modules/system/boot/systemd-lib.nix#L177-L198
115
-
This has been talked over at https://discourse.nixos.org/t/how-to-enable-upstream-systemd-user-services-declaratively/7649/5
117
-
However, You can imperatively enable it by adding the package's attribute to
118
-
<link linkend="opt-environment.systemPackages">
119
-
<literal>systemd.packages</literal></link> and then do this (e.g):
121
-
<prompt>$ </prompt>mkdir -p ~/.config/systemd/user/default.target.wants
122
-
<prompt>$ </prompt>ln -s /run/current-system/sw/lib/systemd/user/syncthing.service ~/.config/systemd/user/default.target.wants/
123
-
<prompt>$ </prompt>systemctl --user daemon-reload
124
-
<prompt>$ </prompt>systemctl --user enable syncthing.service
126
-
If you are interested in a timer file, use <literal>timers.target.wants</literal>
127
-
instead of <literal>default.target.wants</literal> in the 1st and 2nd command.
130
-
Using <literal>systemctl --user enable syncthing.service</literal> instead of
131
-
the above, will work, but it'll use the absolute path of
132
-
<literal>syncthing.service</literal> for the symlink, and this path is in
133
-
<literal>/nix/store/.../lib/systemd/user/</literal>. Hence
134
-
<link xlink:href="#sec-nix-gc">garbage collection</link> will remove that file
135
-
and you will wind up with a broken symlink in your systemd configuration, which
136
-
in turn will not make the service / timer start on login.