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

View File

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

View File

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

View File

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

View File

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