!PSmBFWNKoXmlQBzUQf:helsinki-systems.de

Stage 1 systemd

81 Members
systemd in NixOs's stage 1, replacing the current bash tooling https://github.com/NixOS/nixpkgs/projects/5125 Servers

Load older messages


SenderMessageTime
19 Mar 2022
@elvishjerricco:matrix.org@elvishjerricco:matrix.org

Ok... I'm just gonna keep truckin on the refactor branch for now then :P I did add a smaller unification of my own by placing the types into a common place, at least.

diff --git a/nixos/lib/systemd-types.nix b/nixos/lib/systemd-types.nix
new file mode 100644
index 00000000000..b303335ffc1
--- /dev/null
+++ b/nixos/lib/systemd-types.nix
@@ -0,0 +1,30 @@
+{ lib, systemdUtils }:
+
+with systemdUtils.lib;
+with systemdUtils.unitOptions;
+with lib;
+
+rec {
+  units = with types;
+    attrsOf (submodule ({ name, config, ... }: {
+      options = concreteUnitOptions;
+      config = { unit = mkDefault (systemdUtils.lib.makeUnit name config); };
+    }));
+
+  services = with types; attrsOf (submodule [ { options = serviceOptions; } unitConfig serviceConfig ]);
+  initrdServices = with types; attrsOf (submodule [ { options = serviceOptions; } unitConfig initrdServiceConfig ]);
+
+  targets = with types; attrsOf (submodule [ { options = targetOptions; } unitConfig ]);
+
+  sockets = with types; attrsOf (submodule [ { options = socketOptions; } unitConfig ]);
+
+  timers = with types; attrsOf (submodule [ { options = timerOptions; } unitConfig ]);
+
+  paths = with types; attrsOf (submodule [ { options = pathOptions; } unitConfig ]);
+
+  slices = with types; attrsOf (submodule [ { options = sliceOptions; } unitConfig ]);
+
+  mounts = with types; listOf (submodule [ { options = mountOptions; } unitConfig mountConfig ]);
+
+  automounts = with types; listOf (submodule [ { options = automountOptions; } unitConfig automountConfig ]);
+}
diff --git a/nixos/lib/utils.nix b/nixos/lib/utils.nix
index ae68c3920c5..80341dd48fc 100644
--- a/nixos/lib/utils.nix
+++ b/nixos/lib/utils.nix
@@ -197,5 +197,6 @@ rec {
   systemdUtils = {
     lib = import ./systemd-lib.nix { inherit lib config pkgs; };
     unitOptions = import ./systemd-unit-options.nix { inherit lib systemdUtils; };
+    types = import ./systemd-types.nix { inherit lib systemdUtils; };
   };
 }
diff --git a/nixos/modules/system/boot/systemd.nix b/nixos/modules/system/boot/systemd.nix
index 057474c607a..396457e1a9f 100644
--- a/nixos/modules/system/boot/systemd.nix
+++ b/nixos/modules/system/boot/systemd.nix
@@ -14,10 +14,6 @@ let
     makeUnit
     generateUnits
     makeJobScript
-    unitConfig
-    serviceConfig
-    mountConfig
-    automountConfig
     commonUnitText
     targetToUnit
     serviceToUnit
