diff --git a/modules/systemd.nix b/modules/systemd.nix index 43ac60a4..af4071c7 100644 --- a/modules/systemd.nix +++ b/modules/systemd.nix @@ -46,16 +46,29 @@ let filename = "${name}.${style}"; pathSafeName = mkPathSafeName filename; - # Needed because systemd derives unit names from the ultimate - # link target. + # The actual unit content after filtering. + finalUnit = + let + # Filters out fields that are set to `null` or empty list. + shouldKeepField = + section: key: value: + value != null && value != [ ]; + + # Filters out empty sections. + shouldKeepSection = _: value: value != { }; + + inherit (lib) mapAttrs filterAttrs; + + filteredFields = mapAttrs (section: filterAttrs (shouldKeepField section)) serviceCfg; + filteredSections = filterAttrs shouldKeepSection filteredFields; + in + filteredSections; + + # Needed because systemd derives unit names from the ultimate link target. source = pkgs.writeTextFile { name = pathSafeName; - text = toSystemdIni ( - lib.filterAttrs (_: v: v != { }) ( - lib.mapAttrs (_: lib.filterAttrs (_: v: v != null && v != [ ])) serviceCfg - ) - ); + text = toSystemdIni finalUnit; destination = "/${filename}"; } + "/${filename}";