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
|
{
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05";
};
outputs = {
self,
nixpkgs,
flake-utils,
}: let
mapOverlayOverride = prefix: overlay: final: prev: let
overlayPkgs = overlay final prev;
in
{
"${prefix}" = (prev.${prefix} or {}) // builtins.removeAttrs overlayPkgs ["override"];
}
// (overlayPkgs.override or {});
doctrineNoPkgs = self.lib.mkDoctrine {
lib = nixpkgs.lib;
pkgs = null;
};
in
flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {inherit system;};
in {
formatter = pkgs.alejandra;
packages =
(import nixpkgs {
inherit system;
overlays = [self.overlays.default];
}).${
doctrineNoPkgs.prefix
};
})
// {
templates = let
system-flake = {
path = ./templates/system-flake;
description = "Opinionated flake for a NixOS system with Home Manager";
};
in {
inherit system-flake;
default = system-flake;
};
overlays = let
overlay = mapOverlayOverride doctrineNoPkgs.prefix (import ./pkgs);
in {
default = overlay;
${doctrineNoPkgs.prefix} = overlay;
};
homeManagerModules.default = ./modules;
nixosModules.default = ./modules;
lib = {
mkDoctrine = import ./doctrine;
mkSystemFlake = {
flakes,
system,
doctrinePrefix ? null,
formatter ? "alejandra",
paths ? {},
}: let
mkDoctrine = args:
self.lib.mkDoctrine
(args
// optionalAttrs (doctrinePrefix != null) {
prefix = doctrinePrefix;
});
doctrineNoPkgs = mkDoctrine {
lib = nixpkgs.lib;
pkgs = null;
};
optionalFlake = name:
if flakes ? "${name}"
then flakes.${name}
else null;
requireFlake = name:
if flakes ? "${name}"
then flakes.${name}
else throw "Required flake input '${name}' is missing";
nur = optionalFlake "nur";
nixpkgs = requireFlake "nixpkgs";
unstable = optionalFlake "unstable";
home-manager =
if hmSourcePath != null
then requireFlake "home-manager"
else null;
pathFromSelf = path: builtins.toPath "${flakes.self}" + "/${path}";
localOverlayPath = pathFromSelf paths.localOverlay;
nixpkgsConfigPath = pathFromSelf paths.nixpkgsConfig;
nixosSourcePath = pathFromSelf paths.nixosSource;
nixosPlatformsPath = pathFromSelf paths.nixosPlatforms;
hmSourcePath = pathFromSelf paths.hmSource;
hmPlatformsPath = pathFromSelf paths.hmPlatforms;
pkgs = importPkgs nixpkgs;
importPkgs = flake:
import flake ({
inherit system;
overlays = let
conditions = [
{
overlay = nur.overlays.default;
condition = nur != null;
}
# NB: Preserve the relative order
{
overlay = mapOverlayOverride prefix (import ./pkgs);
condition = true;
}
{
overlay = flakes.self.overlays.default;
condition = true;
}
];
in
builtins.map (cond: cond.overlay) (builtins.filter (cond: cond.condition) conditions);
}
// optionalAttrs (paths ? nixpkgsConfig) {
config = import nixpkgsConfigPath {inherit (nixpkgs) lib;};
});
inherit (pkgs) lib;
inherit (nixpkgs.lib) optionalAttrs; # Prevents infinite recursion
inherit (doctrineNoPkgs) prefix;
inherit (doctrineNoPkgs.lib) importAll;
in
{
formatter.${system} =
if formatter == "alejandra"
then pkgs.alejandra
else if formatter == "nixpkgs-fmt"
then pkgs.nixpkgs-fmt
else throw "Unknown formatter: '${formatter}'";
packages.${system} = pkgs.${prefix};
overlays.default = final: prev: let
overlay = final: prev:
if paths ? localOverlay
then import localOverlayPath {inherit final prev flakes;}
else {};
in
mapOverlayOverride prefix overlay final prev
// optionalAttrs (unstable != null) {
unstable = importPkgs unstable;
};
}
// optionalAttrs (paths ? nixosSource) {
nixosConfigurations = let
hostConfig = platform:
self.lib.mkSystem {
inherit flakes pkgs;
doctrine = doctrineNoPkgs;
modules = [
nixosSourcePath
platform
];
};
in
lib.mapAttrs (_: hostConfig) (importAll {root = nixosPlatformsPath;});
}
// optionalAttrs (paths ? hmSource) {
homeConfigurations = let
home = name: platform:
home-manager.lib.homeManagerConfiguration {
inherit pkgs;
extraSpecialArgs = {
inherit flakes;
doctrine = mkDoctrine {
inherit pkgs;
namespace = "hm";
};
};
modules = [
self.homeManagerModules.default
hmSourcePath
platform
];
};
in
lib.mapAttrs home (importAll {root = hmPlatformsPath;});
};
mkSystem = {
pkgs,
flakes,
doctrine,
modules,
}:
flakes.nixpkgs.lib.makeOverridable flakes.nixpkgs.lib.nixosSystem {
inherit pkgs;
inherit (pkgs.stdenv.hostPlatform) system;
modules = [self.nixosModules.default] ++ modules;
specialArgs = {
inherit flakes;
doctrine = self.lib.mkDoctrine {
inherit pkgs;
inherit (doctrine) prefix;
namespace = "sys";
};
};
};
};
};
}
|