From 5822d03851cfd9e8e7449c05c2cea2232e52cc42 Mon Sep 17 00:00:00 2001 From: Kirill Elagin Date: Fri, 17 Apr 2020 10:12:34 +0300 Subject: [PATCH 1/4] systemd: Simplify unit script names Current journal output from services started by `script` rather than `ExexStart` is unreadable because the name of the file (which journalctl records and outputs) quite literally takes 1/3 of the screen (on smaller screens). Make it shorter. In particular: * Drop the `unit-script` prefix as it is not very useful. * Use `writeShellScriptBin` to write them because: * It has a `checkPhase` which is better than no checkPhase. * The script itself ends up having a short name. --- nixos/modules/system/boot/systemd.nix | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/nixos/modules/system/boot/systemd.nix b/nixos/modules/system/boot/systemd.nix index 7f207e6c7ef4..7682b26e5b7f 100644 --- a/nixos/modules/system/boot/systemd.nix +++ b/nixos/modules/system/boot/systemd.nix @@ -201,8 +201,13 @@ let ]; makeJobScript = name: text: - let mkScriptName = s: "unit-script-" + (replaceChars [ "\\" "@" ] [ "-" "_" ] (shellEscape s) ); - in pkgs.writeTextFile { name = mkScriptName name; executable = true; inherit text; }; + let + scriptName = replaceChars [ "\\" "@" ] [ "-" "_" ] (shellEscape name); + out = pkgs.writeShellScriptBin scriptName '' + set -e + ${text} + ''; + in "${out}/bin/${scriptName}"; unitConfig = { config, options, ... }: { config = { From f1a78e1b5ea83d4875490d823e2ea5903edd282b Mon Sep 17 00:00:00 2001 From: Kirill Elagin Date: Fri, 17 Apr 2020 13:35:29 +0300 Subject: [PATCH 2/4] fixup! systemd: Simplify unit script names --- nixos/modules/system/boot/systemd.nix | 6 ------ 1 file changed, 6 deletions(-) diff --git a/nixos/modules/system/boot/systemd.nix b/nixos/modules/system/boot/systemd.nix index 7682b26e5b7f..caa3435cfcda 100644 --- a/nixos/modules/system/boot/systemd.nix +++ b/nixos/modules/system/boot/systemd.nix @@ -256,37 +256,31 @@ let } (mkIf (config.preStart != "") { serviceConfig.ExecStartPre = makeJobScript "${name}-pre-start" '' - #! ${pkgs.runtimeShell} -e ${config.preStart} ''; }) (mkIf (config.script != "") { serviceConfig.ExecStart = makeJobScript "${name}-start" '' - #! ${pkgs.runtimeShell} -e ${config.script} '' + " " + config.scriptArgs; }) (mkIf (config.postStart != "") { serviceConfig.ExecStartPost = makeJobScript "${name}-post-start" '' - #! ${pkgs.runtimeShell} -e ${config.postStart} ''; }) (mkIf (config.reload != "") { serviceConfig.ExecReload = makeJobScript "${name}-reload" '' - #! ${pkgs.runtimeShell} -e ${config.reload} ''; }) (mkIf (config.preStop != "") { serviceConfig.ExecStop = makeJobScript "${name}-pre-stop" '' - #! ${pkgs.runtimeShell} -e ${config.preStop} ''; }) (mkIf (config.postStop != "") { serviceConfig.ExecStopPost = makeJobScript "${name}-post-stop" '' - #! ${pkgs.runtimeShell} -e ${config.postStop} ''; }) From a9e9d37fc8b7df73d68f9ed78f25da15a40a1c83 Mon Sep 17 00:00:00 2001 From: Kirill Elagin Date: Fri, 17 Apr 2020 13:55:48 +0300 Subject: [PATCH 3/4] systemd: Add prefix to unit script derivations Add a distinctive `unit-script` prefix to systemd unit scripts to make them easier to find in the store directory. Do not add this prefix to actual script file name as it clutters logs. --- nixos/modules/system/boot/systemd.nix | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/nixos/modules/system/boot/systemd.nix b/nixos/modules/system/boot/systemd.nix index caa3435cfcda..516f8ed75411 100644 --- a/nixos/modules/system/boot/systemd.nix +++ b/nixos/modules/system/boot/systemd.nix @@ -203,10 +203,20 @@ let makeJobScript = name: text: let scriptName = replaceChars [ "\\" "@" ] [ "-" "_" ] (shellEscape name); - out = pkgs.writeShellScriptBin scriptName '' - set -e - ${text} - ''; + out = pkgs.writeTextFile { + # The derivation name is different from the script file name + # to keep the script file name short to avoid cluttering logs. + name = "unit-script-${scriptName}"; + executable = true; + destination = "/bin/${scriptName}"; + text = '' + #!${pkgs.runtimeShell} -e + ${text} + ''; + checkPhase = '' + ${pkgs.stdenv.shell} -n "$out/bin/${scriptName}" + ''; + }; in "${out}/bin/${scriptName}"; unitConfig = { config, options, ... }: { From daac85d9916ea095eadf5c89d685dd9057c8d48f Mon Sep 17 00:00:00 2001 From: Kirill Elagin Date: Mon, 20 Apr 2020 11:42:47 +0300 Subject: [PATCH 4/4] fixup! systemd: Add prefix to unit script derivations * Avoid extra string interpolation. --- nixos/modules/system/boot/systemd.nix | 30 +++++++++++---------------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/nixos/modules/system/boot/systemd.nix b/nixos/modules/system/boot/systemd.nix index 516f8ed75411..e702fed8c3f4 100644 --- a/nixos/modules/system/boot/systemd.nix +++ b/nixos/modules/system/boot/systemd.nix @@ -265,34 +265,28 @@ let environment.PATH = config.path; } (mkIf (config.preStart != "") - { serviceConfig.ExecStartPre = makeJobScript "${name}-pre-start" '' - ${config.preStart} - ''; + { serviceConfig.ExecStartPre = + makeJobScript "${name}-pre-start" config.preStart; }) (mkIf (config.script != "") - { serviceConfig.ExecStart = makeJobScript "${name}-start" '' - ${config.script} - '' + " " + config.scriptArgs; + { serviceConfig.ExecStart = + makeJobScript "${name}-start" config.script + " " + config.scriptArgs; }) (mkIf (config.postStart != "") - { serviceConfig.ExecStartPost = makeJobScript "${name}-post-start" '' - ${config.postStart} - ''; + { serviceConfig.ExecStartPost = + makeJobScript "${name}-post-start" config.postStart; }) (mkIf (config.reload != "") - { serviceConfig.ExecReload = makeJobScript "${name}-reload" '' - ${config.reload} - ''; + { serviceConfig.ExecReload = + makeJobScript "${name}-reload" config.reload; }) (mkIf (config.preStop != "") - { serviceConfig.ExecStop = makeJobScript "${name}-pre-stop" '' - ${config.preStop} - ''; + { serviceConfig.ExecStop = + makeJobScript "${name}-pre-stop" config.preStop; }) (mkIf (config.postStop != "") - { serviceConfig.ExecStopPost = makeJobScript "${name}-post-stop" '' - ${config.postStop} - ''; + { serviceConfig.ExecStopPost = + makeJobScript "${name}-post-stop" config.postStop; }) ]; };