git.delta.rocks / jrsonnet / refs/commits / a31940c2af82

difftreelog

feat apply networking.hostName from fleet attr name

Yaroslav Bolyukin2024-01-07parent: #0dbf004.patch.diff
in: trunk

2 files changed

modifiedlib/fleetLib.nixdiffbeforeafterboth
before · lib/fleetLib.nix
1# Shared functions for fleet configuration, available as `fleet` module argument2{ nixpkgs, hostNames }: with nixpkgs.lib; rec {3  hostsToAttrs = f: listToAttrs (4    map (name: { inherit name; value = f name; }) hostNames5  );6  hostsCartesian = remove null (7    unique (8      crossLists9        (10          a: b:11            if a == b then12              null13            else14              hostsPair a b15        ) [ hostNames hostNames ]16    )17  );18  hostsPair = this: other:19    let20      sorted = sort (a: b: a < b) [ this other ];21    in22    {23      a = elemAt sorted 0;24      b = elemAt sorted 1;25    };26  hostPairName = this: other:27    if this < other then "${this}-${other}"28    else "${other}-${this}";29}
modifiedmodules/fleet/meta.nixdiffbeforeafterboth
--- a/modules/fleet/meta.nix
+++ b/modules/fleet/meta.nix
@@ -5,9 +5,12 @@
   nixpkgs,
   ...
 }:
-with lib; let
+with lib;
+with fleetLib; let
   hostModule = with types;
-    {...} @ hostConfig: {
+    {...} @ hostConfig: let
+      hostName = hostConfig.config._module.args.name;
+    in {
       options = {
         modules = mkOption {
           type = listOf (mkOptionType {
@@ -32,12 +35,19 @@
           description = "Nixos configuration";
         };
       };
-      config.nixosSystem = nixpkgs.lib.nixosSystem {
-        inherit (hostConfig.config) system modules;
-        specialArgs = {
-          inherit fleetLib;
-          fleet = fleetLib.hostsToAttrs (host: config.hosts.${host}.nixosSystem.config);
+      config = {
+        nixosSystem = nixpkgs.lib.nixosSystem {
+          inherit (hostConfig.config) system modules;
+          specialArgs = {
+            inherit fleetLib;
+            fleet = hostsToAttrs (host: config.hosts.${host}.nixosSystem.config);
+          };
         };
+        modules = [
+          ({...}: {
+            networking.hostName = mkFleetDefault hostName;
+          })
+        ];
       };
     };
   overlayType = mkOptionType {
@@ -69,7 +79,7 @@
     };
   };
   config = {
-    hosts = fleetLib.hostsToAttrs (host: {
+    hosts = hostsToAttrs (host: {
       modules =
         config.globalModules
         ++ [