systemd: bring more in line with upstream

This commit is contained in:
bb010g 2019-10-28 19:29:12 -07:00 committed by Robert Helgesson
parent c982c19f53
commit b6613a8544
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89

View file

@ -1,11 +1,14 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib;
let let
cfg = config.systemd.user; cfg = config.systemd.user;
inherit (lib) getAttr hm isBool literalExample mkIf mkMerge mkOption types;
# From <nixpkgs/nixos/modules/system/boot/systemd-lib.nix>
mkPathSafeName = lib.replaceChars ["@" ":" "\\" "[" "]"] ["-" "-" "-" "" ""];
enabled = cfg.services != {} enabled = cfg.services != {}
|| cfg.slices != {} || cfg.slices != {}
|| cfg.sockets != {} || cfg.sockets != {}
@ -15,7 +18,7 @@ let
|| cfg.mounts != {} || cfg.mounts != {}
|| cfg.sessionVariables != {}; || cfg.sessionVariables != {};
toSystemdIni = generators.toINI { toSystemdIni = lib.generators.toINI {
listsAsDuplicateKeys = true; listsAsDuplicateKeys = true;
mkKeyValue = key: value: mkKeyValue = key: value:
let let
@ -29,16 +32,14 @@ let
buildService = style: name: serviceCfg: buildService = style: name: serviceCfg:
let let
filename = "${name}.${style}"; filename = "${name}.${style}";
pathSafeName = lib.replaceChars ["@" ":" "\\" "[" "]"] pathSafeName = mkPathSafeName filename;
["-" "-" "-" "" "" ]
filename;
# Needed because systemd derives unit names from the ultimate # Needed because systemd derives unit names from the ultimate
# link target. # link target.
source = pkgs.writeTextFile { source = pkgs.writeTextFile {
name = pathSafeName; name = pathSafeName;
text = toSystemdIni serviceCfg; text = toSystemdIni serviceCfg;
destination = "/${filename}"; destination = lib.escapeShellArg "/${filename}";
} + "/${filename}"; } + "/${filename}";
wantedBy = target: wantedBy = target:
@ -47,7 +48,7 @@ let
value = { inherit source; }; value = { inherit source; };
}; };
in in
singleton { lib.singleton {
name = "systemd/user/${filename}"; name = "systemd/user/${filename}";
value = { inherit source; }; value = { inherit source; };
} }
@ -55,7 +56,7 @@ let
map wantedBy (serviceCfg.Install.WantedBy or []); map wantedBy (serviceCfg.Install.WantedBy or []);
buildServices = style: serviceCfgs: buildServices = style: serviceCfgs:
concatLists (mapAttrsToList (buildService style) serviceCfgs); lib.concatLists (lib.mapAttrsToList (buildService style) serviceCfgs);
servicesStartTimeoutMs = builtins.toString cfg.servicesStartTimeoutMs; servicesStartTimeoutMs = builtins.toString cfg.servicesStartTimeoutMs;
@ -82,7 +83,7 @@ let
unitExample = type: literalExample '' unitExample = type: literalExample ''
{ {
${toLower type}-name = { ${lib.toLower type}-name = {
Unit = { Unit = {
Description = "Example description"; Description = "Example description";
Documentation = [ "man:example(1)" "man:example(5)" ]; Documentation = [ "man:example(1)" "man:example(5)" ];
@ -97,15 +98,15 @@ let
sessionVariables = mkIf (cfg.sessionVariables != {}) { sessionVariables = mkIf (cfg.sessionVariables != {}) {
"environment.d/10-home-manager.conf".text = "environment.d/10-home-manager.conf".text =
concatStringsSep "\n" ( lib.concatStringsSep "\n" (
mapAttrsToList (n: v: "${n}=${toString v}") cfg.sessionVariables lib.mapAttrsToList (n: v: "${n}=${toString v}") cfg.sessionVariables
) + "\n"; ) + "\n";
}; };
in in
{ {
meta.maintainers = [ maintainers.rycee ]; meta.maintainers = [ lib.maintainers.rycee ];
options = { options = {
systemd.user = { systemd.user = {
@ -244,8 +245,8 @@ in
assertion = enabled -> pkgs.stdenv.isLinux; assertion = enabled -> pkgs.stdenv.isLinux;
message = message =
let let
names = concatStringsSep ", " ( names = lib.concatStringsSep ", " (
attrNames ( lib.attrNames (
cfg.services // cfg.slices // cfg.sockets // cfg.targets cfg.services // cfg.slices // cfg.sockets // cfg.targets
// cfg.timers // cfg.paths // cfg.mounts // cfg.sessionVariables // cfg.timers // cfg.paths // cfg.mounts // cfg.sessionVariables
) )
@ -260,7 +261,7 @@ in
# available, in particular we assume that systemctl is in PATH. # available, in particular we assume that systemctl is in PATH.
(mkIf pkgs.stdenv.isLinux { (mkIf pkgs.stdenv.isLinux {
xdg.configFile = mkMerge [ xdg.configFile = mkMerge [
(listToAttrs ( (lib.listToAttrs (
(buildServices "service" cfg.services) (buildServices "service" cfg.services)
++ ++
(buildServices "slices" cfg.slices) (buildServices "slices" cfg.slices)