-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
250 lines (221 loc) · 8.06 KB
/
flake.nix
File metadata and controls
250 lines (221 loc) · 8.06 KB
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
{
description = "system configurations of bryan";
inputs = {
/**
_private_ machine attributes
_not_ secret tho, might leak through /nix/store & cache!
machines = home-attrs.outputs = {
id = {
system = ... ; ## required, for `nixpkgs.system`
username = ... ; ## required, for `home.username`
homeDirectory = ... ; ## optional, defaults to "/home/${username}"
hostname = ... ; ## optional, defaults to `id`
};
}
*/
home-attrs.url = "git+ssh://git@github.com/bryango/attrs.git";
/* cachix update:
nix eval --raw cheznix#cheznix.inputs.home-attrs.outPath \
| cachix push chezbryan
*/
## p13n nixpkgs with config
nixpkgs-config.url = "path:./nixpkgs-config";
nixpkgs.follows = "nixpkgs-config/nixpkgs";
system-manager = {
url = "github:numtide/system-manager";
inputs.nixpkgs.follows = "nixpkgs";
};
nix-darwin = {
url = "github:nix-darwin/nix-darwin/master";
# /** https://github.com/nix-darwin/nix-darwin/pull/1635 */
# url = "github:nix-darwin/nix-darwin?ref=pull/1635/merge";
inputs.nixpkgs.follows = "nixpkgs";
};
/** provide store path for some homemade darwin .apps */
darwin-apps = {
url = "git+https://gist.github.com/0057346dbf85981e58518be49d36fc06.git";
flake = false;
};
};
outputs = { self, home-attrs, system-manager, nix-darwin, ... }:
let
## consistent namings
nixpkgs-follows =
let
result = "nixpkgs-config";
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in
assert lock.nodes.${result}.original.path == "./${result}";
result;
/* ^ refers to both the input _name_ & its _source_,
.. therefore these two must coincide! */
cheznix = self;
nixpkgs = self.inputs.${nixpkgs-follows};
inherit (nixpkgs) lib;
# inherit (lib) yants;
## upstream overrides: inputs.${nixpkgs-follows}
## home overlay:
overlay = final: prev: import ./overlay.nix final prev // (with final; {
inherit cheznix;
system-manager = system-manager.packages.${system}.default;
});
machines = lib.mapAttrs updateHomeAttrs home-attrs.outputs;
isLinux = lib.hasSuffix "linux";
isDarwin = lib.hasSuffix "darwin";
linuxMachines = lib.filterAttrs (_: { system, ... }: isLinux system) machines;
darwinMachines = lib.filterAttrs (_: { system, ... }: isDarwin system) machines;
mkConfigWithAliases = id: { system, ... }@_attrs: { name, value }: {
${name} = value;
${system}.${id} = value; # .${system} alias
} // {
${id} = value; # unique id
};
/** lib.mergeAttrsList, but with deep merges */
mergeAttrsListDeep = lib.foldl lib.recursiveUpdate { };
genMergedAttrs = x: f: mergeAttrsListDeep (lib.mapAttrsToList f x);
forMyMachines = genMergedAttrs machines;
forMyLinux = genMergedAttrs linuxMachines;
forMyDarwin = genMergedAttrs darwinMachines;
inherit (lib)
mySystems
forMySystems;
mkSystemPkgs = system: nixpkgs.legacyPackages.${system}.extend overlay;
updateHomeAttrs = id: attrs:
attrs // {
hostname = attrs.hostname or id;
pkgs =
assert lib.elem attrs.system mySystems;
mkSystemPkgs attrs.system;
};
mkSpecialAttrs = attrs: {
inherit attrs cheznix nixpkgs-follows;
};
mkHomeConfig = id: { system, username, hostname, pkgs, ... }@attrs:
mkConfigWithAliases id attrs {
name = "${username}@${hostname}";
value = pkgs.home-manager.flake.lib.homeManagerConfiguration {
inherit pkgs;
## specify your home configuration modules
modules = [
./home.nix
{
# must set for `nix.settings` and stuff
nix.package = pkgs.nixPackage; # defined in `nixpkgs-config`
}
];
## pass through arguments to home.nix
extraSpecialArgs = mkSpecialAttrs attrs;
};
};
mkSystemConfig = id: { pkgs, ... }@attrs:
mkConfigWithAliases id attrs {
name = "${attrs.hostname}";
value = system-manager.lib.makeSystemConfig {
modules = [
./system-modules
# {
# # not yet implemented upstream
# nix.package = pkgs.nixPackage; # defined in `nixpkgs-config`
# }
];
extraSpecialArgs = mkSpecialAttrs attrs // {
inherit pkgs;
## ^ add overlaid nixpkgs
## ^ override github:numtide/system-manager/main/nix/lib.nix
};
};
};
mkDarwinConfig = id: { hostname, pkgs, ... }@attrs:
mkConfigWithAliases id attrs {
name = "${hostname}";
value = nix-darwin.lib.darwinSystem {
modules = [
./darwin
{
nixpkgs = {
inherit pkgs;
};
nix.package = pkgs.nixPackage; # defined in `nixpkgs-config`
}
];
specialArgs = mkSpecialAttrs attrs;
};
};
in
{
inherit lib;
overlays.default = overlay;
homeConfigurations = forMyMachines mkHomeConfig;
systemConfigs = forMyLinux mkSystemConfig;
darwinConfigurations = forMyDarwin mkDarwinConfig;
legacyPackages = forMySystems mkSystemPkgs;
packages = forMySystems (system:
let
pkgs = self.legacyPackages.${system};
nixDarwinPackages = nix-darwin.packages.${system};
darwinConfigs = self.darwinConfigurations.${system} or { };
homeConfigs = self.homeConfigurations.${system} or { };
mkConfigNames = configs: lib.pipe configs [
lib.attrNames
lib.escapeShellArgs
];
darwinConfigNames = mkConfigNames darwinConfigs;
homeConfigNames = mkConfigNames homeConfigs;
packages =
lib.optionalAttrs (nixDarwinPackages ? darwin-rebuild)
{
darwin-rebuild = nixDarwinPackages.darwin-rebuild // {
flake = nix-darwin;
packages = nixDarwinPackages;
};
}
// rec {
home-manager = pkgs.home-manager // {
packages = home-manager.flake.packages.${system};
};
};
in packages // {
config-manager = pkgs.writeShellApplication rec {
name = "config-manager";
runtimeInputs = lib.attrValues packages;
excludeShellChecks = [
"SC2043" # allow for loops to run only once
];
text = ''
>&2 echo ${name}: applying [ "$@" ] to all supported configurations:
>&2 echo
>&2 echo " - darwinConfigurations: [ ${darwinConfigNames} ] "
>&2 echo " - homeConfigurations: [ ${homeConfigNames} ] "
>&2 echo
>&2 echo ${name}: starting in 3 seconds ...
>&2 echo
sleep 3
set -x
for oneConfig in ${darwinConfigNames}; do
darwin-rebuild --flake ".#$oneConfig" "$@"
done
for oneConfig in ${homeConfigNames}; do
home-manager --flake .#$oneConfig "$@"
done
set +x
'';
};
default =
let
mapConfigs = configs: prefix: (lib.mapAttrs' (name: value: {
name = "${prefix}-${name}";
value = value.activationPackage /* hm */ or value.system /* darwin */;
}) configs);
homePackages = mapConfigs homeConfigs "home";
darwinPackages = mapConfigs darwinConfigs "darwin";
allPackages = darwinPackages // homePackages;
in pkgs.linkFarm "activation-packages" allPackages;
});
apps = forMySystems (system: {
default = {
type = "app";
program = lib.getExe self.packages.${system}.config-manager;
};
});
};
}