Add a ‘wantedBy’ attribute to unit definitions

This attribute allows a unit to make itself a dependency of another unit.

Also, add an option to set the default target unit.
This commit is contained in:
Eelco Dolstra 2012-06-17 23:31:21 -04:00
parent 4a95f8996b
commit 42ee3b4209
5 changed files with 57 additions and 71 deletions

View File

@ -317,13 +317,15 @@ in
} }
]; ];
boot.systemd.units."sshd.service" = boot.systemd.units."sshd.service".text =
'' ''
[Unit] [Unit]
Description=SSH daemon Description=SSH daemon
[Service] [Service]
Environment=PATH=${pkgs.coreutils}/bin:${pkgs.openssh}/bin Environment=PATH=${pkgs.coreutils}/bin:${pkgs.openssh}/bin
Environment=LD_LIBRARY_PATH=${nssModulesPath}
Environment=LOCALE_ARCHIVE=/var/run/current-system/sw/lib/locale/locale-archive
ExecStartPre=${preStart} ExecStartPre=${preStart}
ExecStart=\ ExecStart=\
${pkgs.openssh}/sbin/sshd -h ${cfg.hostKeyPath} \ ${pkgs.openssh}/sbin/sshd -h ${cfg.hostKeyPath} \
@ -334,39 +336,8 @@ in
PIDFile=/run/sshd.pid PIDFile=/run/sshd.pid
''; '';
jobs.sshd = boot.systemd.units."sshd.service".wantedBy = [ "multi-user.target" ];
{ description = "OpenSSH server";
startOn = "started network-interfaces";
environment = {
LD_LIBRARY_PATH = nssModulesPath;
# Duplicated from bashrc. OpenSSH needs a patch for this.
LOCALE_ARCHIVE = "/var/run/current-system/sw/lib/locale/locale-archive";
};
path = [ pkgs.openssh pkgs.gnused ];
preStart =
''
${mkAuthkeyScript}
mkdir -m 0755 -p /etc/ssh
if ! test -f ${cfg.hostKeyPath}; then
ssh-keygen -t ${hktn} -b ${toString hktb} -f ${cfg.hostKeyPath} -N ""
fi
'';
daemonType = "fork";
exec =
''
${pkgs.openssh}/sbin/sshd -h ${cfg.hostKeyPath} \
-f ${pkgs.writeText "sshd_config" cfg.extraConfig}
'';
};
networking.firewall.allowedTCPPorts = cfg.ports; networking.firewall.allowedTCPPorts = cfg.ports;
services.openssh.extraConfig = services.openssh.extraConfig =

View File

