systemd service: add support for shutdown packages

Shutdown hooks are executed right before the shutdown, which is useful
for some applications. Among other things this is needed for mdadm hook
to run.
This commit is contained in:
Nikolay Amiantov 2019-07-25 16:27:28 +03:00
parent fd405dab3e
commit a304fc5d75

View file

@ -504,6 +504,23 @@ in
description = "Packages providing systemd generators."; description = "Packages providing systemd generators.";
}; };
systemd.shutdown = mkOption {
type = types.attrsOf types.path;
default = {};
description = ''
Definition of systemd shutdown executables.
For each <literal>NAME = VALUE</literal> pair of the attrSet, a link is generated from
<literal>/etc/systemd/system-shutdown/NAME</literal> to <literal>VALUE</literal>.
'';
};
systemd.shutdownPackages = mkOption {
default = [];
type = types.listOf types.package;
example = literalExample "[ pkgs.mdadm ]";
description = "Packages providing systemd shutdown executables.";
};
systemd.defaultUnit = mkOption { systemd.defaultUnit = mkOption {
default = "multi-user.target"; default = "multi-user.target";
type = types.str; type = types.str;
@ -761,18 +778,21 @@ in
environment.systemPackages = [ systemd ]; environment.systemPackages = [ systemd ];
environment.etc = let environment.etc = let
# generate contents for /etc/systemd/system-generators from # generate contents for /etc/systemd/system-${type} from attrset of links and packages
# systemd.generators and systemd.generatorPackages hooks = type: links: packages: pkgs.runCommand "system-${type}" {
generators = pkgs.runCommand "system-generators" {
preferLocalBuild = true; preferLocalBuild = true;
packages = cfg.generatorPackages; packages = packages;
} '' } ''
set -e
mkdir -p $out mkdir -p $out
for package in $packages for package in $packages
do do
ln -s $package/lib/systemd/system-generators/* $out/ for hook in $package/lib/systemd/system-${type}/*
done; do
${concatStrings (mapAttrsToList (generator: target: "ln -s ${target} $out/${generator};\n") cfg.generators)} ln -s $hook $out/
done
done
${concatStrings (mapAttrsToList (exec: target: "ln -s ${target} $out/${exec};\n") links)}
''; '';
in ({ in ({
"systemd/system".source = generateUnits "system" cfg.units upstreamSystemUnits upstreamSystemWants; "systemd/system".source = generateUnits "system" cfg.units upstreamSystemUnits upstreamSystemWants;
@ -834,7 +854,8 @@ in
${concatStringsSep "\n" cfg.tmpfiles.rules} ${concatStringsSep "\n" cfg.tmpfiles.rules}
''; '';
"systemd/system-generators" = { source = generators; }; "systemd/system-generators" = { source = hooks "generators" cfg.generators cfg.generatorPackages; };
"systemd/system-shutdown" = { source = hooks "shutdown" cfg.shutdown cfg.shutdownPackages; };
}); });
services.dbus.enable = true; services.dbus.enable = true;