Extend NixOS: Difference between revisions
imported>Qm3ster m 1. imports is a config attribute, not to be confused with the import keyword 2. Fixed code to correspond to description |
imported>Bzm3r Simplifying language, focusing purpose. |
||
Line 1: | Line 1: | ||
This tutorial | This tutorial shows how to extend a NixOS configuration to include custom [[{{ic|systemd}}]] units, by creating a [[{{ic|systemd}}]] unit IRC client every time a system session starts. Beginning by adding functionality directly to a {{ic|configuration.nix}} file, it then shows how to abstract the functionality into a separate NixOS [[module]]. | ||
= | = The Problem = | ||
We want to start up an IRC client whenever a user logs into/starts their session. | |||
It is possible to find a variety of different ways to do this, but a simple modern approach that fits well within NixOS's {{declarative model}} is to declare a {{ic|systemd}} unit which initializes the IRC client upon session login by a user. | |||
Assume that our IRC client is {{ic|irssi}} as the IRC client. We'll run it inside a [https://wiki.archlinux.org/title/GNU_Screen {{ic|screen}}] daemon, which apart from allowing us to {{multiplex}} our terminal sessions, also enables the IRC session to continue even after we log out of our shell session. | |||
= Implementations = | = Implementations = | ||
Line 13: | Line 13: | ||
== Quick Implementation == | == Quick Implementation == | ||
NixOS provides [https://search.nixos.org/options?channel=23.05&show=systemd a {{ic|systemd}} module] with a wide variety of configuration options. A small number of those (which you can check out on {{NixOS search}}) allows us to implement this small snippet within our {{ic|configuration.nix}}: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
# pkgs is used to fetch screen & irssi. | # pkgs is used to fetch screen & irssi. | ||
{pkgs, ...}: | |||
{ | { | ||
# ircSession is the name of the new service we'll be creating | |||
systemd.services.ircSession = { | systemd.services.ircSession = { | ||
# | |||
wantedBy = [ "multi-user.target" ]; | wantedBy = [ "multi-user.target" ]; | ||
after = [ "network.target" ]; | after = [ "network.target" ]; | ||
Line 38: | Line 38: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
What does this do? The <code>systemd.services.ircSession</code> bit is an option which adds a new system service. This option is defined elsewhere in the NixOS configuration files. This is one of many options; you can see a list of all NixOS configuration options in the [https://nixos.org/nixos/manual/options.html NixOS Manual: List of Options] (or use https://search.nixos.org/options to search for options). We add attributes to this option to configure our new service. As you can see, we configure it to start when the network connects, and to execute a shell command. | What does this do? The <code>systemd.services.ircSession</code> bit is an option which adds a new system service. This option is defined elsewhere in the NixOS configuration files. This is one of many options; you can see a list of all NixOS configuration options in the [https://nixos.org/nixos/manual/options.html NixOS Manual: List of Options] (or use https://search.nixos.org/options to search for options). We add attributes to this option to configure our new service. As you can see, we configure it to start when the network connects, and to execute a shell command. | ||
Line 232: | Line 233: | ||
* [https://web.archive.org/web/20150331124128/http://larrythecow.org/archives/2011-10-11.html System Services on NixOS: larrythecow.org (archived)] | * [https://web.archive.org/web/20150331124128/http://larrythecow.org/archives/2011-10-11.html System Services on NixOS: larrythecow.org (archived)] | ||
[[Category:systemd]] | |||
[[Category:Tutorial]] | [[Category:Tutorial]] | ||
[[Category:NixOS]] | [[Category:NixOS]] |