Home Explore Blog CI



nixpkgs

2nd chunk of `nixos/doc/manual/administration/service-mgmt.chapter.md`
60ca02122490cf63c4740f6625b3e98189bb12ece8232e59000000010000083c
Jan 07 15:55:55 hagbard postgres[2394]: [1-1] LOG:  database system was shut down at 2013-01-07 15:55:05 CET
Jan 07 15:55:57 hagbard postgres[2390]: [1-1] LOG:  database system is ready to accept connections
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

Title: Managing systemd Units and Services in NixOS
Summary
This section explains how to manage systemd units and services within NixOS. It highlights that simply installing a package containing a systemd unit doesn't automatically enable the service. To enable a system service from a Nixpkgs package, you need to add the package to `systemd.packages`. It also discusses using NixOS modules for service management and mentions the difference between system and user systemd services, including how to start user services manually.