blob: 1218ef31cc367d2c44709de3dab0c68063aca928 (
plain)
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
|
{
description = "GKleen's flakey nixos configuration";
inputs = {
nixpkgs = {
type = "github";
owner = "NixOS";
repo = "nixpkgs";
ref = "master";
};
home-manager = {
type = "github";
owner = "nix-community";
repo = "home-manager";
ref = "master";
inputs.nixpkgs.follows = "nixpkgs";
};
sops-nix = {
type = "github";
owner = "Mic92";
repo = "sops-nix";
ref = "master";
inputs.nixpkgs.follows = "nixpkgs";
};
deploy-rs = {
type = "github";
owner = "serokell";
repo = "deploy-rs";
ref = "master";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-compat.follows = "flake-compat";
};
};
nvfetcher = {
type = "github";
owner = "berberman";
repo = "nvfetcher";
ref = "master";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-compat.follows = "flake-compat";
};
};
flake-compat = {
type = "github";
owner = "edolstra";
repo = "flake-compat";
ref = "master";
flake = false;
};
};
outputs = { self, nixpkgs, home-manager, sops-nix, deploy-rs, nvfetcher, ... }@inputs:
let
inherit (builtins) attrNames attrValues elemAt toJSON isNull pathExists;
inherit (nixpkgs) lib;
utils = import ./utils { inherit lib; };
inherit (utils) nixImport overrideModule;
inherit (lib) nixosSystem mkIf splitString filterAttrs listToAttrs mapAttrsToList nameValuePair concatMap composeManyExtensions mapAttrs mapAttrs' recursiveUpdate genAttrs unique elem optionalAttrs isDerivation concatLists concatStringsSep fix filter makeOverridable foldr;
inherit (lib.strings) escapeNixString;
accountUserName = accountName:
let
accountName' = splitString "@" accountName;
in elemAt accountName' 0;
accountHostName = accountName:
let
accountName' = splitString "@" accountName;
in elemAt accountName' 1;
mkSources = pkgs: optionalAttrs (pathExists _sources/generated.nix) { sources = pkgs.callPackage _sources/generated.nix {}; };
mkOverlay = path: final: prev: import path ({ inherit final; inherit prev; } // mkSources prev);
mkNixosConfiguration = addProfiles: dir: path: hostName: nixosSystem rec {
specialArgs = {
flake = self;
flakeInputs = inputs;
path = ./.;
};
modules =
let
defaultProfiles = with self.nixosModules.systemProfiles;
[ core
];
local = dir + "/${path}";
argsModule = { pkgs, ... }: {
_module.args = {
customUtils = utils;
inherit hostName;
} // mkSources pkgs;
};
accountModules = attrValues (filterAttrs accountMatchesHost self.nixosModules.accounts);
accountMatchesHost = n: _v: accountHostName n == hostName;
in attrValues (filterAttrs (n: _v: !(elem n ["systemProfiles" "users" "userProfiles" "accounts"])) self.nixosModules) ++ [ argsModule ] ++ defaultProfiles ++ addProfiles ++ [ local ] ++ accountModules;
};
mkSystemProfile = dir: path: profileName: {
imports = [ (dir + "/${path}") ];
config = {
system.profiles = [profileName];
};
};
defaultUserProfiles = userName: with self.nixosModules.userProfiles.${userName};
[ core
];
mkUserModule = dir: path: userName:
overrideModule (import (dir + "/${path}"))
(inputs: inputs // { inherit userName; })
(outputs: { _file = dir + "/${path}"; }
// outputs
// { imports = defaultUserProfiles userName ++ (outputs.imports or []); });
mkUserProfile = userName: dir: path: profileName:
let
profileModule = overrideModule (import (dir + "/${path}"))
(inputs: inputs // { inherit userName; })
(outputs: { _file = dir + "/${path}"; }
// outputs);
in {
imports = [profileModule];
config = {
users.users.${userName}.profiles = [profileName];
};
};
mkAccountModule = dir: path: accountName:
let
userName = accountUserName accountName;
in overrideModule
(import (dir + "/${path}"))
(inputs: inputs // { inherit userName; })
(outputs: { _file = dir + "/${path}"; }
// outputs
// { imports = [self.nixosModules.users.${userName} or ({...}: { imports = defaultUserProfiles userName; })] ++ (outputs.imports or []); });
systemsSelector = "x86_64-linux";
systems = filter (system: !(isNull (builtins.match systemsSelector system))) nixpkgs.lib.systems.supported.hydra;
nixpkgsPackages = localSystem: (makeOverridable (import (nixpkgs.outPath + "/pkgs/top-level"))) { inherit localSystem; };
forAllSystems = f: mapAttrs f (genAttrs systems nixpkgsPackages);
forAllUsers = genAttrs (unique (map accountUserName (attrNames self.nixosModules.accounts)));
activateNixosConfigurations = forAllSystems (system: _pkgs: mapAttrs' (hostName: nixosConfig: nameValuePair "${hostName}-activate" { type = "app"; program = "${nixosConfig.config.system.build.toplevel}/bin/switch-to-configuration"; }) self.nixosConfigurations);
activateHomeManagerConfigurations = forAllSystems (system: _pkgs: listToAttrs (concatLists (mapAttrsToList (hostName: nixosConfig: mapAttrsToList (userName: userCfg: nameValuePair "${userName}@${hostName}-activate" { type = "app"; program = "${userCfg.home.activationPackage}/activate"; }) nixosConfig.config.home-manager.users) self.nixosConfigurations)));
overlayPaths = nixImport rec { dir = ./overlays; _import = (path: _name: dir + "/${path}"); };
installerProfiles = system:
let nixpkgs-path = nixpkgs.outPath;
in mapAttrs (name: {path, output}: { profile = mkSystemProfile nixpkgs-path path "installer-${name}"; inherit output; })
{ cd-dvd = { path = "nixos/modules/installer/cd-dvd/installation-cd-minimal.nix"; output = out: out.config.system.build.isoImage; };
netboot = { path = "nixos/modules/installer/netboot/netboot-minimal.nix"; output = out: (self.legacyPackages.${system}.symlinkJoin { name = "netboot"; paths = with out.config.system.build; [ netbootRamdisk kernel netbootIpxeScript ]; preferLocalBuild = true; }); };
};
installerConfig = if pathExists ./installer.nix then "installer.nix" else (if pathExists ./installer then "installer" else null);
installers =
let mkInstallers = system: mapAttrs (mkInstaller system) (installerProfiles system);
mkInstaller = system: name: {profile, output}: let mkOutput = output; in rec { config = mkNixosConfiguration [profile { config = { nixpkgs.system = system; }; }] ./. installerConfig "installer"; output = mkOutput config; };
in forAllSystems (system: _systemPkgs: optionalAttrs (!(isNull installerConfig)) (mkInstallers system));
installerNixosConfigurations = listToAttrs (concatLists (mapAttrsToList (system: mapAttrsToList (profile: { config, ... }: nameValuePair ("installer-${system}-${profile}") config)) installers));
# packages = forAllSystems (system: systemPkgs: composeManyExtensions (attrValues self.overlays) self.legacyPackages.${system} systemPkgs);
activateHomeManager = system: base: (deploy-rs.lib.${system}.activate.custom // { dryActivate = "DRY_RUN=1 $PROFILE/activate"; }) base.activationPackage "$PROFILE/activate";
in
{
nixosModules =
let modulesAttrs = nixImport { dir = ./modules; };
systemProfiles = nixImport rec { dir = ./system-profiles; _import = mkSystemProfile dir; };
users = nixImport rec { dir = ./users; _import = mkUserModule dir; };
userProfiles = forAllUsers (userName: nixImport rec { dir = ./user-profiles; _import = mkUserProfile userName dir; });
accounts = recursiveUpdate rootAccounts (nixImport rec { dir = ./accounts; _import = mkAccountModule dir; });
rootAccounts = mapAttrs' (hostName: _value: nameValuePair "root@${hostName}" ({...}: { imports = [ self.nixosModules.users.root or ({...}: { imports = defaultUserProfiles "root"; }) ]; })) self.nixosConfigurations;
in modulesAttrs // { inherit systemProfiles users userProfiles accounts; };
nixosConfigurations = installerNixosConfigurations // nixImport rec { dir = ./hosts; _import = mkNixosConfiguration [] dir; };
homeModules = nixImport rec { dir = ./home-modules; };
homeConfigurations = listToAttrs (concatLists (mapAttrsToList (hostname: nixosConfig: mapAttrsToList (username: configuration: nameValuePair "${username}@${hostname}" { inherit (configuration.home) activationPackage; }) nixosConfig.config.home-manager.users) self.nixosConfigurations));
overlays = mapAttrs (_name: path: mkOverlay path) overlayPaths;
# packages = mapAttrs (_name: filterAttrs (_name: isDerivation)) packages;
# packages' = mapAttrs (_name: filterAttrs (_name: value: !(isDerivation value))) packages;
legacyPackages = forAllSystems (system: systemPkgs: systemPkgs.override { overlays = attrValues self.overlays; });
apps = foldr recursiveUpdate {} [activateNixosConfigurations activateHomeManagerConfigurations];
devShells = forAllSystems (system: systemPkgs: { default = import ./shell.nix {
pkgs = self.legacyPackages.${system};
deploy-rs = deploy-rs.packages.${system}.deploy-rs;
nvfetcher = nvfetcher.defaultPackage.${system};
};});
templates.default = {
path = ./.;
description = "GKleen's flakey nixos configuration";
};
deploy.nodes = let
defaults = mapAttrs (hostname: _: {
inherit hostname;
sshUser = "root";
profilesOrder = ["system"]; # system first
profiles = {
system = {
path = deploy-rs.lib.${self.nixosConfigurations.${hostname}.config.nixpkgs.system}.activate.nixos self.nixosConfigurations.${hostname};
};
}; # // (mapAttrs (_user: usercfg: {
# user = usercfg.home.username;
# path = activateHomeManager (self.nixosConfigurations.${hostname}.config.nixpkgs.system) usercfg.home;
# }) self.nixosConfigurations.${hostname}.config.home-manager.users);
}) (nixImport { dir = ./hosts; _import = (_path: name: name); });
overrides = if pathExists ./deploy then nixImport { dir = ./deploy; _import = path: _name: import (./deploy + "/${path}") inputs; } else {};
filterEnabled = attrs: mapAttrs (_n: v: filterAttrs (n: _v: n != "enabled") v) (filterAttrs (_n: v: v.enabled or true) attrs);
in mapAttrs (_n: v: if v ? "profiles" then v // { profiles = filterEnabled v.profiles; } else v) (filterEnabled (recursiveUpdate defaults overrides));
checks = builtins.mapAttrs (system: deployLib: deployLib.deployChecks self.deploy) deploy-rs.lib;
};
}
|