Merge pull request #186314 from ck3d/unit-add-map-control

nixos: Add option to influence override strategies in systemd units
This commit is contained in:
Florian Klink 2022-10-28 17:56:49 +02:00 committed by GitHub
commit eec71f9dee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 11 deletions

View file

@ -187,11 +187,14 @@ in rec {
done done
done done
# Symlink all units defined by systemd.units. If these are also # Symlink units defined by systemd.units where override strategy
# provided by systemd or systemd.packages, then add them as # shall be automatically detected. If these are also provided by
# systemd or systemd.packages, then add them as
# <unit-name>.d/overrides.conf, which makes them extend the # <unit-name>.d/overrides.conf, which makes them extend the
# upstream unit. # upstream unit.
for i in ${toString (mapAttrsToList (n: v: v.unit) units)}; do for i in ${toString (mapAttrsToList
(n: v: v.unit)
(lib.filterAttrs (n: v: (attrByPath [ "overrideStrategy" ] "asDropinIfExists" v) == "asDropinIfExists") units))}; do
fn=$(basename $i/*) fn=$(basename $i/*)
if [ -e $out/$fn ]; then if [ -e $out/$fn ]; then
if [ "$(readlink -f $i/$fn)" = /dev/null ]; then if [ "$(readlink -f $i/$fn)" = /dev/null ]; then
@ -210,6 +213,16 @@ in rec {
fi fi
done done
# Symlink units defined by systemd.units which shall be
# treated as drop-in file.
for i in ${toString (mapAttrsToList
(n: v: v.unit)
(lib.filterAttrs (n: v: v ? overrideStrategy && v.overrideStrategy == "asDropin") units))}; do
fn=$(basename $i/*)
mkdir -p $out/$fn.d
ln -s $i/$fn $out/$fn.d/overrides.conf
done
# Create service aliases from aliases option. # Create service aliases from aliases option.
${concatStrings (mapAttrsToList (name: unit: ${concatStrings (mapAttrsToList (name: unit:
concatMapStrings (name2: '' concatMapStrings (name2: ''
@ -340,7 +353,7 @@ in rec {
''; '';
targetToUnit = name: def: targetToUnit = name: def:
{ inherit (def) aliases wantedBy requiredBy enable; { inherit (def) aliases wantedBy requiredBy enable overrideStrategy;
text = text =
'' ''
[Unit] [Unit]
@ -349,7 +362,7 @@ in rec {
}; };
serviceToUnit = name: def: serviceToUnit = name: def:
{ inherit (def) aliases wantedBy requiredBy enable; { inherit (def) aliases wantedBy requiredBy enable overrideStrategy;
text = commonUnitText def + text = commonUnitText def +
'' ''
[Service] [Service]
@ -371,7 +384,7 @@ in rec {
}; };
socketToUnit = name: def: socketToUnit = name: def:
{ inherit (def) aliases wantedBy requiredBy enable; { inherit (def) aliases wantedBy requiredBy enable overrideStrategy;
text = commonUnitText def + text = commonUnitText def +
'' ''
[Socket] [Socket]
@ -382,7 +395,7 @@ in rec {
}; };
timerToUnit = name: def: timerToUnit = name: def:
{ inherit (def) aliases wantedBy requiredBy enable; { inherit (def) aliases wantedBy requiredBy enable overrideStrategy;
text = commonUnitText def + text = commonUnitText def +
'' ''
[Timer] [Timer]
@ -391,7 +404,7 @@ in rec {
}; };
pathToUnit = name: def: pathToUnit = name: def:
{ inherit (def) aliases wantedBy requiredBy enable; { inherit (def) aliases wantedBy requiredBy enable overrideStrategy;
text = commonUnitText def + text = commonUnitText def +
'' ''
[Path] [Path]
@ -400,7 +413,7 @@ in rec {
}; };
mountToUnit = name: def: mountToUnit = name: def:
{ inherit (def) aliases wantedBy requiredBy enable; { inherit (def) aliases wantedBy requiredBy enable overrideStrategy;
text = commonUnitText def + text = commonUnitText def +
'' ''
[Mount] [Mount]
@ -409,7 +422,7 @@ in rec {
}; };
automountToUnit = name: def: automountToUnit = name: def:
{ inherit (def) aliases wantedBy requiredBy enable; { inherit (def) aliases wantedBy requiredBy enable overrideStrategy;
text = commonUnitText def + text = commonUnitText def +
'' ''
[Automount] [Automount]
@ -418,7 +431,7 @@ in rec {
}; };
sliceToUnit = name: def: sliceToUnit = name: def:
{ inherit (def) aliases wantedBy requiredBy enable; { inherit (def) aliases wantedBy requiredBy enable overrideStrategy;
text = commonUnitText def + text = commonUnitText def +
'' ''
[Slice] [Slice]

View file

@ -48,6 +48,22 @@ in rec {
''; '';
}; };
overrideStrategy = mkOption {
default = "asDropinIfExists";
type = types.enum [ "asDropinIfExists" "asDropin" ];
description = lib.mdDoc ''
Defines how unit configuration is provided for systemd:
`asDropinIfExists` creates a unit file when no unit file is provided by the package
otherwise a drop-in file name `overrides.conf`.
`asDropin` creates a drop-in file named `overrides.conf`.
Mainly needed to define instances for systemd template units (e.g. `systemd-nspawn@mycontainer.service`).
See also systemd.unit(1).
'';
};
requiredBy = mkOption { requiredBy = mkOption {
default = []; default = [];
type = types.listOf unitNameType; type = types.listOf unitNameType;

View file

@ -44,6 +44,14 @@ import ./make-test-python.nix ({ pkgs, ... }:
# not needed, but we want to test the nspawn file generation # not needed, but we want to test the nspawn file generation
systemd.nspawn.${containerName} = { }; systemd.nspawn.${containerName} = { };
systemd.services."systemd-nspawn@${containerName}" = {
serviceConfig.Environment = [
# Disable tmpfs for /tmp
"SYSTEMD_NSPAWN_TMPFS_TMP=0"
];
overrideStrategy = "asDropin";
};
}; };
testScript = '' testScript = ''
@ -95,6 +103,9 @@ import ./make-test-python.nix ({ pkgs, ... }:
machine.succeed("machinectl stop ${containerName}"); machine.succeed("machinectl stop ${containerName}");
machine.wait_until_succeeds("test $(systemctl is-active systemd-nspawn@${containerName}) = inactive"); machine.wait_until_succeeds("test $(systemctl is-active systemd-nspawn@${containerName}) = inactive");
# Test tmpfs for /tmp
machine.fail("mountpoint /tmp");
# Show to to delete the container # Show to to delete the container
machine.succeed("chattr -i ${containerRoot}/var/empty"); machine.succeed("chattr -i ${containerRoot}/var/empty");
machine.succeed("rm -rf ${containerRoot}"); machine.succeed("rm -rf ${containerRoot}");