git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/stage-1-systemd: Implement job scripts
commit4b9efea2554e035ec03f73fb922eab16db8c7fb3 authorJanne Heß <janne@hess.ooo> dateMon 4 Apr 2022, 10:04 (5 years ago) parentff5da2c
2 files changed+95−85
nixos/lib/systemd-unit-options.nix+93−84 nixos/modules/system/boot/systemd/initrd.nix+2−1
nixos/lib/systemd-unit-options.nix+93−84
@@ -250,7 +250,7 @@ in rec {
250250 };
251251 stage1CommonUnitOptions = commonUnitOptions;
252252
253− serviceOptions = { options = {
253+ serviceOptions = { name, config, ... }: { options = {
254254
255255 environment = mkOption {
256256 default = {};
@@ -284,71 +284,107 @@ in rec {
284284 '';
285285 };
286286
287− }; };
287+ script = mkOption {
288+ type = types.lines;
289+ default = "";
290+ description = "Shell commands executed as the service's main process.";
291+ };
288292
289− stage2ServiceOptions = { name, config, ... }: {
290− imports = [
291− stage2CommonUnitOptions
292− serviceOptions
293− ];
293+ scriptArgs = mkOption {
294+ type = types.str;
295+ default = "";
296+ description = "Arguments passed to the main process script.";
297+ };
294298
295− options = {
296− script = mkOption {
297− type = types.lines;
298− default = "";
299− description = "Shell commands executed as the service's main process.";
300− };
299+ preStart = mkOption {
300+ type = types.lines;
301+ default = "";
302+ description = ''
303+ Shell commands executed before the service's main process
304+ is started.
305+ '';
306+ };
301307
302− scriptArgs = mkOption {
303− type = types.str;
304− default = "";
305− description = "Arguments passed to the main process script.";
306− };
308+ postStart = mkOption {
309+ type = types.lines;
310+ default = "";
311+ description = ''
312+ Shell commands executed after the service's main process
313+ is started.
314+ '';
315+ };
307316
308− preStart = mkOption {
309− type = types.lines;
310− default = "";
311− description = ''
312− Shell commands executed before the service's main process
313− is started.
314− '';
315− };
317+ reload = mkOption {
318+ type = types.lines;
319+ default = "";
320+ description = ''
321+ Shell commands executed when the service's main process
322+ is reloaded.
323+ '';
324+ };
316325
317− postStart = mkOption {
318− type = types.lines;
319− default = "";
320− description = ''
321− Shell commands executed after the service's main process
322− is started.
323− '';
324− };
326+ preStop = mkOption {
327+ type = types.lines;
328+ default = "";
329+ description = ''
330+ Shell commands executed to stop the service.
331+ '';
332+ };
325333
326− reload = mkOption {
327− type = types.lines;
328− default = "";
329− description = ''
330− Shell commands executed when the service's main process
331− is reloaded.
332− '';
333− };
334+ postStop = mkOption {
335+ type = types.lines;
336+ default = "";
337+ description = ''
338+ Shell commands executed after the service's main process
339+ has exited.
340+ '';
341+ };
334342
335− preStop = mkOption {
336− type = types.lines;
337− default = "";
338− description = ''
339− Shell commands executed to stop the service.
340− '';
341− };
343+ jobScripts = mkOption {
344+ type = with types; coercedTo path singleton (listOf path);
345+ internal = true;
346+ description = "A list of all job script derivations of this unit.";
347+ default = [];
348+ };
342349
343− postStop = mkOption {
344− type = types.lines;
345− default = "";
346− description = ''
347− Shell commands executed after the service's main process
348− has exited.
349− '';
350− };
350+ };
351+
352+ config = mkMerge [
353+ (mkIf (config.preStart != "") rec {
354+ jobScripts = makeJobScript "${name}-pre-start" config.preStart;
355+ serviceConfig.ExecStartPre = [ jobScripts ];
356+ })
357+ (mkIf (config.script != "") rec {
358+ jobScripts = makeJobScript "${name}-start" config.script;
359+ serviceConfig.ExecStart = jobScripts + " " + config.scriptArgs;
360+ })
361+ (mkIf (config.postStart != "") rec {
362+ jobScripts = (makeJobScript "${name}-post-start" config.postStart);
363+ serviceConfig.ExecStartPost = [ jobScripts ];
364+ })
365+ (mkIf (config.reload != "") rec {
366+ jobScripts = makeJobScript "${name}-reload" config.reload;
367+ serviceConfig.ExecReload = jobScripts;
368+ })
369+ (mkIf (config.preStop != "") rec {
370+ jobScripts = makeJobScript "${name}-pre-stop" config.preStop;
371+ serviceConfig.ExecStop = jobScripts;
372+ })
373+ (mkIf (config.postStop != "") rec {
374+ jobScripts = makeJobScript "${name}-post-stop" config.postStop;
375+ serviceConfig.ExecStopPost = jobScripts;
376+ })
377+ ];
378+
379+ };
351380
381+ stage2ServiceOptions = {
382+ imports = [
383+ stage2CommonUnitOptions
384+ serviceOptions
385+ ];
386+
387+ options = {
352388 restartIfChanged = mkOption {
353389 type = types.bool;
354390 default = true;
@@ -404,33 +440,6 @@ in rec {
404440 apply = v: if isList v then v else [ v ];
405441 };
406442 };
407−
408− config = mkMerge
409− [ (mkIf (config.preStart != "")
410− { serviceConfig.ExecStartPre =
411− [ (makeJobScript "${name}-pre-start" config.preStart) ];
412− })
413− (mkIf (config.script != "")
414− { serviceConfig.ExecStart =
415− makeJobScript "${name}-start" config.script + " " + config.scriptArgs;
416− })
417− (mkIf (config.postStart != "")
418− { serviceConfig.ExecStartPost =
419− [ (makeJobScript "${name}-post-start" config.postStart) ];
420− })
421− (mkIf (config.reload != "")
422− { serviceConfig.ExecReload =
423− makeJobScript "${name}-reload" config.reload;
424− })
425− (mkIf (config.preStop != "")
426− { serviceConfig.ExecStop =
427− makeJobScript "${name}-pre-stop" config.preStop;
428− })
429− (mkIf (config.postStop != "")
430− { serviceConfig.ExecStopPost =
431− makeJobScript "${name}-post-stop" config.postStop;
432− })
433− ];
434443 };
435444
436445 stage1ServiceOptions = {
nixos/modules/system/boot/systemd/initrd.nix+2−1
@@ -96,6 +96,7 @@ let
9696
9797 enabledUpstreamUnits = filter (n: ! elem n cfg.suppressedUnits) upstreamUnits;
9898 enabledUnits = filterAttrs (n: v: ! elem n cfg.suppressedUnits) cfg.units;
99+ jobScripts = concatLists (mapAttrsToList (_: unit: unit.jobScripts or []) (filterAttrs (_: v: v.enable) cfg.services));
99100
100101 stage1Units = generateUnits {
101102 type = "initrd";
@@ -378,7 +379,7 @@ in {
378379
379380 # so NSS can look up usernames
380381 "${pkgs.glibc}/lib/libnss_files.so"
381− ];
382+ ] ++ jobScripts;
382383
383384 targets.initrd.aliases = ["default.target"];
384385 units =