diff --git a/nixos/doc/manual/development.xml b/nixos/doc/manual/development.xml
index 6bbccac6e5c1..cbf7ff8902fc 100644
--- a/nixos/doc/manual/development.xml
+++ b/nixos/doc/manual/development.xml
@@ -652,6 +652,37 @@ $ qemu-system-x86_64 -kernel ./kernel/bzImage -initrd ./initrd/initrd -hda /dev/
+
+ systemd.units.unit-name.unit
+
+ This builds the unit with the specified name. Note that
+ since unit names contain dots
+ (e.g. httpd.service), you need to put them
+ between quotes, like this:
+
+
+$ nix-build -A 'config.systemd.units."httpd.service".unit'
+
+
+ You can also test individual units, without rebuilding the whole
+ system, by putting them in
+ /run/systemd/system:
+
+
+$ cp $(nix-build -A 'config.systemd.units."httpd.service".unit')/httpd.service \
+ /run/systemd/system/tmp-httpd.service
+$ systemctl daemon-reload
+$ systemctl start tmp-httpd.service
+
+
+ Note that the unit must not have the same name as any unit in
+ /etc/systemd/system since those take
+ precedence over /run/systemd/system.
+ That’s why the unit is installed as
+ tmp-httpd.service here.
+
+
+
diff --git a/nixos/modules/system/boot/systemd.nix b/nixos/modules/system/boot/systemd.nix
index 8895f9bf727f..5c25dabd0c0e 100644
--- a/nixos/modules/system/boot/systemd.nix
+++ b/nixos/modules/system/boot/systemd.nix
@@ -311,8 +311,6 @@ let
'';
};
- nixosUnits = mapAttrsToList makeUnit cfg.units;
-
units = pkgs.runCommand "units" { preferLocalBuild = true; }
''
mkdir -p $out
@@ -338,7 +336,7 @@ let
done
done
- for i in ${toString nixosUnits}; do
+ for i in ${toString (mapAttrsToList (n: v: v.unit) cfg.units)}; do
ln -s $i/* $out/
done
@@ -387,32 +385,41 @@ in
description = "Definition of systemd units.";
default = {};
type = types.attrsOf types.optionSet;
- options = {
- text = mkOption {
- type = types.str;
- description = "Text of this systemd unit.";
+ options = { name, config, ... }:
+ { options = {
+ text = mkOption {
+ type = types.str;
+ description = "Text of this systemd unit.";
+ };
+ enable = mkOption {
+ default = true;
+ type = types.bool;
+ description = ''
+ If set to false, this unit will be a symlink to
+ /dev/null. This is primarily useful to prevent specific
+ template instances (e.g. serial-getty@ttyS0)
+ from being started.
+ '';
+ };
+ requiredBy = mkOption {
+ default = [];
+ type = types.listOf types.string;
+ description = "Units that require (i.e. depend on and need to go down with) this unit.";
+ };
+ wantedBy = mkOption {
+ default = [];
+ type = types.listOf types.string;
+ description = "Units that want (i.e. depend on) this unit.";
+ };
+ unit = mkOption {
+ internal = true;
+ description = "The generated unit.";
+ };
+ };
+ config = {
+ unit = makeUnit name config;
+ };
};
- enable = mkOption {
- default = true;
- type = types.bool;
- description = ''
- If set to false, this unit will be a symlink to
- /dev/null. This is primarily useful to prevent specific
- template instances (e.g. serial-getty@ttyS0)
- from being started.
- '';
- };
- requiredBy = mkOption {
- default = [];
- type = types.listOf types.string;
- description = "Units that require (i.e. depend on and need to go down with) this unit.";
- };
- wantedBy = mkOption {
- default = [];
- type = types.listOf types.string;
- description = "Units that want (i.e. depend on) this unit.";
- };
- };
};
systemd.packages = mkOption {