From a31940c2af8213f9317d7cadf764f87f90f8bdeb Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Sun, 07 Jan 2024 18:41:23 +0000 Subject: [PATCH] feat: apply networking.hostName from fleet attr name --- --- 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; } --- 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 ++ [ -- gitstuff