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
--- a/lib/fleetLib.nix
+++ b/lib/fleetLib.nix
@@ -1,29 +1,39 @@
 # Shared functions for fleet configuration, available as `fleet` module argument
-{ nixpkgs, hostNames }: with nixpkgs.lib; rec {
-  hostsToAttrs = f: listToAttrs (
-    map (name: { inherit name; value = f name; }) hostNames
-  );
+{
+  nixpkgs,
+  hostNames,
+}:
+with nixpkgs.lib; rec {
+  hostsToAttrs = f:
+    listToAttrs (
+      map (name: {
+        inherit name;
+        value = f name;
+      })
+      hostNames
+    );
   hostsCartesian = remove null (
     unique (
       crossLists
-        (
-          a: b:
-            if a == b then
-              null
-            else
-              hostsPair a b
-        ) [ hostNames hostNames ]
+      (
+        a: b:
+          if a == b
+          then null
+          else hostsPair a b
+      ) [hostNames hostNames]
     )
   );
-  hostsPair = this: other:
-    let
-      sorted = sort (a: b: a < b) [ this other ];
-    in
-    {
-      a = elemAt sorted 0;
-      b = elemAt sorted 1;
-    };
+  hostsPair = this: other: let
+    sorted = sort (a: b: a < b) [this other];
+  in {
+    a = elemAt sorted 0;
+    b = elemAt sorted 1;
+  };
   hostPairName = this: other:
-    if this < other then "${this}-${other}"
+    if this < other
+    then "${this}-${other}"
     else "${other}-${this}";
+
+  # For places, where fleet knows better than nixpkgs defaults
+  mkFleetDefault = mkOverride 999;
 }
modifiedmodules/fleet/meta.nixdiffbeforeafterboth
before · modules/fleet/meta.nix
1{2  lib,3  fleetLib,4  config,5  nixpkgs,6  ...7}:8with lib; let9  hostModule = with types;10    {...} @ hostConfig: {11      options = {12        modules = mkOption {13          type = listOf (mkOptionType {14            name = "submodule";15            inherit (submodule {}) check;16            merge = lib.options.mergeOneOption;17            description = "Nixos modules";18          });19          description = "List of nixos modules";20          default = [];21        };22        system = mkOption {23          type = str;24          description = "Type of system";25        };26        encryptionKey = mkOption {27          type = str;28          description = "Encryption key";29        };30        nixosSystem = mkOption {31          type = unspecified;32          description = "Nixos configuration";33        };34      };35      config.nixosSystem = nixpkgs.lib.nixosSystem {36        inherit (hostConfig.config) system modules;37        specialArgs = {38          inherit fleetLib;39          fleet = fleetLib.hostsToAttrs (host: config.hosts.${host}.nixosSystem.config);40        };41      };42    };43  overlayType = mkOptionType {44    name = "nixpkgs-overlay";45    description = "nixpkgs overlay";46    check = lib.isFunction;47    merge = lib.mergeOneOption;48  };49in {50  options = with types; {51    hosts = mkOption {52      type = attrsOf (submodule hostModule);53      default = {};54      description = "Configurations of individual hosts";55    };56    globalModules = mkOption {57      type = listOf (mkOptionType {58        name = "submodule";59        inherit (submodule {}) check;60        merge = lib.options.mergeOneOption;61        description = "Nixos modules";62      });63      description = "Modules, which should be added to every system";64      default = [];65    };66    overlays = mkOption {67      default = [];68      type = listOf overlayType;69    };70  };71  config = {72    hosts = fleetLib.hostsToAttrs (host: {73      modules =74        config.globalModules75        ++ [76          ({...}: {77            nixpkgs.overlays = config.overlays;78          })79        ];80    });81    globalModules = import ../../nixos/modules/module-list.nix;82  };83}
after · modules/fleet/meta.nix
1{2  lib,3  fleetLib,4  config,5  nixpkgs,6  ...7}:8with lib;9with fleetLib; let10  hostModule = with types;11    {...} @ hostConfig: let12      hostName = hostConfig.config._module.args.name;13    in {14      options = {15        modules = mkOption {16          type = listOf (mkOptionType {17            name = "submodule";18            inherit (submodule {}) check;19            merge = lib.options.mergeOneOption;20            description = "Nixos modules";21          });22          description = "List of nixos modules";23          default = [];24        };25        system = mkOption {26          type = str;27          description = "Type of system";28        };29        encryptionKey = mkOption {30          type = str;31          description = "Encryption key";32        };33        nixosSystem = mkOption {34          type = unspecified;35          description = "Nixos configuration";36        };37      };38      config = {39        nixosSystem = nixpkgs.lib.nixosSystem {40          inherit (hostConfig.config) system modules;41          specialArgs = {42            inherit fleetLib;43            fleet = hostsToAttrs (host: config.hosts.${host}.nixosSystem.config);44          };45        };46        modules = [47          ({...}: {48            networking.hostName = mkFleetDefault hostName;49          })50        ];51      };52    };53  overlayType = mkOptionType {54    name = "nixpkgs-overlay";55    description = "nixpkgs overlay";56    check = lib.isFunction;57    merge = lib.mergeOneOption;58  };59in {60  options = with types; {61    hosts = mkOption {62      type = attrsOf (submodule hostModule);63      default = {};64      description = "Configurations of individual hosts";65    };66    globalModules = mkOption {67      type = listOf (mkOptionType {68        name = "submodule";69        inherit (submodule {}) check;70        merge = lib.options.mergeOneOption;71        description = "Nixos modules";72      });73      description = "Modules, which should be added to every system";74      default = [];75    };76    overlays = mkOption {77      default = [];78      type = listOf overlayType;79    };80  };81  config = {82    hosts = hostsToAttrs (host: {83      modules =84        config.globalModules85        ++ [86          ({...}: {87            nixpkgs.overlays = config.overlays;88          })89        ];90    });91    globalModules = import ../../nixos/modules/module-list.nix;92  };93}