1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 11:36:05 +01:00

systemd: make unit definitions freeform modules

We add types for a few of the most common attributes.
This commit is contained in:
Robert Helgesson 2025-08-16 23:18:03 +02:00
parent 12dfb0cff9
commit 5c14260fad
No known key found for this signature in database
GPG key ID: 96E745BD17AA17ED

View file

@ -51,7 +51,11 @@ let
source = source =
pkgs.writeTextFile { pkgs.writeTextFile {
name = pathSafeName; name = pathSafeName;
text = toSystemdIni serviceCfg; text = toSystemdIni (
lib.filterAttrs (_: v: v != { }) (
lib.mapAttrs (_: lib.filterAttrs (_: v: v != null && v != [ ])) serviceCfg
)
);
destination = "/${filename}"; destination = "/${filename}";
} }
+ "/${filename}"; + "/${filename}";
@ -73,22 +77,118 @@ let
servicesStartTimeoutMs = builtins.toString cfg.servicesStartTimeoutMs; servicesStartTimeoutMs = builtins.toString cfg.servicesStartTimeoutMs;
unitType = unitBaseType =
unitKind: unitKind: mod:
with types; types.submodule {
let freeformType =
primitive = oneOf [ with types;
bool let
int primitive = oneOf [
str bool
path int
str
path
];
in
attrsOf (attrsOf (either primitive (listOf primitive)))
// {
description = "systemd ${unitKind} unit configuration";
};
imports = [
{
options.Unit = {
Description = mkOption {
type = types.nullOr types.str;
default = null;
example = "My daily database backup";
description = "A short human-readable label of the unit.";
};
Documentation = mkOption {
type = with types; coercedTo str lib.toList (listOf str);
example = [ "my-${unitKind}.${unitKind}" ];
default = [ ];
description = "List of URIs referencing documentation for the unit.";
};
};
}
mod
]; ];
in
attrsOf (attrsOf (attrsOf (either primitive (listOf primitive))))
// {
description = "systemd ${unitKind} unit configuration";
}; };
unitType = unitKind: types.attrsOf (unitBaseType unitKind { });
serviceType = types.attrsOf (
unitBaseType "service" {
options = {
Unit = {
X-Reload-Triggers = mkOption {
type = with types; listOf (either package str);
default = [ ];
example = literalExpression ''[ config.xdg.configFile."service.conf".source ]'';
description = ''
List of free form strings that can be used to trigger a service
reload during Home Manager activation.
'';
};
X-Restart-Triggers = mkOption {
type = with types; listOf (either package str);
default = [ ];
example = literalExpression ''[ config.xdg.configFile."service.conf".source ]'';
description = ''
List of free form strings that can be used to trigger a service
restart during Home Manager activation.
'';
};
X-SwitchMethod = mkOption {
type = types.enum [
null
"reload"
"restart"
"stop-start"
"keep-old"
];
default = null;
example = literalExpression ''[ "''${config.xdg.configFile."service.conf".source}" ]'';
description = ''
The preferred method to use when switching from an old to a new
version of this service.
'';
};
};
Service = {
Environment = mkOption {
type = with types; coercedTo str lib.toList (listOf str);
default = [ ];
example = [
"VAR1=foo"
"VAR2=\"bar baz\""
];
description = "Environment variables available to executed processes.";
};
ExecStart = mkOption {
type =
with types;
let
primitive = either package str;
in
either primitive (listOf primitive);
apply = lib.toList;
default = [ ];
example = "/absolute/path/to/command arg1 arg2";
description = "Command that is executed when this service is started.";
};
};
};
}
);
unitDescription = type: '' unitDescription = type: ''
Definition of systemd per-user ${type} units. Attributes are Definition of systemd per-user ${type} units. Attributes are
merged recursively. merged recursively.
@ -151,7 +251,7 @@ in
services = mkOption { services = mkOption {
default = { }; default = { };
type = unitType "service"; type = serviceType;
description = (unitDescription "service"); description = (unitDescription "service");
example = unitExample "Service"; example = unitExample "Service";
}; };