systemd: add packages option (#8540)

* systemd: add `packages` option

The `systemd.user.packages` option is the Home Manager equivalent of
NixOS’s `systemd.packages` option and provides a way to specify packages
providing systemd user units.

This option is similar to `dbus.packages`.
* systemd: only create the parent directory if there are packages to symlink
This commit is contained in:
Arthur Khashaev 2026-01-15 02:51:28 +03:00 committed by GitHub
parent 0b24f3a487
commit 27613c7299
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1,13 @@
{ pkgs, ... }:
{
time = "2026-01-11T04:00:11+00:00";
condition = pkgs.stdenv.hostPlatform.isLinux;
message = ''
A new option is available: `systemd.user.packages`.
This option is the Home Manager equivalent of NixOSs `systemd.packages`
option and provides a way to specify packages providing systemd user units.
This option is similar to `dbus.packages`.
'';
}

View file

@ -249,6 +249,20 @@ in
'';
};
packages = mkOption {
type = with types; listOf package;
default = [ ];
description = ''
Packages providing systemd user units.
This is the Home Manager equivalent of NixOSs `systemd.packages`
option.
Files in {file}`«pkg»/share/systemd/user` will be included in the
users {file}`$XDG_DATA_HOME/systemd/user` directory.
'';
};
services = mkOption {
default = { };
type = serviceType;
@ -450,6 +464,17 @@ in
settings
];
xdg.dataFile = lib.mkIf (cfg.packages != [ ]) {
"systemd/user" = {
recursive = true;
source = pkgs.symlinkJoin {
name = "user-systemd-units";
paths = cfg.packages;
stripPrefix = "/share/systemd/user";
};
};
};
# Run systemd service reload if user is logged in. If we're
# running this from the NixOS module then XDG_RUNTIME_DIR is not
# set and systemd commands will fail. We'll therefore have to

View file

@ -1,5 +1,6 @@
{
systemd-services = ./services.nix;
systemd-packages = ./packages.nix;
systemd-services-disabled-for-root = ./services-disabled-for-root.nix;
systemd-session-variables = ./session-variables.nix;
systemd-user-config = ./user-config.nix;

View file

@ -0,0 +1,20 @@
{ pkgs, config, ... }:
let
package = config.lib.test.mkStubPackage {
buildScript = ''
mkdir -p $out/share/systemd/user
> $out/share/systemd/user/dummy.service cat <<EOF
[Service]
ExecStart=$out/bin/nonexistent
EOF
'';
};
in
{
systemd.user.packages = [ package ];
nmt.script = ''
serviceFile=home-files/.local/share/systemd/user/dummy.service
assertFileExists "$serviceFile"
assertFileContent "$serviceFile" ${package}/share/systemd/user/dummy.service
'';
}