about summary refs log tree commit diff
path: root/nixos/modules/system
diff options
context:
space:
mode:
authorNikolay Amiantov <ab@fmap.me>2019-07-25 16:27:28 +0300
committerNikolay Amiantov <ab@fmap.me>2019-08-01 00:55:35 +0300
commita304fc5d753d0d2c134bf0925070bcead8650dff (patch)
treee0eccbced7cb31ce9e0c09fe5c55e22e3f19ae96 /nixos/modules/system
parentfd405dab3ea0b5a2d3730c836dbcfabf2e820951 (diff)
downloadnixlib-a304fc5d753d0d2c134bf0925070bcead8650dff.tar
nixlib-a304fc5d753d0d2c134bf0925070bcead8650dff.tar.gz
nixlib-a304fc5d753d0d2c134bf0925070bcead8650dff.tar.bz2
nixlib-a304fc5d753d0d2c134bf0925070bcead8650dff.tar.lz
nixlib-a304fc5d753d0d2c134bf0925070bcead8650dff.tar.xz
nixlib-a304fc5d753d0d2c134bf0925070bcead8650dff.tar.zst
nixlib-a304fc5d753d0d2c134bf0925070bcead8650dff.zip
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.
Diffstat (limited to 'nixos/modules/system')
-rw-r--r--nixos/modules/system/boot/systemd.nix39
1 files changed, 30 insertions, 9 deletions
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 <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 {
       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;