nixos/systemd: allow using writeShellApplication for systemd unit scripts

This commit is contained in:
r-vdp
2023-11-30 10:54:09 +01:00
parent 509b099fd4
commit 2b224f0e3c
4 changed files with 72 additions and 13 deletions

View File

@@ -386,18 +386,27 @@ in rec {
''}
''; # */
makeJobScript = name: text:
makeJobScript = { name, text, enableStrictShellChecks }:
let
scriptName = replaceStrings [ "\\" "@" ] [ "-" "_" ] (shellEscape name);
out = (pkgs.writeShellScriptBin scriptName ''
set -e
${text}
'').overrideAttrs (_: {
out = (
if ! enableStrictShellChecks then
pkgs.writeShellScriptBin scriptName ''
set -e
${text}
''
else
pkgs.writeShellApplication {
name = scriptName;
inherit text;
}
).overrideAttrs (_: {
# 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}";
});
in "${out}/bin/${scriptName}";
in lib.getExe out;
unitConfig = { config, name, options, ... }: {
config = {
@@ -448,10 +457,16 @@ in rec {
};
};
serviceConfig = { name, config, ... }: {
serviceConfig =
let
nixosConfig = config;
in
{ name, lib, config, ... }: {
config = {
name = "${name}.service";
environment.PATH = mkIf (config.path != []) "${makeBinPath config.path}:${makeSearchPathOutput "bin" "sbin" config.path}";
enableStrictShellChecks = lib.mkOptionDefault nixosConfig.systemd.enableStrictShellChecks;
};
};