@ -118,7 +118,7 @@ in
# FIXME: these are copied verbatim from the dbus source tree. We # FIXME: these are copied verbatim from the dbus source tree. We
# should install and use the originals. # should install and use the originals.
boot.systemd.units."dbus.socket" = boot.systemd.units."dbus.socket".text =
'' ''
[Unit] [Unit]
Description=D-Bus System Message Bus Socket Description=D-Bus System Message Bus Socket
@ -127,7 +127,7 @@ in
ListenStream=/var/run/dbus/system_bus_socket ListenStream=/var/run/dbus/system_bus_socket
''; '';
boot.systemd.units."dbus.service" = boot.systemd.units."dbus.service".text =
'' ''
[Unit] [Unit]
Description=D-Bus System Message Bus Description=D-Bus System Message Bus
@ -142,6 +142,7 @@ in
OOMScoreAdjust=-900 OOMScoreAdjust=-900
''; '';
/*
jobs.dbus = jobs.dbus =
{ startOn = "started udev and started syslogd"; { startOn = "started udev and started syslogd";
@ -164,15 +165,6 @@ in
exec = "dbus-daemon --system"; exec = "dbus-daemon --system";
/*
postStart =
''
# Signal Upstart to connect to the system bus. This
# allows initctl to work for non-root users.
kill -USR1 1
'';
*/
postStop = postStop =
'' ''
# !!! Hack: doesn't belong here. # !!! Hack: doesn't belong here.
@ -183,6 +175,7 @@ in
fi fi
''; '';
}; };
*/
security.setuidOwners = singleton security.setuidOwners = singleton
{ program = "dbus-daemon-launch-helper"; { program = "dbus-daemon-launch-helper";

View File

@ -4,10 +4,12 @@ with pkgs.lib;
let let
cfg = config.boot.systemd;
systemd = pkgs.systemd; systemd = pkgs.systemd;
makeUnit = name: text: makeUnit = name: unit:
pkgs.writeTextFile { name = "unit"; inherit text; destination = "/${name}"; }; pkgs.writeTextFile { name = "unit"; inherit (unit) text; destination = "/${name}"; };
upstreamUnits = upstreamUnits =
[ # Targets. [ # Targets.
@ -109,7 +111,7 @@ let
"shutdown.target.wants" "shutdown.target.wants"
]; ];
nixosUnits = mapAttrsToList makeUnit config.boot.systemd.units; nixosUnits = mapAttrsToList makeUnit cfg.units;
units = pkgs.runCommand "units" { preferLocalBuild = true; } units = pkgs.runCommand "units" { preferLocalBuild = true; }
'' ''
@ -123,6 +125,7 @@ let
ln -s $fn $out/system ln -s $fn $out/system
fi fi
done done
for i in ${toString upstreamWants}; do for i in ${toString upstreamWants}; do
fn=${systemd}/example/systemd/system/$i fn=${systemd}/example/systemd/system/$i
[ -e $fn ] [ -e $fn ]
@ -134,9 +137,18 @@ let
if ! [ -e $y ]; then rm -v $y; fi if ! [ -e $y ]; then rm -v $y; fi
done done
done done
for i in ${toString nixosUnits}; do for i in ${toString nixosUnits}; do
cp $i/* $out/system cp $i/* $out/system
done done
${concatStrings (mapAttrsToList (name: unit:
concatMapStrings (name2: ''
mkdir -p $out/system/${name2}.wants
ln -sfn ../${name} $out/system/${name2}.wants/
'') unit.wantedBy) cfg.units)}
ln -s ${cfg.defaultUnit} $out/system/default.target
''; # */ ''; # */
in in
@ -148,10 +160,28 @@ in
options = { options = {
boot.systemd.units = mkOption { boot.systemd.units = mkOption {
default = {} ; default = {};
description = "Systemd units."; type = types.attrsOf types.optionSet;
options = {
text = mkOption {
types = types.uniq types.string;
description = "Text of this systemd unit.";
};
wantedBy = mkOption {
default = [];
types = types.listOf types.string;
description = "Units that want (i.e. depend on) this unit.";
};
};
description = "Definition of systemd units.";
}; };
boot.systemd.defaultUnit = mkOption {
default = "multi-user.target";
type = types.uniq types.string;
description = "Default unit started when the system boots.";
};
}; };
@ -171,18 +201,7 @@ in
} }
]; ];
boot.systemd.units."default.target" = boot.systemd.units."getty@.service".text =
''
[Unit]
Description=Default System
Requires=multi-user.target
After=multi-user.target
Conflicts=rescue.target
AllowIsolate=yes
Wants=sshd.service
'';
boot.systemd.units."getty@.service" =
'' ''
[Unit] [Unit]
Description=Getty on %I Description=Getty on %I
@ -218,7 +237,7 @@ in
KillSignal=SIGHUP KillSignal=SIGHUP
''; '';
boot.systemd.units."rescue.service" = boot.systemd.units."rescue.service".text =
'' ''
[Unit] [Unit]
Description=Rescue Shell Description=Rescue Shell

View File

@ -51,6 +51,7 @@ let
#! ${pkgs.stdenv.shell} -e #! ${pkgs.stdenv.shell} -e
${job.postStop} ${job.postStop}
''; '';
in {
text = text =
'' ''
@ -93,6 +94,13 @@ let
${optionalString (!job.task && job.respawn) "Restart=always"} ${optionalString (!job.task && job.respawn) "Restart=always"}
''; '';
wantedBy =
if job.startOn == "" then [ ]
else if job.startOn == "startup" then [ "basic.target" ]
else [ "multi-user.target" ];
};
/* /*
text = text =
'' ''
@ -189,8 +197,6 @@ let
''; '';
*/ */
in text;
# Shell functions for use in Upstart jobs. # Shell functions for use in Upstart jobs.
jobHelpers = pkgs.writeText "job-helpers.sh" jobHelpers = pkgs.writeText "job-helpers.sh"
@ -440,10 +446,9 @@ let
options = { options = {
unitText = mkOption { unit = mkOption {
default = makeUnit config; default = makeUnit config;
type = types.uniq types.string; description = "Generated definition of the systemd unit corresponding to this job.";
description = "Generated text of the systemd unit corresponding to this job.";
}; };
}; };
@ -504,7 +509,7 @@ in
boot.systemd.units = boot.systemd.units =
flip mapAttrs' config.jobs (name: job: flip mapAttrs' config.jobs (name: job:
nameValuePair "${job.name}.service" job.unitText); nameValuePair "${job.name}.service" job.unit);
}; };

View File

@ -14,8 +14,6 @@
# Make all logical volumes on all volume groups available, i.e., # Make all logical volumes on all volume groups available, i.e.,
# make them appear in /dev. # make them appear in /dev.
${pkgs.lvm2}/sbin/vgchange --available y ${pkgs.lvm2}/sbin/vgchange --available y
initctl emit -n new-devices
''; '';
task = true; task = true;