the configuration for all my nixos machines (hacky! bad! ugly!)
1# Shamelessly copied from github.com/orual/flake because they basically nailed
2# it and I can't be assed to do this myself.
3let
4 inherit (builtins) readDir hasAttr attrNames filter concatMap listToAttrs;
5
6 loadHosts = dir: inputs:
7 let
8 loadConf = dir: n: (import "${dir}/${n}" inputs) // { hostname = n; };
9
10 hosts' =
11 let contents = readDir dir;
12 in filter (n: contents."${n}" == "directory") (attrNames contents);
13 in
14 concatMap
15 (n:
16 let
17 contents = readDir "${dir}/${n}";
18 hasDefault = (hasAttr "default.nix" contents)
19 && (contents."default.nix" == "regular");
20 in
21 if hasDefault then [ (loadConf dir n) ] else [ ])
22 hosts';
23in
24{
25 # Discover NixOS configurations.
26 # It will find all sub-directories in `directory` and
27 # include it if it has a default.nix.
28 genNixOSHosts =
29 { inputs
30 , self
31 , directory ? "${inputs.self}/hosts"
32 , nixpkgs ? inputs.nixpkgs
33 , builder ? nixpkgs.lib.nixosSystem
34 , specialArgs ? { }
35 , baseModules ? [ ]
36 , overlays ? [ ]
37 , config ? { allowUnfree = true; }
38 }:
39 let
40 mkHost = { system, modules, hostname, }:
41 builder {
42 inherit system;
43
44 specialArgs = { inherit inputs self; } // specialArgs;
45
46 modules = [
47 ({ ... }: {
48 networking.hostName = hostname;
49
50 nixpkgs = { inherit overlays config; };
51 })
52 ] ++ baseModules ++ modules;
53 };
54 in
55 listToAttrs (map
56 (conf: {
57 name = conf.hostname;
58 value = mkHost (removeAttrs conf [ "home" ]);
59 })
60 (loadHosts directory inputs));
61
62 # Discover home-manager configurations.
63 # It will find all sub-directories in `directory` and
64 # include it if it has a default.nix.
65 genHomeHosts =
66 { inputs
67 , user
68 , directory ? "${inputs.self}/hosts"
69 , nixpkgs ? inputs.nixpkgs
70 , home ? inputs.home
71 , builder ? home.lib.homeManagerConfiguration
72 , specialArgs ? { }
73 , baseModules ? [ ]
74 , overlays ? [ ]
75 , config ? { allowUnfree = true; }
76 ,
77 }:
78 let
79 homeHosts = concatMap
80 (h:
81 if (hasAttr "home" h) then
82 [ ((removeAttrs h [ "home" "modules" ]) // h.home) ]
83 else
84 [ ])
85 (loadHosts directory inputs);
86
87 mkHost = { system, modules, hostname, }:
88 let pkgs = import nixpkgs { inherit system config overlays; };
89 in builder {
90 inherit pkgs;
91 extraSpecialArgs = { inherit inputs; } // specialArgs;
92
93 modules = [
94 ({ lib, ... }: {
95 home.username = lib.mkDefault user;
96 home.homeDirectory = lib.mkDefault "/home/${user}";
97 })
98 ] ++ baseModules ++ modules;
99 };
100 in
101 listToAttrs (map
102 (conf: {
103 name = "${user}@${conf.hostname}";
104 value = mkHost conf;
105 })
106 homeHosts);
107}