-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
81 lines (65 loc) · 2.53 KB
/
flake.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
72
73
74
75
76
77
78
79
80
81
{
description = "NixOS configuration for NestOps";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
home-manager.url = "github:nix-community/home-manager?ref=release-24.05";
home-manager.inputs.nixpkgs.follows = "nixpkgs";
disko.url = "github:nix-community/disko";
disko.inputs.nixpkgs.follows = "nixpkgs";
sops-nix.url = "github:Mic92/sops-nix";
sops-nix.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, nixpkgs, nixpkgs-unstable, home-manager, disko, sops-nix }:
let
system = "x86_64-linux";
lib = nixpkgs.lib;
# Load the nix file with host definitions
hosts = import ./hosts.nix;
# Validation function to ensure each host has the correct fields
isValidHost = host:
lib.isAttrs host &&
lib.hasAttr "name" host && lib.isString host.name &&
lib.hasAttr "extraModules" host && lib.isList host.extraModules &&
lib.hasAttr "critical" host && lib.isBool host.critical;
# Filter out invalid hosts and keep only valid ones
validHosts = lib.filter isValidHost hosts;
# Define fallback hosts for any missing critical hosts
fallbackHosts = [
{ name = "flex"; extraModules = []; critical = true; }
{ name = "reactor"; extraModules = []; critical = true; }
{ name = "vector"; extraModules = []; critical = true; }
];
# Combine valid hosts with fallback critical hosts (only if missing)
allHosts = lib.concatMap (host:
if lib.any (h: h.name == host.name) validHosts then [] else [host]
) fallbackHosts ++ validHosts;
# Function to find the index of an element by name
findIndex = name: list:
let
names = map (h: h.name) list;
in
builtins.elemAt (builtins.filter (i: builtins.elemAt names i == name) (lib.range 0 (builtins.length names - 1))) 0;
# Function to generate each host configuration
mkHost = host: lib.nixosSystem {
inherit system;
pkgs = import nixpkgs {
inherit system;
config.allowUnfree = true;
};
modules = [
disko.nixosModules.disko
(./hosts/${host.name}/configuration.nix)
home-manager.nixosModules.home-manager
sops-nix.nixosModules.sops
] ++ host.extraModules;
};
in {
# Generate host configurations from `hosts` list
nixosConfigurations = lib.genAttrs
(map (h: h.name) allHosts)
(hostName: mkHost
(builtins.elemAt allHosts (findIndex hostName allHosts))
);
};
}