Skip to content

Commit

Permalink
Refactor nixos/home configs generation
Browse files Browse the repository at this point in the history
Expand facts and filter supported systems before
feeding list to generator functions.
  • Loading branch information
josqu4red committed Jan 2, 2025
1 parent 7e8ab34 commit 6680382
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
8 changes: 5 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
let
lib = import ./lib { inherit inputs; };
inherit (import ./secrets/build/facts.nix {}) facts;
inherit (lib) forAllSystems buildHomeConfigs buildNixosConfigs;
inherit (lib) buildHomeConfigs buildNixosConfigs expandFacts filterSystem forAllSystems;

hosts = expandFacts facts;

local-pkgs = final: _prev: import ./pkgs { pkgs = final; };

Expand All @@ -47,11 +49,11 @@
in {
inherit facts legacyPackages lib;

nixosConfigurations = buildNixosConfigs facts;
nixosConfigurations = buildNixosConfigs (filterSystem "linux" hosts);
nixosModules = import ./modules/nixos;
nixosProfiles = import ./modules/profiles;

homeConfigurations = buildHomeConfigs facts;
homeConfigurations = buildHomeConfigs hosts;
homeModules = import ./modules/home-manager;

devShells = forAllSystems (system: {
Expand Down
36 changes: 18 additions & 18 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
let
inherit (inputs) self home-manager nixpkgs;

inherit (builtins) concatStringsSep hashString map pathExists split substring;
inherit (nixpkgs.lib) isList nameValuePair nixosSystem optional optionalAttrs;
inherit (builtins) concatStringsSep hashString map match pathExists split substring;
inherit (nixpkgs.lib) filterAttrs nameValuePair nixosSystem optional optionalAttrs;
inherit (nixpkgs.lib.misc) mergeAttrsWithFunc;
inherit (nixpkgs.lib.lists) flatten remove unique;
inherit (nixpkgs.lib.lists) flatten isList remove unique;
inherit (nixpkgs.lib.attrsets) concatMapAttrs genAttrs listToAttrs mapAttrs;
inherit (home-manager.lib) homeManagerConfiguration;

Expand All @@ -16,6 +16,8 @@ let

forAllSystems = genAttrs [ "aarch64-darwin" "aarch64-linux" "x86_64-linux" ];

filterSystem = system: (filterAttrs (k: v: (match ".*-${system}" v.system) == []));

machineId = hostname: hashString "md5" hostname;

macAddress = hostname: let
Expand All @@ -26,31 +28,29 @@ let

setDefaults = mergeAttrsWithFunc (a: b: if isList(a) then (unique (a ++ b)) else b);

buildNixosConfigs = facts: mapAttrs (hostname: v: let
hostFacts = setDefaults facts.defaults v;
pkgs = self.legacyPackages.${hostFacts.system};
pkgsCross = self.legacyPackages.${facts.defaults.system}.pkgsCross.aarch64-multiplatform;
expandFacts = facts: mapAttrs (hostname: v: setDefaults facts.defaults v) facts.hosts;

extraArgs = { inherit hostFacts; } // optionalAttrs (hostFacts.system == "aarch64-linux") { inherit pkgsCross; };
buildNixosConfigs = mapAttrs (hostname: hostFacts: let
pkgsCross = optionalAttrs (hostFacts.system == "aarch64-linux")
{ pkgsCross = self.legacyPackages.x86_64-linux.pkgsCross.aarch64-multiplatform; };
in nixosSystem {
inherit pkgs;
specialArgs = { inherit self inputs hostname secrets; } // extraArgs;
pkgs = self.legacyPackages.${hostFacts.system};
specialArgs = { inherit self inputs hostname hostFacts secrets; } // pkgsCross;
modules = [ self.nixosModules.default ../secrets/build/facts.nix ]
++ ifExists ../hosts/${hostname}
++ map (u: ../users/${u}) hostFacts.users;
}
) facts.hosts;
);

buildHomeConfigs = facts: concatMapAttrs (hostname: v: let
hostFacts = setDefaults v facts.defaults;
pkgs = self.legacyPackages.${hostFacts.system};
buildHomeConfigs = concatMapAttrs (hostname: v: let
pkgs = self.legacyPackages.${v.system};
in listToAttrs (map (username:
nameValuePair "${username}@${hostname}" (homeManagerConfiguration {
inherit pkgs;
extraSpecialArgs = { inherit inputs username; inherit (hostFacts) stateVersion; };
extraSpecialArgs = { inherit inputs username; inherit (v) stateVersion; };
modules = [ ../users/${username}/home.nix ]
++ ifExists ../users/${username}/${hostname}.nix;
})
) hostFacts.users)
) facts.hosts;
in { inherit buildHomeConfigs buildNixosConfigs forAllSystems machineId macAddress; }
) v.users)
);
in { inherit buildHomeConfigs buildNixosConfigs expandFacts filterSystem forAllSystems machineId macAddress; }
Binary file modified secrets/build/facts.nix
Binary file not shown.

0 comments on commit 6680382

Please sign in to comment.