-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
430 lines (374 loc) · 16.8 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
{
description = "Machines with Nix/NixOS";
inputs = {
nixpkgs-1stable.url = "github:NixOS/nixpkgs/nixos-24.05";
home-manager-1stable.url = "github:nix-community/home-manager/release-24.05";
home-manager-1stable.inputs.nixpkgs.follows = "nixpkgs-1stable";
nixpkgs-1stable-small.url = "github:NixOS/nixpkgs/nixos-24.05-small";
home-manager-1stable-small.url = "github:nix-community/home-manager/release-24.05";
home-manager-1stable-small.inputs.nixpkgs.follows = "nixpkgs-1stable-small";
nixpkgs-0unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
home-manager-0unstable.url = "github:nix-community/home-manager/master";
home-manager-0unstable.inputs.nixpkgs.follows = "nixpkgs-0unstable";
nixpkgs-0unstable-small.url = "github:NixOS/nixpkgs/nixos-unstable-small";
home-manager-0unstable-small.url = "github:nix-community/home-manager/master";
home-manager-0unstable-small.inputs.nixpkgs.follows = "nixpkgs-0unstable-small";
};
outputs = { self,
nixpkgs-1stable, home-manager-1stable,
nixpkgs-1stable-small, home-manager-1stable-small,
nixpkgs-0unstable, home-manager-0unstable,
nixpkgs-0unstable-small, home-manager-0unstable-small,
... }:
let
nixpkgs = nixpkgs-1stable-small;
home-manager = home-manager-1stable-small;
mkPkgs = { system, passed-nixpkgs }: import passed-nixpkgs { inherit system; };
mkForEachSupportedSystem = supportedSystems: f:
nixpkgs.lib.genAttrs supportedSystems (system:
f rec {
pkgs = pkgs1Stable;
pkgs1Stable = mkPkgs { inherit system; passed-nixpkgs = nixpkgs-1stable; };
pkgs1StableSmall = mkPkgs { inherit system; passed-nixpkgs = nixpkgs-1stable-small; };
pkgs0Unstable = mkPkgs { inherit system; passed-nixpkgs = nixpkgs-0unstable; };
pkgs0UnstableSmall = mkPkgs { inherit system; passed-nixpkgs = nixpkgs-0unstable-small; };
});
linuxSystems = {
aarch64 = "aarch64-linux";
riscv64 = "riscv64-linux";
x86_64 = "x86_64-linux";
};
darwinSystems = {
aarch64 = "aarch64-darwin";
x86_64 = "x86_64-darwin";
};
supportedLinuxSystems = nixpkgs.lib.attrValues linuxSystems;
supportedSystems = supportedLinuxSystems ++ (nixpkgs.lib.attrValues darwinSystems);
forEachSupportedLinuxSystem = mkForEachSupportedSystem supportedLinuxSystems;
forEachSupportedSystem = mkForEachSupportedSystem supportedSystems;
miscUsers = {
# generate the `hashedPassword` using `mkpasswd`
root.hashedPassword = "$y$j9T$BzG/Oq8bbL.2uq2/MuRox.$cofHV3CnpcdI4PI5nEEJz//nWqFWiAL8Mfj8/adLJhC";
nixosIso.hashedPassword = "$y$j9T$vMupXoz6rvpT55CkuDenZ0$umu0dpi6NG7lllLcoP9L.wY0ZxEY2wbmPwkiBFsejnC";
nixosIso.username = "nixos";
};
# actual, real system users
realUsers = {
pratham = {
username = "pratham";
fullname = "Pratham Patel";
hashedPassword = "$y$j9T$dyQH1g6q6YjT.8lNruhJT.$xU2x3Phl3L6ey6tIWfmBlgHlCMrTnAxn9yD.a2/yS82";
enableLingering = true;
};
};
systemUsers = miscUsers // realUsers;
nixosMachines = {
misc = {
gatewayAddr = "10.0.0.1";
ipv4PrefixLength = 24;
# actual filesystems that I use
supportedFilesystemsSansZFS = [
"ext4"
"vfat"
"xfs"
];
};
hosts = {
# generate the `hostId` using `head -c4 /dev/urandom | od -A none -t x4 | xargs`
flameboi.hostId = "20c95fe3";
indra.hostId = "d92f6246";
sentinel.hostId = "041d6ae7";
reddish.hostId = "996ccb68";
mahadev.hostId = "c06c1a49";
pawandev.hostId = "2fefd3b2";
stuti.hostId = "07ca9dd4";
chaturvyas.hostId = "6e52044b";
raajan.hostId = "337088b4";
vaaman.hostId = "3c8077f9";
vaayu.hostId = "d81cd923";
# "former" (now dead) "AI" "learning" PC (64GB; R9 3900XT; RTX 3070)
flameboi = {
hostname = "flameboi";
ipv4Address = "10.0.0.13";
networkingIface = "eth0";
system = linuxSystems.x86_64;
};
# Lenovo Yoga Slim 6 (Intel 16GB; i5-13500H; Iris Xe)
indra = {
hostname = "indra";
ipv4Address = "10.0.0.50";
networkingIface = "wlp0s20f3";
system = linuxSystems.x86_64;
};
# Raspberry Pi 4 Model B (4GB)
sentinel = {
hostname = "sentinel";
ipv4Address = "10.0.0.14";
networkingIface = "end0";
system = linuxSystems.aarch64;
};
# Raspberry Pi 4 Model B (8GB)
reddish = {
hostname = "reddish";
ipv4Address = "10.0.0.19";
networkingIface = "end0";
system = linuxSystems.aarch64;
};
# Raspberry Pi 5 Model B (8GB)
raajan = {
hostname = "reddish";
ipv4Address = "10.0.0.59";
networkingIface = "end0";
system = linuxSystems.aarch64;
};
# Radxa ROCK 5 Model B (16GB; RK3588)
mahadev = {
hostname = "mahadev";
ipv4Address = "10.0.0.21";
networkingIface = "enP4p65s0";
system = linuxSystems.aarch64;
};
# Xunlong Orange Pi 5 (4GB; RK3588S)
pawandev = {
hostname = "pawandev";
ipv4Address = "10.0.0.22";
networkingIface = "eth0";
system = linuxSystems.aarch64;
};
# FriendlyElec NanoPC-T6 (16GB; RK3588)
stuti = {
hostname = "stuti";
ipv4Address = "10.0.0.23";
networkingIface = "enP4p65s0"; # second one from the right
system = linuxSystems.aarch64;
};
# FriendlyElec CM3588 NAS (16GB; RK3588)
chaturvyas = {
hostname = "chaturvyas";
ipv4Address = "10.0.0.24";
networkingIface = "enP4p65s0";
forceLtsKernel = true;
system = linuxSystems.aarch64;
};
# StarFive VisionFive 2 (8GB; JH7110)
vaaman = {
hostname = "vaaman";
ipv4Address = "10.0.0.41";
networkingIface = "end0"; # first one from the right
system = linuxSystems.riscv64;
};
# StarFive VisionFive 2 (4GB; JH7110)
vaayu = {
hostname = "vaayu";
ipv4Address = "10.0.0.42";
networkingIface = "end0"; # first one from the right
system = linuxSystems.riscv64;
};
};
};
mkNixosSystem = { hostname, passed-nixpkgs ? nixpkgs, passed-home-manager ? home-manager }:
let
nixpkgs = passed-nixpkgs;
home-manager = passed-home-manager;
system = nixosMachines.hosts."${hostname}".system;
pkgs1Stable = mkPkgs { inherit system; passed-nixpkgs = nixpkgs-1stable; };
pkgs1StableSmall = mkPkgs { inherit system; passed-nixpkgs = nixpkgs-1stable-small; };
pkgs0Unstable = mkPkgs { inherit system; passed-nixpkgs = nixpkgs-0unstable; };
pkgs0UnstableSmall = mkPkgs { inherit system; passed-nixpkgs = nixpkgs-0unstable-small; };
in nixpkgs.lib.nixosSystem {
specialArgs = {
inherit system;
inherit pkgs1Stable pkgs1StableSmall pkgs0Unstable pkgs0UnstableSmall;
inherit (nixosMachines.hosts."${hostname}") hostname ipv4Address networkingIface hostId;
forceLtsKernel = nixosMachines.hosts."${hostname}".forceLtsKernel or false;
systemUser = nixosMachines.hosts."${hostname}".systemUser or systemUsers.pratham;
gatewayAddr = nixosMachines.hosts."${hostname}".gatewayAddr or nixosMachines.misc.gatewayAddr;
ipv4PrefixLength = nixosMachines.hosts."${hostname}".ipv4PrefixLength or nixosMachines.misc.ipv4PrefixLength;
};
modules = [
./nixos-configuration/hosts/${hostname}/default.nix
./nixos-configuration/hosts/hosts-common.nix
(self.nixosModules.customNixosBaseModule { inherit passed-nixpkgs passed-home-manager; })
home-manager.nixosModules.home-manager {
home-manager.extraSpecialArgs = { inherit pkgs1Stable pkgs1StableSmall pkgs0Unstable pkgs0UnstableSmall; };
}
] ++ (nixosMachines.hosts."${hostname}".extraSystemModules or [ ]);
};
mkNonNixosHomeManager = pkgs: systemUser:
let system = pkgs.stdenv.system;
in home-manager.lib.homeManagerConfiguration {
inherit pkgs;
extraSpecialArgs = {
inherit systemUser;
pkgs1Stable = mkPkgs { inherit system; passed-nixpkgs = nixpkgs-1stable; };
pkgs1StableSmall = mkPkgs { inherit system; passed-nixpkgs = nixpkgs-1stable-small; };
pkgs0Unstable = mkPkgs { inherit system; passed-nixpkgs = nixpkgs-0unstable; };
pkgs0UnstableSmall = mkPkgs { inherit system; passed-nixpkgs = nixpkgs-0unstable-small; };
};
modules = [ ./nixos-configuration/home-manager/non-nixos-home.nix ];
};
mkNixosIso = { systemArch }:
nixpkgs-1stable-small.lib.nixosSystem {
system = linuxSystems."${systemArch}";
modules = [ self.nixosModules.customNixosIsoModule ];
};
in {
nixosModules = {
customNixosBaseModule = { passed-nixpkgs ? nixpkgs, passed-home-manager ? home-manager, ... }:
let
nixpkgs = passed-nixpkgs;
home-manager = passed-home-manager;
in {
_module.args = {
inherit home-manager nixpkgs;
inherit (nixosMachines.misc) supportedFilesystemsSansZFS;
};
imports = let nixpkgsChannelPath = "nixpkgs/channels/nixpkgs";
in [
./nixos-configuration/configuration.nix
{
# declare config options that depend on "super sets"
# which are best not passed to the nixos-configuration/* files
environment.etc."${nixpkgsChannelPath}".source = nixpkgs.outPath;
nix.registry.nixpkgs.flake = nixpkgs;
nix.nixPath = [
"nixpkgs=/etc/${nixpkgsChannelPath}"
"nixos-config=/etc/nixos/configuration.nix"
"/nix/var/nix/profiles/per-user/root/channels"
];
users.users.root.hashedPassword = "${systemUsers.root.hashedPassword}";
}
home-manager.nixosModules.home-manager { imports = [ ./nixos-configuration/home-manager/nixos-home.nix ]; }
];
};
customNixosIsoModule = {
_module.args = {
inherit (nixosMachines.misc) supportedFilesystemsSansZFS;
isoUser = miscUsers.nixosIso;
};
imports = [ ./nixos-configuration/_iso/iso.nix ];
};
};
nixosConfigurations = {
flameboi = mkNixosSystem { hostname = "flameboi"; passed-nixpkgs = nixpkgs-1stable; passed-home-manager = home-manager-1stable; };
indra = mkNixosSystem { hostname = "indra"; passed-nixpkgs = nixpkgs-1stable; passed-home-manager = home-manager-1stable; };
sentinel = mkNixosSystem { hostname = "sentinel"; };
reddish = mkNixosSystem { hostname = "reddish"; };
raajan = mkNixosSystem { hostname = "raajan"; };
mahadev = mkNixosSystem { hostname = "mahadev"; };
pawandev = mkNixosSystem { hostname = "pawandev"; };
stuti = mkNixosSystem { hostname = "stuti"; };
chaturvyas = mkNixosSystem { hostname = "chaturvyas"; };
vaaman = mkNixosSystem { hostname = "vaaman"; };
vaayu = mkNixosSystem { hostname = "vaayu"; };
z-iso-aarch64 = mkNixosIso { systemArch = "aarch64"; };
z-iso-riscv64 = mkNixosIso { systemArch = "riscv64"; };
z-iso-x86_64 = mkNixosIso { systemArch = "x86_64"; };
};
legacyPackages = forEachSupportedSystem ({ pkgs, ... }: {
homeConfigurations."${systemUsers.pratham.username}" = mkNonNixosHomeManager pkgs systemUsers.pratham;
});
packages = forEachSupportedSystem ({ pkgs, ... }: {
});
apps = forEachSupportedSystem ({ pkgs, ... }: {
default = self.apps.${pkgs.stdenv.system}.buildEverything;
buildEverything = {
type = "app";
program = "${self.builders.${pkgs.stdenv.system}.default}/bin/run.sh";
};
buildThisNixosSystem = {
type = "app";
program = "${self.builders.${pkgs.stdenv.system}.thisNixosSystem}/bin/run.sh";
};
buildAllNixosSystems = {
type = "app";
program = "${self.builders.${pkgs.stdenv.system}.allNixosSystems}/bin/run.sh";
};
buildThisHome = {
type = "app";
program = "${self.builders.${pkgs.stdenv.system}.thisHome}/bin/run.sh";
};
buildAllHomes = {
type = "app";
program = "${self.builders.${pkgs.stdenv.system}.allHomes}/bin/run.sh";
};
buildThisPackage = {
type = "app";
program = "${self.builders.${pkgs.stdenv.system}.thisPackage}/bin/run.sh";
};
buildAllPackages = {
type = "app";
program = "${self.builders.${pkgs.stdenv.system}.allPackages}/bin/run.sh";
};
buildIso = {
type = "app";
program = "${self.builders.${pkgs.stdenv.system}.theIso}/bin/run.sh";
};
});
builders = forEachSupportedSystem ({ pkgs, ... }: let
lib = pkgs.lib;
system = pkgs.stdenv.system;
nixBuildCmd = "${pkgs.nix-output-monitor}/bin/nom build --verbose --trace-verbose --print-build-logs --show-trace";
buildableSystems = lib.filterAttrs (name: host: host.system == system) nixosMachines.hosts;
allPackages = pkgs.lib.attrNames self.packages.${pkgs.stdenv.system};
concatListToString = passedList: lib.concatStringsSep "," passedList;
encloseInBrackets = passedList: if (lib.lists.length passedList > 1)
then "{" + concatListToString passedList + "}"
else concatListToString passedList;
listOfAllSystems = encloseInBrackets (lib.attrNames buildableSystems);
listOfAllUsers = encloseInBrackets (lib.attrNames realUsers);
listOfAllPackages = encloseInBrackets allPackages;
buildExpressionOfSystem = nixosSystem: if (lib.stringLength nixosSystem == 0) then ""
else ".#nixosConfigurations.${nixosSystem}.config.system.build.toplevel";
buildExpressionOfHome = user: if (lib.stringLength user == 0) then ""
else ".#legacyPackages.${system}.homeConfigurations.${user}.activationPackage";
buildExpressionOfPackage = package: if (lib.stringLength package== 0) then ""
else ".#packages.${system}.${package}";
buildExpressionOfIso = ".#nixosConfigurations.z-iso-$(uname -m).config.system.build.isoImage";
in {
default = pkgs.writeShellScriptBin "run.sh" ''
set -x
# the order matters because they are listed in the priority of build status to me
${nixBuildCmd} ${buildExpressionOfSystem "${listOfAllSystems}"} ${buildExpressionOfHome "${listOfAllUsers}"} ${buildExpressionOfPackage "${listOfAllPackages}"} ${buildExpressionOfIso}
'';
thisNixosSystem = pkgs.writeShellScriptBin "run.sh" ''
set -x
${nixBuildCmd} ${buildExpressionOfSystem ''''${NIXOS_MACHINE_HOSTNAME:-}''}
'';
allNixosSystems = pkgs.writeShellScriptBin "run.sh" ''
set -x
${nixBuildCmd} ${buildExpressionOfSystem "${listOfAllSystems}"}
'';
thisHome = pkgs.writeShellScriptBin "run.sh" ''
set -x
${nixBuildCmd} ${buildExpressionOfHome ''''${USER:-}''}
'';
allHomes = pkgs.writeShellScriptBin "run.sh" ''
set -x
${nixBuildCmd} ${buildExpressionOfHome "${listOfAllUsers}"}
'';
thisPackage = pkgs.writeShellScriptBin "run.sh" ''
set -x
${nixBuildCmd} ${buildExpressionOfPackage ''''${1:-}''}
'';
allPackages = pkgs.writeShellScriptBin "run.sh" ''
set -x
${nixBuildCmd} ${buildExpressionOfPackage "${listOfAllPackages}"}
'';
theIso = pkgs.writeShellScriptBin "run.sh" ''
set -x
${nixBuildCmd} ${buildExpressionOfIso}
'';
});
devShells = forEachSupportedSystem ({ pkgs, ... }: {
default = pkgs.mkShell {
packages = with pkgs; [ nixfmt-classic ];
env = {
UPDATE_LOCKFILE = 0;
USE_NOM_INSTEAD = 1;
DO_DRY_RUN = 1;
};
};
});
};
}