Home Explore Blog Models CI



nixpkgs

2nd chunk of `nixos/doc/manual/administration/service-mgmt.chapter.md`
bfda734d33c0c460eb8edf37d92ab92f52bb7564ea9c2d8500000001000008cf
Jan 07 15:55:57 hagbard postgres[2420]: [1-1] LOG:  autovacuum launcher started
Jan 07 15:55:57 hagbard systemd[1]: Started PostgreSQL Server.
```

Note that this shows the status of the unit (active and running), all
the processes belonging to the service, as well as the most recent log
messages from the service.

Units can be stopped, started or restarted:

```ShellSession
# systemctl stop postgresql.service
# systemctl start postgresql.service
# systemctl restart postgresql.service
```

These operations are synchronous: they wait until the service has
finished starting or stopping (or has failed). Starting a unit will
cause the dependencies of that unit to be started as well (if
necessary).

## systemd in NixOS {#sect-nixos-systemd-nixos}

Packages in Nixpkgs sometimes provide systemd units with them, usually
in e.g `#pkg-out#/lib/systemd/`. Putting such a package in
`environment.systemPackages` doesn't make the service available to
users or the system.

In order to enable a systemd *system* service with provided upstream
package, use (e.g):

```nix
{ systemd.packages = [ pkgs.packagekit ]; }
```

Usually NixOS modules written by the community do the above, plus take
care of other details. If a module was written for a service you are
interested in, you'd probably need only to use
`services.#name#.enable = true;`. These services are defined in
Nixpkgs' [ `nixos/modules/` directory
](https://github.com/NixOS/nixpkgs/tree/master/nixos/modules). In case
the service is simple enough, the above method should work, and start
the service on boot.

*User* systemd services on the other hand, should be treated
differently. Given a package that has a systemd unit file at
`#pkg-out#/lib/systemd/user/`, using [](#opt-systemd.packages) will
make you able to start the service via `systemctl --user start`, but it
won't start automatically on login. However, You can imperatively
enable it by adding the package's attribute to
[](#opt-systemd.packages) and then do this (e.g):

```ShellSession
$ mkdir -p ~/.config/systemd/user/default.target.wants
$ ln -s /run/current-system/sw/lib/systemd/user/syncthing.service ~/.config/systemd/user/default.target.wants/
$ systemctl --user daemon-reload
$ systemctl --user enable syncthing.service

Title: Configuring Systemd Services in NixOS
Summary
This text details how to manage systemd services within NixOS, covering both system and user-level configurations. It reiterates that `systemctl` commands for stopping, starting, and restarting services are synchronous and handle dependencies. For *system* services provided by Nixpkgs packages, it explains two methods: either directly adding the package to `systemd.packages = [ pkgs.packagekit ];` for raw unit files, or preferably using `services.#name#.enable = true;` when a dedicated NixOS module is available. For *user* systemd services, it notes that while `systemd.packages` makes them available, they require manual configuration (creating a symlink in `~/.config/systemd/user/default.target.wants/` and using `systemctl --user daemon-reload` followed by `systemctl --user enable`) to ensure automatic startup on user login.