From a304fc5d753d0d2c134bf0925070bcead8650dff Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Thu, 25 Jul 2019 16:27:28 +0300 Subject: 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. --- nixos/modules/system/boot/systemd.nix | 39 +++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'nixos/modules/system') diff --git a/nixos/modules/system/boot/systemd.nix b/nixos/modules/system/boot/systemd.nix index a89a200960e4..96c4ee30584b 100644 --- a/nixos/modules/system/boot/systemd.nix +++ b/nixos/modules/system/boot/systemd.nix @@ -504,6 +504,23 @@ in description = "Packages providing systemd generators."; }; + systemd.shutdown = mkOption { + type = types.attrsOf types.path; + default = {}; + description = '' + Definition of systemd shutdown executables. + For each NAME = VALUE pair of the attrSet, a link is generated from + /etc/systemd/system-shutdown/NAME to VALUE. + ''; + }; + + systemd.shutdownPackages = mkOption { + default = []; + type = types.listOf types.package; + example = literalExample "[ pkgs.mdadm ]"; + description = "Packages providing systemd shutdown executables."; + }; + systemd.defaultUnit = mkOption { default = "multi-user.target"; type = types.str; @@ -761,18 +778,21 @@ in environment.systemPackages = [ systemd ]; environment.etc = let - # generate contents for /etc/systemd/system-generators from - # systemd.generators and systemd.generatorPackages - generators = pkgs.runCommand "system-generators" { + # generate contents for /etc/systemd/system-${type} from attrset of links and packages + hooks = type: links: packages: pkgs.runCommand "system-${type}" { preferLocalBuild = true; - packages = cfg.generatorPackages; - } '' + packages = packages; + } '' + set -e mkdir -p $out for package in $packages do - ln -s $package/lib/systemd/system-generators/* $out/ - done; - ${concatStrings (mapAttrsToList (generator: target: "ln -s ${target} $out/${generator};\n") cfg.generators)} + for hook in $package/lib/systemd/system-${type}/* + do + ln -s $hook $out/ + done + done + ${concatStrings (mapAttrsToList (exec: target: "ln -s ${target} $out/${exec};\n") links)} ''; in ({ "systemd/system".source = generateUnits "system" cfg.units upstreamSystemUnits upstreamSystemWants; @@ -834,7 +854,8 @@ in ${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; -- cgit 1.4.1