@@ -185,13 +181,7 @@ in
     systemd.units = mkOption {
       description = "Definition of systemd units.";
       default = {};
-      type = with types; attrsOf (submodule (
-        { name, config, ... }:
-        { options = concreteUnitOptions;
-          config = {
-            unit = mkDefault (makeUnit name config);
-          };
-        }));
+      type = systemdUtils.types.units;
     };
 
     systemd.packages = mkOption {
@@ -203,37 +193,37 @@ in
 
     systemd.targets = mkOption {
       default = {};
-      type = with types; attrsOf (submodule [ { options = targetOptions; } unitConfig] );
+      type = systemdUtils.types.targets;
       description = "Definition of systemd target units.";
     };
 
     systemd.services = mkOption {
       default = {};
-      type = with types; attrsOf (submodule [ { options = serviceOptions; } unitConfig serviceConfig ]);
+      type = systemdUtils.types.services;
       description = "Definition of systemd service units.";
     };
 
     systemd.sockets = mkOption {
       default = {};
-      type = with types; attrsOf (submodule [ { options = socketOptions; } unitConfig ]);
+      type = systemdUtils.types.sockets;
       description = "Definition of systemd socket units.";
     };
 
     systemd.timers = mkOption {
       default = {};
-      type = with types; attrsOf (submodule [ { options = timerOptions; } unitConfig ]);
+      type = systemdUtils.types.timers;
       description = "Definition of systemd timer units.";
     };
 
     systemd.paths = mkOption {
       default = {};
-      type = with types; attrsOf (submodule [ { options = pathOptions; } unitConfig ]);
+      type = systemdUtils.types.paths;
       description = "Definition of systemd path units.";
     };
 
     systemd.mounts = mkOption {
       default = [];
-      type = with types; listOf (submodule [ { options = mountOptions; } unitConfig mountConfig ]);
+      type = systemdUtils.types.mounts;
       description = ''
         Definition of systemd mount units.
         This is a list instead of an attrSet, because systemd mandates the names to be derived from
@@ -243,7 +233,7 @@ in
 
     systemd.automounts = mkOption {
       default = [];
-      type = with types; listOf (submodule [ { options = automountOptions; } unitConfig automountConfig ]);
+      type = systemdUtils.types.automounts;
       description = ''
         Definition of systemd automount units.
         This is a list instead of an attrSet, because systemd mandates the names to be derived from
@@ -253,7 +243,7 @@ in
 
     systemd.slices = mkOption {
       default = {};
-      type = with types; attrsOf (submodule [ { options = sliceOptions; } unitConfig] );
+      type = systemdUtils.types.slices;
       description = "Definition of slice configurations.";
     };
 
diff --git a/nixos/modules/system/boot/systemd/user.nix b/nixos/modules/system/boot/systemd/user.nix
index 6f63292bf9f..c04933263ac 100644
--- a/nixos/modules/system/boot/systemd/user.nix
+++ b/nixos/modules/system/boot/systemd/user.nix
@@ -12,10 +12,6 @@ let
     (systemdUtils.lib)
     makeUnit
     generateUnits
-    makeJobScript
-    unitConfig
-    serviceConfig
-    commonUnitText
     targetToUnit
     serviceToUnit
     socketToUnit
@@ -57,48 +53,42 @@ in {
     systemd.user.units = mkOption {
       description = "Definition of systemd per-user units.";
       default = {};
-      type = with types; attrsOf (submodule (
-        { name, config, ... }:
-        { options = concreteUnitOptions;
-          config = {
-            unit = mkDefault (makeUnit name config);
-          };
-        }));
+      type = systemdUtils.types.units;
     };
 
     systemd.user.paths = mkOption {
       default = {};
-      type = with types; attrsOf (submodule [ { options = pathOptions; } unitConfig ]);
+      type = systemdUtils.types.paths;
       description = "Definition of systemd per-user path units.";
     };
 
     systemd.user.services = mkOption {
       default = {};
-      type = with types; attrsOf (submodule [ { options = serviceOptions; } unitConfig serviceConfig ] );
+      type = systemdUtils.types.services;
       description = "Definition of systemd per-user service units.";
     };
 
     systemd.user.slices = mkOption {
       default = {};
-      type = with types; attrsOf (submodule [ { options = sliceOptions; } unitConfig ] );
+      type = systemdUtils.types.slices;
       description = "Definition of systemd per-user slice units.";
     };
 
     systemd.user.sockets = mkOption {
       default = {};
-      type = with types; attrsOf (submodule [ { options = socketOptions; } unitConfig ] );
+      type = systemdUtils.types.sockets;
       description = "Definition of systemd per-user socket units.";
     };
 
     systemd.user.targets = mkOption {
       default = {};
-      type = with types; attrsOf (submodule [ { options = targetOptions; } unitConfig] );
+      type = systemdUtils.types.targets;
       description = "Definition of systemd per-user target units.";
     };
 
     systemd.user.timers = mkOption {
       default = {};
-      type = with types; attrsOf (submodule [ { options = timerOptions; } unitConfig ] );
+      type = systemdUtils.types.timers;
       description = "Definition of systemd per-user timer units.";
     };
 
10:34:04
@elvishjerricco:matrix.org@elvishjerricco:matrix.orgLets me reuse the types and use "only" 70 lines to copy stuff for the initrd module10:37:03
@bobvanderlinden_:matrix.orgbobvanderlindenah, exactly. that looks similar to what I would've done if the module-reusing is a bit too much 😅10:37:39
@elvishjerricco:matrix.org@elvishjerricco:matrix.orgI do think fully modularizing it would be nice, but definitely not necessary. I think if it hurts eval time at all it's not worth it10:37:41
@bobvanderlinden_:matrix.orgbobvanderlinden btw, initrdServiceConfig? is that different from serviceConfig? 10:37:53
@elvishjerricco:matrix.org@elvishjerricco:matrix.orgBut if not, and the code isn't crazy, then cool10:38:02
@elvishjerricco:matrix.org@elvishjerricco:matrix.org bobvanderlinden: Yes. I couldn't think of a really clean way to have the default path option differ 10:38:20
@elvishjerricco:matrix.org@elvishjerricco:matrix.orgBecause we don't want anything in that by default for initrd10:38:29
@elvishjerricco:matrix.org@elvishjerricco:matrix.org If anyone actually uses path, we'll have no sane choice but to add the whole bin directory for those packages to the initrd because that's the only thing it could mean 10:39:21
@elvishjerricco:matrix.org@elvishjerricco:matrix.orgSo anything in there by default is necessarily excessive10:39:44
@elvishjerricco:matrix.org@elvishjerricco:matrix.org Though I definitely wasn't supposed to put the initrdServices line in that config lol. Supposed to be in the commit that actually adds that stuff 10:41:33
@elvishjerricco:matrix.org@elvishjerricco:matrix.org * Though I definitely wasn't supposed to put the initrdServices line in that commit lol. Supposed to be in the commit that actually adds that stuff 10:41:50
@bobvanderlinden_:matrix.orgbobvanderlinden

ah, I think you refer to:

https://github.com/nixos/nixpkgs/blob/9bc093b30a3ab38f9e5b30a132b08a9fd9b27ade/nixos/lib/systemd-lib.nix#L299

That should refer to a new option config.systemd.path, which for boot/systemd.nix and boot/systemd/user.nix should refer to config.path.

10:42:36
@bobvanderlinden_:matrix.orgbobvanderlindenbut I guess that'll force serviceConfig to be a function again with its added complexity 😅10:43:15
@elvishjerricco:matrix.org@elvishjerricco:matrix.org Yea, so I just... made a mkServiceConfig function that takes an argument for the default path and created serviceConfig and initrdServiceConfig to keep it simple 10:44:05
@elvishjerricco:matrix.org@elvishjerricco:matrix.orgNot exactly clean10:44:09
@elvishjerricco:matrix.org@elvishjerricco:matrix.orgbut it'll do the trick10:44:12
@elvishjerricco:matrix.org@elvishjerricco:matrix.orgAnd it's at least very simple10:44:22
@bobvanderlinden_:matrix.orgbobvanderlinden

thinking about this more. IT could also do in systemd.nix's config section:

systemd.services.*.environment.PATH = mkDefault "${makeBinPath config.path}:${makeSearchPathOutput "bin" "sbin" config.path}";

with some added fiddlyness to handle *

10:46:08
@elvishjerricco:matrix.org@elvishjerricco:matrix.org Yea the fiddlyness for * is the tricky part. I don't know if there's a correct way to do that 10:47:18
@elvishjerricco:matrix.org@elvishjerricco:matrix.orgThere used to be a hack that worked if you defined the option a second time, but I'm pretty sure that stopped working some time ago10:47:38
@elvishjerricco:matrix.org@elvishjerricco:matrix.org and I'm pretty sure it was never intended to work 10:47:48
@janne.hess:helsinki-systems.de@janne.hess:helsinki-systems.de
In reply to @elvishjerricco:matrix.org
Yea the fiddlyness for * is the tricky part. I don't know if there's a correct way to do that
You can iterate over the attrNames and generate a mkMerge
10:50:56
@janne.hess:helsinki-systems.de@janne.hess:helsinki-systems.de* You can iterate over the attrNames and map them to a mkMerge10:51:30
@elvishjerricco:matrix.org@elvishjerricco:matrix.org Janne Heß: No you can't. You can do that to define a different attrsOf in terms of a different attrsOf. But you can't do foo = mapAttrs (...) config.foo because then foo is infinite 10:52:39
@janne.hess:helsinki-systems.de@janne.hess:helsinki-systems.de Oh I see your point now. Pretty sure there was a way somehow that we are using downstream. Maybe Andreas Schrägle remembers? 10:53:48
@elvishjerricco:matrix.org@elvishjerricco:matrix.org

I've seen this before:

# foo.nix
{
  options.foo = mkOption { type = attrsOf (submodule <module>); };
}

#bar.nix
{
  options.foo = mkOption { type attrsOf (submodule <more module>); };
}

But I'm pretty sure this wasn't intended to work, and doesn't work anymore (last I checked, like a year ago)

10:55:25
@elvishjerricco:matrix.org@elvishjerricco:matrix.org *

I've seen this before:

# foo.nix
{
  options.foo = mkOption { type = attrsOf (submodule <module>); };
}

#bar.nix
{
  options.foo = mkOption { type = attrsOf (submodule <more module>); };
}

But I'm pretty sure this wasn't intended to work, and doesn't work anymore (last I checked, like a year ago)

10:55:44
@janne.hess:helsinki-systems.de@janne.hess:helsinki-systems.deThat should work fine. There's typesMergable for that so that should do it10:56:20
@elvishjerricco:matrix.org@elvishjerricco:matrix.orgtypesMergable?10:56:33

Show newer messages


Back to Room ListRoom Version: 6