about summary refs log tree commit diff
path: root/nixos/lib/systemd-unit-options.nix
diff options
context:
space:
mode:
authorJanne Heß <janne@hess.ooo>2022-04-04 10:04:45 +0100
committerJanne Heß <janne@hess.ooo>2022-04-04 11:44:31 +0100
commit4b9efea2554e035ec03f73fb922eab16db8c7fb3 (patch)
tree00c195d46ae079bc8b8a0e0bb625526728046c6d /nixos/lib/systemd-unit-options.nix
parentff5da2c34a2f48354d6cd8d25223477f5359c2ba (diff)
nixos/stage-1-systemd: Implement job scripts
Diffstat (limited to 'nixos/lib/systemd-unit-options.nix')
-rw-r--r--nixos/lib/systemd-unit-options.nix177
1 files changed, 93 insertions, 84 deletions
diff --git a/nixos/lib/systemd-unit-options.nix b/nixos/lib/systemd-unit-options.nix
index c9d424d391196..35831f4b7f44b 100644
--- a/nixos/lib/systemd-unit-options.nix
+++ b/nixos/lib/systemd-unit-options.nix
@@ -250,7 +250,7 @@ in rec {
   };
   stage1CommonUnitOptions = commonUnitOptions;
 
-  serviceOptions = { options = {
+  serviceOptions = { name, config, ... }: { options = {
 
     environment = mkOption {
       default = {};
@@ -284,71 +284,107 @@ in rec {
       '';
     };
 
-  }; };
+    script = mkOption {
+      type = types.lines;
+      default = "";
+      description = "Shell commands executed as the service's main process.";
+    };
 
-  stage2ServiceOptions = { name, config, ... }: {
-    imports = [
-      stage2CommonUnitOptions
-      serviceOptions
-    ];
+    scriptArgs = mkOption {
+      type = types.str;
+      default = "";
+      description = "Arguments passed to the main process script.";
+    };
 
-    options = {
-      script = mkOption {
-        type = types.lines;
-        default = "";
-        description = "Shell commands executed as the service's main process.";
-      };
+    preStart = mkOption {
+      type = types.lines;
+      default = "";
+      description = ''
+        Shell commands executed before the service's main process
+        is started.
+      '';
+    };
 
-      scriptArgs = mkOption {
-        type = types.str;
-        default = "";
-        description = "Arguments passed to the main process script.";
-      };
+    postStart = mkOption {
+      type = types.lines;
+      default = "";
+      description = ''
+        Shell commands executed after the service's main process
+        is started.
+      '';
+    };
 
-      preStart = mkOption {
-        type = types.lines;
-        default = "";
-        description = ''
-          Shell commands executed before the service's main process
-          is started.
-        '';
-      };
+    reload = mkOption {
+      type = types.lines;
+      default = "";
+      description = ''
+        Shell commands executed when the service's main process
+        is reloaded.
+      '';
+    };
 
-      postStart = mkOption {
-        type = types.lines;
-        default = "";
-        description = ''
-          Shell commands executed after the service's main process
-          is started.
-        '';
-      };
+    preStop = mkOption {
+      type = types.lines;
+      default = "";
+      description = ''
+        Shell commands executed to stop the service.
+      '';
+    };
 
-      reload = mkOption {
-        type = types.lines;
-        default = "";
-        description = ''
-          Shell commands executed when the service's main process
-          is reloaded.
-        '';
-      };
+    postStop = mkOption {
+      type = types.lines;
+      default = "";
+      description = ''
+        Shell commands executed after the service's main process
+        has exited.
+      '';
+    };
 
-      preStop = mkOption {
-        type = types.lines;
-        default = "";
-        description = ''
-          Shell commands executed to stop the service.
-        '';
-      };
+    jobScripts = mkOption {
+      type = with types; coercedTo path singleton (listOf path);
+      internal = true;
+      description = "A list of all job script derivations of this unit.";
+      default = [];
+    };
 
-      postStop = mkOption {
-        type = types.lines;
-        default = "";
-        description = ''
-          Shell commands executed after the service's main process
-          has exited.
-        '';
-      };
+  };
+
+  config = mkMerge [
+    (mkIf (config.preStart != "") rec {
+      jobScripts = makeJobScript "${name}-pre-start" config.preStart;
+      serviceConfig.ExecStartPre = [ jobScripts ];
+    })
+    (mkIf (config.script != "") rec {
+      jobScripts = makeJobScript "${name}-start" config.script;
+      serviceConfig.ExecStart = jobScripts + " " + config.scriptArgs;
+    })
+    (mkIf (config.postStart != "") rec {
+      jobScripts = (makeJobScript "${name}-post-start" config.postStart);
+      serviceConfig.ExecStartPost = [ jobScripts ];
+    })
+    (mkIf (config.reload != "") rec {
+      jobScripts = makeJobScript "${name}-reload" config.reload;
+      serviceConfig.ExecReload = jobScripts;
+    })
+    (mkIf (config.preStop != "") rec {
+      jobScripts = makeJobScript "${name}-pre-stop" config.preStop;
+      serviceConfig.ExecStop = jobScripts;
+    })
+    (mkIf (config.postStop != "") rec {
+      jobScripts = makeJobScript "${name}-post-stop" config.postStop;
+      serviceConfig.ExecStopPost = jobScripts;
+    })
+  ];
+
+  };
 
+  stage2ServiceOptions = {
+    imports = [
+      stage2CommonUnitOptions
+      serviceOptions
+    ];
+
+    options = {
       restartIfChanged = mkOption {
         type = types.bool;
         default = true;
@@ -404,33 +440,6 @@ in rec {
         apply = v: if isList v then v else [ v ];
       };
     };
-
-    config = mkMerge
-      [ (mkIf (config.preStart != "")
-          { serviceConfig.ExecStartPre =
-              [ (makeJobScript "${name}-pre-start" config.preStart) ];
-          })
-        (mkIf (config.script != "")
-          { serviceConfig.ExecStart =
-              makeJobScript "${name}-start" config.script + " " + config.scriptArgs;
-          })
-        (mkIf (config.postStart != "")
-          { serviceConfig.ExecStartPost =
-              [ (makeJobScript "${name}-post-start" config.postStart) ];
-          })
-        (mkIf (config.reload != "")
-          { serviceConfig.ExecReload =
-              makeJobScript "${name}-reload" config.reload;
-          })
-        (mkIf (config.preStop != "")
-          { serviceConfig.ExecStop =
-              makeJobScript "${name}-pre-stop" config.preStop;
-          })
-        (mkIf (config.postStop != "")
-          { serviceConfig.ExecStopPost =
-              makeJobScript "${name}-post-stop" config.postStop;
-          })
-      ];
   };
 
   stage1ServiceOptions = {