-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmkFlake.nix
243 lines (216 loc) · 7.54 KB
/
mkFlake.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
{ flake-utils-plus }:
{ self
, supportedSystems ? flake-utils-plus.lib.defaultSystems
, inputs
, channels ? { }
, channelsConfig ? { }
, sharedOverlays ? [ ]
, hosts ? { }
, hostDefaults ? {
system = "x86_64-linux";
modules = [ ];
extraArgs = { };
}
, outputsBuilder ? _: { }
, ...
}@args:
let
inherit (flake-utils-plus.lib)
eachSystem
mergeAny
patchChannel
;
inherit (flake-utils-plus.lib.internal)
filterAttrs
partitionString
reverseList
;
inherit (builtins)
pathExists
attrNames
attrValues
concatMap
concatStringsSep
elemAt
filter
foldl'
genList
head
isString
length
listToAttrs
mapAttrs
removeAttrs
split
tail
;
srcs = filterAttrs (_: value: !value ? outputs) inputs;
# set defaults and validate host arguments
evalHostArgs =
{ channelName ? "nixpkgs"
, system ? "x86_64-linux"
, output ? "nixosConfigurations"
, builder ? (getChannels system).${channelName}.input.lib.nixosSystem
, modules ? [ ]
, extraArgs ? { }
# These are not part of the module system, so they can be used in `imports` lines without infinite recursion
, specialArgs ? { }
}: {
inherit channelName system output builder extraArgs specialArgs;
modules = modules ++ [ ./options.nix ];
};
optionalAttrs = check: value: if check then value else { };
otherArguments = removeAttrs args [
"inputs"
"hosts"
"hostDefaults"
"nixosProfiles"
"channels"
"channelsConfig"
"self"
"sharedOverlays"
"supportedSystems"
];
getChannels = system: self.pkgs.${system};
ensureChannelsWitsInputs = mapAttrs
(n: v:
if (!v ? input) then
v // {
input = inputs.${n} or (
throw ''
No input is inferable by name from flake inputs for channel "${n}"
'');
}
else v
)
channels;
getNixpkgs = host: (getChannels host.system).${host.channelName};
configurationBuilder = reverseDomainName: host': (
let
dnsLabels = reverseList (partitionString "\\." reverseDomainName);
hostname = head dnsLabels;
domain =
let
domainLabels = tail dnsLabels;
in
if domainLabels == [ ] then (lib.mkDefault null) # null is the networking.domain option's default
else concatStringsSep "." domainLabels;
selectedNixpkgs = getNixpkgs host;
host = evalHostArgs (mergeAny hostDefaults host');
patchedChannel = selectedNixpkgs.path;
channels' = getChannels host.system;
specialArgs = host.specialArgs // { channel = selectedNixpkgs; };
/* nixos specific arguments */
# Use lib from patched nixpkgs
lib = selectedNixpkgs.lib;
# Use nixos modules from patched nixpkgs
baseModules = import (patchedChannel + "/nixos/modules/module-list.nix");
nixosSpecialArgs =
let
f = channelName:
{ "${channelName}ModulesPath" = toString (channels'.${channelName}.input + "/nixos/modules"); };
in
# Add `<channelName>ModulesPath`s
(foldl' (lhs: rhs: lhs // rhs) { } (map f (attrNames channels')))
# Override `modulesPath` because otherwise imports from there will not use patched nixpkgs
// { modulesPath = toString (patchedChannel + "/nixos/modules"); };
# The only way to find out if a host has `nixpkgs.config` set to
# the non-default value is by evalling most of the config.
hostConfig = (lib.evalModules {
prefix = [ ];
check = false;
modules = baseModules ++ host.modules;
args = { inherit inputs; } // host.extraArgs;
specialArgs = nixosSpecialArgs // specialArgs;
}).config;
in
{
${host.output}.${reverseDomainName} = host.builder ({
inherit (host) system;
modules = [
({ pkgs, lib, options, config, ... }: {
# 'mkMerge` to separate out each part into its own module
_type = "merge";
contents = [
(optionalAttrs (options ? networking.hostName) {
networking.hostName = hostname;
})
(optionalAttrs (options ? networking.domain) {
networking.domain = domain;
})
(if options ? nixpkgs.pkgs then
{
nixpkgs.config = selectedNixpkgs.config;
nixpkgs.pkgs =
# Make sure we don't import nixpkgs again if not
# necessary. We can't use `config.nixpkgs.config`
# because that triggers infinite recursion.
if (hostConfig.nixpkgs.config == { }) then
selectedNixpkgs
else
import patchedChannel
{
inherit (host) system;
overlays = selectedNixpkgs.overlays;
config = selectedNixpkgs.config // config.nixpkgs.config;
} // { inherit (selectedNixpkgs) name input; };
}
else { _module.args.pkgs = selectedNixpkgs; })
(optionalAttrs (options ? system.configurationRevision) {
system.configurationRevision = lib.mkIf (self ? rev) self.rev;
})
(optionalAttrs (options ? nix.package) {
nix.package = lib.mkDefault pkgs.nixUnstable;
})
(optionalAttrs (options ? nix.extraOptions) {
nix.extraOptions = "extra-experimental-features = nix-command ca-references flakes";
})
{
# at this point we assume, that an evaluator at least
# uses nixpkgs.lib to evaluate modules.
_module.args = { inherit inputs; } // host.extraArgs;
}
];
})
] ++ host.modules;
inherit specialArgs;
} // (optionalAttrs (host.output == "nixosConfigurations") {
inherit lib baseModules;
specialArgs = nixosSpecialArgs // specialArgs;
}));
}
);
in
mergeAny otherArguments (
eachSystem supportedSystems
(system:
let
importChannel = name: value: (import (patchChannel system value.input (value.patches or [ ])) {
inherit system;
overlays = [
(final: prev: {
__dontExport = true; # in case user uses overlaysFromChannelsExporter, doesn't hurt for others
inherit srcs;
})
] ++ sharedOverlays ++ (if (value ? overlaysBuilder) then (value.overlaysBuilder pkgs) else [ ]) ++ [ flake-utils-plus.overlay ];
config = channelsConfig // (value.config or { });
}) // { inherit name; inherit (value) input; };
pkgs = mapAttrs importChannel ensureChannelsWitsInputs;
systemOutputs = outputsBuilder pkgs;
mkOutputs = attrs: output:
attrs //
mergeAny
# prevent override of nested outputs in otherArguments
(optionalAttrs (otherArguments ? ${output}.${system})
{ ${output} = otherArguments.${output}.${system}; })
(optionalAttrs (systemOutputs ? ${output})
{ ${output} = systemOutputs.${output}; });
in
{ inherit pkgs; }
// (foldl' mkOutputs { } (attrNames systemOutputs))
)
# produces attrset in the shape of
# { nixosConfigurations = {}; darwinConfigurations = {}; ... }
# according to profile.output or the default `nixosConfigurations`
// foldl' mergeAny { } (attrValues (mapAttrs configurationBuilder hosts))
)