-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
flake.nix
182 lines (168 loc) · 5.05 KB
/
flake.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
{
description = "nixos-generators - one config, multiple formats";
# Lib dependency
inputs.nixlib.url = "github:nix-community/nixpkgs.lib";
# Bin dependency
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
outputs = {
self,
nixpkgs,
nixlib,
} @ inputs: let
lib = nixpkgs.lib;
callFlake = flake: let
args =
inputs
// {
nixos-generators = self;
self = subFlake;
};
subFlake = (import flake).outputs args;
in
subFlake;
# Ensures a derivation's name can be accessed without evaluating it deeply.
# Prevents `nix flake show` from being very slow.
makeLazyDrv = name: drv: {
inherit name;
inherit
(drv)
drvPath
outPath
outputName
system
;
type = "derivation";
};
in
# Library modules (depend on nixlib)
{
# export all generator formats in ./formats
nixosModules =
{
all-formats = ./all-formats.nix;
}
// (nixlib.lib.mapAttrs' (file: _: {
name = nixlib.lib.removeSuffix ".nix" file;
# The exported module should include the internal format* options
value.imports = [(./formats + "/${file}") ./format-module.nix];
}) (builtins.readDir ./formats));
# example usage in flakes:
# outputs = { self, nixpkgs, nixos-generators, ...}: {
# vmware = nixos-generators.nixosGenerate {
# system = "x86_64-linux";
# modules = [./configuration.nix];
# format = "vmware";
# };
# }
nixosGenerate = {
pkgs ? null,
lib ? nixpkgs.lib,
nixosSystem ? nixpkgs.lib.nixosSystem,
format,
system ? null,
specialArgs ? {},
modules ? [],
customFormats ? {},
}: let
extraFormats =
lib.mapAttrs' (
name: value:
lib.nameValuePair
name
{
imports = [
value
./format-module.nix
];
}
)
customFormats;
formatModule = builtins.getAttr format (self.nixosModules // extraFormats);
image = nixosSystem {
inherit pkgs specialArgs;
system =
if system != null
then system
else pkgs.system;
lib =
if lib != null
then lib
else pkgs.lib;
modules =
[
formatModule
]
++ modules;
};
in
image.config.system.build.${image.config.formatAttr};
}
//
# Binary and Devshell outputs (depend on nixpkgs)
(
let
forAllSystems = nixpkgs.lib.genAttrs ["x86_64-linux" "x86_64-darwin" "i686-linux" "aarch64-linux" "aarch64-darwin"];
in {
formatter = forAllSystems (system: nixpkgs.legacyPackages.${system}.alejandra);
packages = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages."${system}";
in rec {
default = nixos-generate;
nixos-generate = pkgs.callPackage ./package.nix {};
});
checks =
lib.recursiveUpdate
(callFlake ./checks/test-all-formats-flake/flake.nix).checks
(
lib.genAttrs [
"x86_64-linux"
# We currently don't have kvm support on our builder
#"aarch64-linux"
]
(
system: let
allFormats = import ./checks/test-all-formats.nix {
inherit nixpkgs system;
};
test-customize-format = import ./checks/test-customize-format.nix {
inherit nixpkgs system;
};
in
lib.mapAttrs makeLazyDrv (
{
inherit
(self.packages.${system})
nixos-generate
;
inherit test-customize-format;
is-formatted = import ./checks/is-formatted.nix {
pkgs = nixpkgs.legacyPackages.${system};
};
}
// allFormats
)
)
);
devShells = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages."${system}";
in {
default = pkgs.mkShell {
buildInputs = with pkgs; [jq coreutils findutils];
};
});
# Make it runnable with `nix run`
apps = forAllSystems (system: let
nixos-generate = {
type = "app";
program = "${self.packages."${system}".nixos-generate}/bin/nixos-generate";
};
in {
inherit nixos-generate;
# Nix >= 2.7 flake output schema uses `apps.<system>.default` instead
# of `defaultApp.<system>` to signify the default app (the thing that
# gets run with `nix run . -- <args>`)
default = nixos-generate;
});
}
);
}