Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional type for systemd(timers, paths, etc) #3

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
23 changes: 13 additions & 10 deletions nixproc/backends/systemd/create-systemd-service.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
name
# An attribute set specifying arbitrary environment variables
, environment ? {}
, unitType ? "service"
# List of supervisord services that this configuration depends on.
# These properties are translated to Wants= and After= properties to ensure
# proper activation ordering and that the dependencies are started first
Expand All @@ -36,7 +37,7 @@ let
inherit lib;
};

sections = removeAttrs args [ "name" "environment" "dependencies" "path" "credentials" "postInstall" ];
sections = removeAttrs args [ "unitType" "name" "environment" "dependencies" "path" "credentials" "postInstall" ];

_environment = util.appendPathToEnvironment {
inherit environment;
Expand All @@ -56,8 +57,8 @@ let
if dependencies == [] then ""
else
''
Wants=${toString (map (dependency: "${dependency.name}.service") dependencies)}
After=${toString (map (dependency: "${dependency.name}.service") dependencies)}
Wants=${toString (map (dependency: "${dependency.name}.${dependency.unitType}") dependencies)}
After=${toString (map (dependency: "${dependency.name}.${dependency.unitType}") dependencies)}
'';

generateSection = {title, properties}:
Expand All @@ -69,7 +70,7 @@ let
value = builtins.getAttr name properties;
in
if forceDisableUserChange && (name == "User" || name == "Group") then "" else # Don't change user privileges when we force it to be disabled
''${name}=${toString value}
''${if builtins.isList value then lib.strings.concatMapStringsSep "\n" (x: "${name}=${toString x}") value else "${name}=${toString value}"}
''
) (builtins.attrNames properties)}''
+ (if title == "Service" then generateEnvironmentVariables _environment else "")
Expand All @@ -85,11 +86,11 @@ let
}
) (builtins.attrNames sections);

service = writeTextFile {
name = "${name}.service";
systemdFile = writeTextFile {
name = "${name}.${unitType}";
text = ''
${generateSections sections}
${lib.optionalString (!(sections ? Service) && _environment != {}) ''
${lib.optionalString (!(sections ? Service) && _environment != {} && unitType == "service") ''
[Service]

${generateEnvironmentVariables _environment}''}
Expand All @@ -106,15 +107,17 @@ in
stdenv.mkDerivation {
name = "${prefix}${name}";

inherit unitType;

buildCommand = ''
mkdir -p $out/etc/systemd/system
ln -s ${service} $out/etc/systemd/system/${prefix}${name}.service
ln -s ${systemdFile} $out/etc/systemd/system/${prefix}${name}.${unitType}

${lib.optionalString (dependencies != []) ''
mkdir -p $out/etc/systemd/system/${prefix}${name}.service.wants
mkdir -p $out/etc/systemd/system/${prefix}${name}.${unitType}.wants

${lib.concatMapStrings (dependency: ''
ln -s ${dependency}/etc/systemd/system/${dependency.name}.service $out/etc/systemd/system/${prefix}${name}.service.wants
ln -s ${dependency}/etc/systemd/system/${dependency.name}.${dependency.unitType} $out/etc/systemd/system/${prefix}${name}.${unitType}.wants
'') dependencies}
''}

Expand Down
40 changes: 33 additions & 7 deletions tools/systemd/nixproc-systemd-deploy.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/bash
set -e
shopt -s nullglob

showUsage()
{
cat <<EOF
Expand Down Expand Up @@ -143,8 +142,11 @@ oldunits=()

if [ -d "$oldProfilePath" ]
then
for i in $oldProfilePath/etc/systemd/system/*.service
for i in $oldProfilePath/etc/systemd/system/*
do
if [[ "$i" =~ .wants ]]; then
continue
fi
currentPath=$(readlink -f "$i")
oldunits+=($currentPath)
done
Expand All @@ -154,8 +156,11 @@ fi

newunits=()

for i in $profilePath/etc/systemd/system/*.service
for i in $profilePath/etc/systemd/system/*
do
if [[ "$i" =~ .wants ]]; then
continue
fi
currentPath=$(readlink -f "$i")
newunits+=($currentPath)
done
Expand All @@ -168,8 +173,11 @@ if [ "$oldProfilePath" != "" ]
then
# Stop obsolete units

for i in $oldProfilePath/etc/systemd/system/*.service
for i in $oldProfilePath/etc/systemd/system/*
do
if [[ "$i" =~ .wants ]]; then
continue
fi
if ! containsElement "$(readlink -f "$i")" "${newunits[@]}"
then
systemctl $systemdUserArg stop "$(basename "$i")"
Expand All @@ -178,8 +186,11 @@ then

# Remove obsolete units

for i in $oldProfilePath/etc/systemd/system/*.service
for i in $oldProfilePath/etc/systemd/system/*
do
if [[ "$i" =~ .wants ]]; then
continue
fi
if ! containsElement "$(readlink -f "$i")" "${newunits[@]}"
then
unitTargetPath="$SYSTEMD_TARGET_DIR/$(basename "$i")"
Expand All @@ -195,8 +206,11 @@ fi

# Add new units

for i in $profilePath/etc/systemd/system/*.service
for i in $profilePath/etc/systemd/system/*
do
if [[ "$i" =~ .wants ]]; then
continue
fi
if ! containsElement "$(readlink -f "$i")" "${oldunits[@]}"
then
if [ -d "$i.wants" ]
Expand All @@ -208,12 +222,24 @@ do
fi
done

# Stop all units
for i in $profilePath/etc/systemd/system/*
do
if [[ "$i" =~ .wants ]]; then
continue
fi
systemctl $systemdUserArg stop "$(basename "$i")" || true
done

# Reload the systemd configuration
systemctl $systemdUserArg daemon-reload

# Start all units in the new configuration
for i in $profilePath/etc/systemd/system/*.service
for i in $profilePath/etc/systemd/system/*
do
if [[ "$i" =~ .wants ]]; then
continue
fi
systemctl $systemdUserArg start "$(basename "$i")"
done

Expand Down