nixos/systemd: introduce settingsToSections util, migrate existing rfc42 options to use settingsToSections, migrate oomd to rfc42 (#437477)

This commit is contained in:
Will Fancher
2025-09-12 03:41:59 +00:00
committed by GitHub
5 changed files with 50 additions and 61 deletions

View File

@@ -349,6 +349,15 @@ rec {
)
);
settingsToSections =
settings:
concatStringsSep "\n" (
mapAttrsToList (section_name: section_attrs: ''
[${section_name}]
${attrsToSection section_attrs}
'') settings
);
generateUnits =
{
allowCollisions ? true,
@@ -723,10 +732,7 @@ rec {
commonUnitText =
def: lines:
''
[Unit]
${attrsToSection def.unitConfig}
''
(settingsToSections { Unit = def.unitConfig; })
+ lines
+ optionalString (def.wantedBy != [ ]) ''
@@ -744,10 +750,7 @@ rec {
enable
overrideStrategy
;
text = ''
[Unit]
${attrsToSection def.unitConfig}
'';
text = (settingsToSections { Unit = def.unitConfig; });
};
serviceToUnit = def: {
@@ -831,10 +834,9 @@ rec {
enable
overrideStrategy
;
text = commonUnitText def ''
[Timer]
${attrsToSection def.timerConfig}
'';
text = commonUnitText def (settingsToSections {
Timer = def.timerConfig;
});
};
pathToUnit = def: {
@@ -847,10 +849,9 @@ rec {
enable
overrideStrategy
;
text = commonUnitText def ''
[Path]
${attrsToSection def.pathConfig}
'';
text = commonUnitText def (settingsToSections {
Path = def.pathConfig;
});
};
mountToUnit = def: {
@@ -863,10 +864,9 @@ rec {
enable
overrideStrategy
;
text = commonUnitText def ''
[Mount]
${attrsToSection def.mountConfig}
'';
text = commonUnitText def (settingsToSections {
Mount = def.mountConfig;
});
};
automountToUnit = def: {
@@ -879,10 +879,9 @@ rec {
enable
overrideStrategy
;
text = commonUnitText def ''
[Automount]
${attrsToSection def.automountConfig}
'';
text = commonUnitText def (settingsToSections {
Automount = def.automountConfig;
});
};
sliceToUnit = def: {
@@ -895,10 +894,9 @@ rec {
enable
overrideStrategy
;
text = commonUnitText def ''
[Slice]
${attrsToSection def.sliceConfig}
'';
text = commonUnitText def (settingsToSections {
Slice = def.sliceConfig;
});
};
# Create a directory that contains systemd definition files from an attrset

View File

@@ -24,7 +24,7 @@ let
mountToUnit
automountToUnit
sliceToUnit
attrsToSection
settingsToSections
;
upstreamSystemUnits = [
@@ -599,10 +599,7 @@ in
upstreamWants = upstreamSystemWants;
};
"systemd/system.conf".text = ''
[Manager]
${attrsToSection cfg.settings.Manager}
'';
"systemd/system.conf".text = settingsToSections cfg.settings;
"systemd/sleep.conf".text = ''
[Sleep]

View File

@@ -22,7 +22,7 @@ let
timerToUnit
mountToUnit
automountToUnit
attrsToSection
settingsToSections
;
cfg = config.boot.initrd.systemd;
@@ -484,10 +484,7 @@ in
"/init".source = "${cfg.package}/lib/systemd/systemd";
"/etc/systemd/system".source = stage1Units;
"/etc/systemd/system.conf".text = ''
[Manager]
${attrsToSection cfg.settings.Manager}
'';
"/etc/systemd/system.conf".text = settingsToSections cfg.settings;
# We can use either ! or * to lock the root account in the
# console, but some software like OpenSSH won't even allow you

View File

@@ -1,7 +1,6 @@
{
config,
lib,
pkgs,
utils,
...
}:
@@ -62,10 +61,8 @@
"user-runtime-dir@.service"
];
environment.etc."systemd/logind.conf".text = ''
[Login]
${utils.systemdUtils.lib.attrsToSection config.services.logind.settings.Login}
'';
environment.etc."systemd/logind.conf".text =
utils.systemdUtils.lib.settingsToSections config.services.logind.settings;
# Restarting systemd-logind breaks X11
# - upstream commit: https://cgit.freedesktop.org/xorg/xserver/commit/?id=dc48bd653c7e101

View File

@@ -1,4 +1,9 @@
{ config, lib, ... }:
{
config,
lib,
utils,
...
}:
let
cfg = config.systemd.oomd;
@@ -10,6 +15,7 @@ in
[ "systemd" "oomd" "enableUserServices" ]
[ "systemd" "oomd" "enableUserSlices" ]
)
(lib.mkRenamedOptionModule [ "systemd" "oomd" "extraConfig" ] [ "systemd" "oomd" "settings" "OOM" ])
];
options.systemd.oomd = {
@@ -23,20 +29,18 @@ in
enableSystemSlice = lib.mkEnableOption "oomd on the system slice (`system.slice`)";
enableUserSlices = lib.mkEnableOption "oomd on all user slices (`user@.slice`) and all user owned slices";
extraConfig = lib.mkOption {
type =
with lib.types;
attrsOf (oneOf [
str
int
bool
]);
default = { };
example = lib.literalExpression ''{ DefaultMemoryPressureDurationSec = "20s"; }'';
settings.OOM = lib.mkOption {
description = ''
Extra config options for `systemd-oomd`. See {command}`man oomd.conf`
for available options.
Settings option for systemd-oomd.
See {manpage}`oomd.conf(5)` for available options.
'';
type = lib.types.submodule {
freeformType = lib.types.attrsOf utils.systemdUtils.unitOptions.unitOption;
};
default = { };
example = {
DefaultMemoryPressureLimit = "60%";
};
};
};
@@ -52,11 +56,7 @@ in
];
systemd.services.systemd-oomd.wantedBy = [ "multi-user.target" ];
environment.etc."systemd/oomd.conf".text = lib.generators.toINI { } {
OOM = cfg.extraConfig;
};
systemd.oomd.extraConfig.DefaultMemoryPressureDurationSec = lib.mkDefault "20s"; # Fedora default
environment.etc."systemd/oomd.conf".text = utils.systemdUtils.lib.settingsToSections cfg.settings;
users.users.systemd-oom = {
description = "systemd-oomd service user";