-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhosts.nix
71 lines (65 loc) · 2.11 KB
/
hosts.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{ self, config, inputs, lib, ... }:
{
flake = {
nixosConfigurations =
let
inherit (inputs.nixpkgs.lib) nixosSystem;
# Get all subdirectories in the hosts directory
hostNames = with builtins;
attrNames (lib.attrsets.filterAttrs (n: v: v == "directory")
(readDir ./.));
specialArgs = {
inherit inputs self;
};
mkHost =
hostname:
nixosSystem {
inherit specialArgs;
modules = [
inputs.disko.nixosModules.disko
inputs.sops-nix.nixosModules.sops
./${hostname}
];
system = "x86_64-linux";
};
in
builtins.listToAttrs (
map (name: {
inherit name;
value = mkHost name;
}) hostNames
);
deploy.nodes =
let
# Get all subdirectories in the hosts directory
hostNames = with builtins;
attrNames (lib.attrsets.filterAttrs (n: v: v == "directory")
(readDir ./.));
# Function to get IP from host's network interface config
getHostIP = hostname:
let
networkConfig = (import ./${hostname}/default.nix { inherit self config lib; }).networking;
# Get the first interface that has IPv4 addresses configured
interface = lib.head (lib.attrNames
(lib.filterAttrs
(name: value: value.ipv4.addresses != [])
networkConfig.interfaces
));
in
# Get the address from the first IPv4 configuration
(lib.head networkConfig.interfaces.${interface}.ipv4.addresses).address;
mkDeployNode = hostname: {
name = hostname;
value = {
hostname = getHostIP hostname;
profiles.system = {
user = "root";
sshUser = "nix";
path = inputs.deploy-rs.lib.x86_64-linux.activate.nixos self.nixosConfigurations.${hostname};
};
};
};
in
builtins.listToAttrs (map mkDeployNode hostNames);
};
}