2012-06-02 02:15:07 +02:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
2012-06-18 21:28:31 +02:00
|
|
|
servicesOptions = {
|
|
|
|
|
|
|
|
description = mkOption {
|
|
|
|
default = "";
|
|
|
|
types = types.uniq types.string;
|
|
|
|
description = "Description of this unit used in systemd messages and progress indicators.";
|
|
|
|
};
|
|
|
|
|
|
|
|
after = mkOption {
|
|
|
|
default = [];
|
|
|
|
types = types.listOf types.string;
|
|
|
|
description = ''
|
|
|
|
If the specified units are started at the same time as
|
|
|
|
this unit, delay this unit until they have started.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
before = mkOption {
|
|
|
|
default = [];
|
|
|
|
types = types.listOf types.string;
|
|
|
|
description = ''
|
|
|
|
If the specified units are started at the same time as
|
|
|
|
this unit, delay them until this unit has started.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
wantedBy = mkOption {
|
|
|
|
default = [];
|
|
|
|
types = types.listOf types.string;
|
|
|
|
description = "Units that want (i.e. depend on) this unit.";
|
|
|
|
};
|
|
|
|
|
|
|
|
environment = mkOption {
|
|
|
|
default = {};
|
|
|
|
type = types.attrs;
|
|
|
|
example = { PATH = "/foo/bar/bin"; LANG = "nl_NL.UTF-8"; };
|
|
|
|
description = "Environment variables passed to the services's processes.";
|
|
|
|
};
|
|
|
|
|
|
|
|
path = mkOption {
|
|
|
|
default = [];
|
|
|
|
apply = ps: "${makeSearchPath "bin" ps}:${makeSearchPath "sbin" ps}";
|
|
|
|
description = ''
|
|
|
|
Packages added to the service's <envar>PATH</envar>
|
|
|
|
environment variable. Both the <filename>bin</filename>
|
|
|
|
and <filename>sbin</filename> subdirectories of each
|
|
|
|
package are added.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
serviceConfig = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.string;
|
|
|
|
description = ''
|
|
|
|
Contents of the <literal>[Service]</literal> section of the unit.
|
|
|
|
See <citerefentry><refentrytitle>systemd.unit</refentrytitle>
|
|
|
|
<manvolnum>5</manvolnum></citerefentry> for details.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
script = mkOption {
|
|
|
|
type = types.uniq types.string;
|
|
|
|
default = "";
|
|
|
|
description = "Shell commands executed as the service's main process.";
|
|
|
|
};
|
|
|
|
|
|
|
|
preStart = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Shell commands executed before the service's main process
|
|
|
|
is started.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
servicesConfig = { name, config, ... }: {
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
|
|
|
# Default path for systemd services. Should be quite minimal.
|
|
|
|
path =
|
|
|
|
[ pkgs.coreutils
|
|
|
|
pkgs.findutils
|
|
|
|
pkgs.gnugrep
|
|
|
|
pkgs.gnused
|
|
|
|
systemd
|
|
|
|
];
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-06-18 05:31:21 +02:00
|
|
|
cfg = config.boot.systemd;
|
|
|
|
|
2012-06-02 02:15:07 +02:00
|
|
|
systemd = pkgs.systemd;
|
|
|
|
|
2012-06-18 05:31:21 +02:00
|
|
|
makeUnit = name: unit:
|
|
|
|
pkgs.writeTextFile { name = "unit"; inherit (unit) text; destination = "/${name}"; };
|
2012-06-02 02:15:07 +02:00
|
|
|
|
|
|
|
upstreamUnits =
|
2012-06-15 00:44:56 +02:00
|
|
|
[ # Targets.
|
2012-06-02 02:15:07 +02:00
|
|
|
"basic.target"
|
|
|
|
"sysinit.target"
|
|
|
|
"sockets.target"
|
2012-06-15 00:44:56 +02:00
|
|
|
"graphical.target"
|
|
|
|
"multi-user.target"
|
|
|
|
"getty.target"
|
|
|
|
"rescue.target"
|
|
|
|
"network.target"
|
|
|
|
"nss-lookup.target"
|
|
|
|
"nss-user-lookup.target"
|
|
|
|
"syslog.target"
|
|
|
|
"time-sync.target"
|
|
|
|
|
2012-06-15 19:09:22 +02:00
|
|
|
# Udev.
|
|
|
|
"systemd-udev-control.socket"
|
|
|
|
"systemd-udev-kernel.socket"
|
|
|
|
"systemd-udev.service"
|
|
|
|
"systemd-udev-settle.service"
|
|
|
|
"systemd-udev-trigger.service"
|
|
|
|
|
2012-06-15 00:44:56 +02:00
|
|
|
# Login stuff.
|
|
|
|
"systemd-logind.service"
|
|
|
|
"autovt@.service"
|
|
|
|
"systemd-vconsole-setup.service"
|
|
|
|
"systemd-user-sessions.service"
|
2012-06-15 19:09:22 +02:00
|
|
|
"dbus-org.freedesktop.login1.service"
|
2012-06-15 20:51:48 +02:00
|
|
|
"user@.service"
|
2012-06-15 00:44:56 +02:00
|
|
|
|
|
|
|
# Journal.
|
|
|
|
"systemd-journald.socket"
|
|
|
|
"systemd-journald.service"
|
2012-06-02 02:15:07 +02:00
|
|
|
|
2012-06-15 00:44:56 +02:00
|
|
|
# SysV init compatibility.
|
|
|
|
"systemd-initctl.socket"
|
|
|
|
"systemd-initctl.service"
|
|
|
|
"runlevel0.target"
|
|
|
|
"runlevel1.target"
|
|
|
|
"runlevel2.target"
|
|
|
|
"runlevel3.target"
|
|
|
|
"runlevel4.target"
|
|
|
|
"runlevel5.target"
|
|
|
|
"runlevel6.target"
|
|
|
|
|
|
|
|
# Random seed.
|
|
|
|
"systemd-random-seed-load.service"
|
|
|
|
"systemd-random-seed-save.service"
|
|
|
|
|
|
|
|
# Utmp maintenance.
|
|
|
|
"systemd-update-utmp-runlevel.service"
|
|
|
|
"systemd-update-utmp-shutdown.service"
|
|
|
|
|
2012-06-02 02:15:07 +02:00
|
|
|
# Filesystems.
|
2012-06-15 00:44:56 +02:00
|
|
|
"fsck@.service"
|
|
|
|
"fsck-root.service"
|
|
|
|
"systemd-remount-fs.service"
|
2012-06-02 02:15:07 +02:00
|
|
|
"local-fs.target"
|
|
|
|
"local-fs-pre.target"
|
|
|
|
"remote-fs.target"
|
|
|
|
"remote-fs-pre.target"
|
|
|
|
"swap.target"
|
2012-06-15 00:44:56 +02:00
|
|
|
"dev-hugepages.mount"
|
2012-06-02 02:15:07 +02:00
|
|
|
"dev-mqueue.mount"
|
2012-06-15 00:44:56 +02:00
|
|
|
"sys-fs-fuse-connections.mount"
|
|
|
|
"sys-kernel-config.mount"
|
|
|
|
"sys-kernel-debug.mount"
|
|
|
|
|
|
|
|
# Hibernate / suspend.
|
|
|
|
"hibernate.target"
|
|
|
|
"hibernate.service"
|
|
|
|
"suspend.target"
|
|
|
|
"suspend.service"
|
|
|
|
"sleep.target"
|
2012-06-02 02:15:07 +02:00
|
|
|
|
|
|
|
# Reboot stuff.
|
|
|
|
"reboot.target"
|
|
|
|
"reboot.service"
|
|
|
|
"poweroff.target"
|
|
|
|
"poweroff.service"
|
|
|
|
"halt.target"
|
|
|
|
"halt.service"
|
|
|
|
"ctrl-alt-del.target"
|
|
|
|
"shutdown.target"
|
|
|
|
"umount.target"
|
|
|
|
"final.target"
|
2012-06-15 00:44:56 +02:00
|
|
|
|
|
|
|
# Misc.
|
|
|
|
"syslog.socket"
|
2012-06-02 02:15:07 +02:00
|
|
|
];
|
|
|
|
|
2012-06-15 00:44:56 +02:00
|
|
|
upstreamWants =
|
|
|
|
[ "basic.target.wants"
|
|
|
|
"sysinit.target.wants"
|
|
|
|
"sockets.target.wants"
|
|
|
|
"local-fs.target.wants"
|
|
|
|
"multi-user.target.wants"
|
|
|
|
"shutdown.target.wants"
|
|
|
|
];
|
|
|
|
|
2012-06-18 21:28:31 +02:00
|
|
|
rescueService =
|
|
|
|
''
|
|
|
|
[Unit]
|
|
|
|
Description=Rescue Shell
|
|
|
|
DefaultDependencies=no
|
|
|
|
Conflicts=shutdown.target
|
|
|
|
After=sysinit.target
|
|
|
|
Before=shutdown.target
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
Environment=HOME=/root
|
|
|
|
WorkingDirectory=/root
|
|
|
|
ExecStartPre=-${pkgs.coreutils}/bin/echo 'Welcome to rescue mode. Use "systemctl default" or ^D to enter default mode.'
|
|
|
|
#ExecStart=-/sbin/sulogin
|
|
|
|
ExecStart=-${pkgs.bashInteractive}/bin/bash --login
|
|
|
|
ExecStopPost=-${systemd}/bin/systemctl --fail --no-block default
|
|
|
|
Type=idle
|
|
|
|
StandardInput=tty-force
|
|
|
|
StandardOutput=inherit
|
|
|
|
StandardError=inherit
|
|
|
|
KillMode=process
|
|
|
|
|
|
|
|
# Bash ignores SIGTERM, so we send SIGHUP instead, to ensure that bash
|
|
|
|
# terminates cleanly.
|
|
|
|
KillSignal=SIGHUP
|
|
|
|
'';
|
|
|
|
|
|
|
|
serviceToUnit = name: def:
|
|
|
|
{ inherit (def) wantedBy;
|
|
|
|
|
|
|
|
text =
|
|
|
|
''
|
|
|
|
[Unit]
|
|
|
|
${optionalString (def.description != "") ''
|
|
|
|
Description=${def.description}
|
|
|
|
''}
|
|
|
|
Before=${concatStringsSep " " def.before}
|
|
|
|
After=${concatStringsSep " " def.after}
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
Environment=PATH=${def.path}
|
2012-06-19 05:30:26 +02:00
|
|
|
${concatMapStrings (n: "Environment=${n}=${getAttr n def.environment}\n") (attrNames def.environment)}
|
2012-06-18 21:28:31 +02:00
|
|
|
|
|
|
|
${optionalString (def.preStart != "") ''
|
|
|
|
ExecStartPre=${pkgs.writeScript "${name}-prestart.sh" ''
|
|
|
|
#! ${pkgs.stdenv.shell} -e
|
|
|
|
${def.preStart}
|
|
|
|
''}
|
|
|
|
''}
|
|
|
|
|
|
|
|
${optionalString (def.script != "") ''
|
|
|
|
ExecStart=${pkgs.writeScript "${name}.sh" ''
|
|
|
|
#! ${pkgs.stdenv.shell} -e
|
|
|
|
${def.script}
|
|
|
|
''}
|
|
|
|
''}
|
|
|
|
|
|
|
|
${def.serviceConfig}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2012-06-18 05:31:21 +02:00
|
|
|
nixosUnits = mapAttrsToList makeUnit cfg.units;
|
2012-06-15 00:44:56 +02:00
|
|
|
|
2012-06-16 06:19:43 +02:00
|
|
|
units = pkgs.runCommand "units" { preferLocalBuild = true; }
|
2012-06-02 02:15:07 +02:00
|
|
|
''
|
|
|
|
mkdir -p $out/system
|
|
|
|
for i in ${toString upstreamUnits}; do
|
|
|
|
fn=${systemd}/example/systemd/system/$i
|
|
|
|
[ -e $fn ]
|
2012-06-15 00:44:56 +02:00
|
|
|
if [ -L $fn ]; then
|
|
|
|
cp -pd $fn $out/system/
|
|
|
|
else
|
|
|
|
ln -s $fn $out/system
|
|
|
|
fi
|
2012-06-02 02:15:07 +02:00
|
|
|
done
|
2012-06-18 05:31:21 +02:00
|
|
|
|
2012-06-15 00:44:56 +02:00
|
|
|
for i in ${toString upstreamWants}; do
|
|
|
|
fn=${systemd}/example/systemd/system/$i
|
|
|
|
[ -e $fn ]
|
|
|
|
x=$out/system/$(basename $fn)
|
|
|
|
mkdir $x
|
|
|
|
for i in $fn/*; do
|
|
|
|
y=$x/$(basename $i)
|
|
|
|
cp -pd $i $y
|
|
|
|
if ! [ -e $y ]; then rm -v $y; fi
|
|
|
|
done
|
|
|
|
done
|
2012-06-18 05:31:21 +02:00
|
|
|
|
2012-06-15 00:44:56 +02:00
|
|
|
for i in ${toString nixosUnits}; do
|
2012-06-02 02:15:07 +02:00
|
|
|
cp $i/* $out/system
|
|
|
|
done
|
2012-06-18 05:31:21 +02:00
|
|
|
|
|
|
|
${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
|
2012-06-02 02:15:07 +02:00
|
|
|
''; # */
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
2012-06-15 00:44:56 +02:00
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
boot.systemd.units = mkOption {
|
2012-06-18 21:28:31 +02:00
|
|
|
description = "Definition of systemd units.";
|
2012-06-18 05:31:21 +02:00
|
|
|
default = {};
|
|
|
|
type = types.attrsOf types.optionSet;
|
2012-06-18 21:28:31 +02:00
|
|
|
|
2012-06-18 05:31:21 +02:00
|
|
|
options = {
|
2012-06-18 21:28:31 +02:00
|
|
|
|
2012-06-18 05:31:21 +02:00
|
|
|
text = mkOption {
|
|
|
|
types = types.uniq types.string;
|
|
|
|
description = "Text of this systemd unit.";
|
|
|
|
};
|
2012-06-18 21:28:31 +02:00
|
|
|
|
2012-06-18 05:31:21 +02:00
|
|
|
wantedBy = mkOption {
|
|
|
|
default = [];
|
|
|
|
types = types.listOf types.string;
|
|
|
|
description = "Units that want (i.e. depend on) this unit.";
|
|
|
|
};
|
2012-06-18 21:28:31 +02:00
|
|
|
|
2012-06-18 05:31:21 +02:00
|
|
|
};
|
2012-06-18 21:28:31 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
boot.systemd.services = mkOption {
|
|
|
|
description = "Definition of systemd services.";
|
|
|
|
default = {};
|
|
|
|
type = types.attrsOf types.optionSet;
|
|
|
|
options = [ servicesOptions servicesConfig ];
|
2012-06-15 00:44:56 +02:00
|
|
|
};
|
|
|
|
|
2012-06-18 05:31:21 +02:00
|
|
|
boot.systemd.defaultUnit = mkOption {
|
|
|
|
default = "multi-user.target";
|
|
|
|
type = types.uniq types.string;
|
|
|
|
description = "Default unit started when the system boots.";
|
|
|
|
};
|
|
|
|
|
2012-06-15 00:44:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-06-02 02:15:07 +02:00
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
2012-06-15 19:09:22 +02:00
|
|
|
system.build.systemd = systemd;
|
|
|
|
|
2012-06-16 06:19:43 +02:00
|
|
|
system.build.units = units;
|
|
|
|
|
2012-06-02 02:15:07 +02:00
|
|
|
environment.systemPackages = [ systemd ];
|
|
|
|
|
|
|
|
environment.etc =
|
2012-06-16 06:19:43 +02:00
|
|
|
[ { source = units;
|
2012-06-02 02:15:07 +02:00
|
|
|
target = "systemd";
|
|
|
|
}
|
|
|
|
];
|
2012-06-15 00:44:56 +02:00
|
|
|
|
2012-06-18 21:28:31 +02:00
|
|
|
boot.systemd.units =
|
2012-06-18 23:55:27 +02:00
|
|
|
{ "rescue.service".text = rescueService; }
|
2012-06-18 21:28:31 +02:00
|
|
|
// mapAttrs serviceToUnit cfg.services;
|
2012-06-15 00:44:56 +02:00
|
|
|
|
2012-06-02 02:15:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|