1# Experimental flake interface to Nixpkgs.
2# See https://github.com/NixOS/rfcs/pull/49 for details.
3{
4 description = "A collection of packages for the Nix package manager";
5
6 outputs =
7 { self }:
8 let
9 libVersionInfoOverlay = import ./lib/flake-version-info.nix self;
10 lib = (import ./lib).extend libVersionInfoOverlay;
11
12 forAllSystems = lib.genAttrs lib.systems.flakeExposed;
13
14 jobs = forAllSystems (
15 system:
16 import ./pkgs/top-level/release.nix {
17 nixpkgs = self;
18 inherit system;
19 }
20 );
21 in
22 {
23 /**
24 `nixpkgs.lib` is a combination of the [Nixpkgs library](https://nixos.org/manual/nixpkgs/unstable/#id-1.4), and other attributes
25 that are _not_ part of the Nixpkgs library, but part of the Nixpkgs flake:
26
27 - `lib.nixosSystem` for creating a NixOS system configuration
28
29 - `lib.nixos` for other NixOS-provided functionality, such as [`runTest`](https://nixos.org/manual/nixos/unstable/#sec-call-nixos-test-outside-nixos)
30 */
31 # DON'T USE lib.extend TO ADD NEW FUNCTIONALITY.
32 # THIS WAS A MISTAKE. See the warning in lib/default.nix.
33 lib = lib.extend (
34 final: prev: {
35
36 /**
37 Other NixOS-provided functionality, such as [`runTest`](https://nixos.org/manual/nixos/unstable/#sec-call-nixos-test-outside-nixos).
38 See also `lib.nixosSystem`.
39 */
40 nixos = import ./nixos/lib { lib = final; };
41
42 /**
43 Create a NixOS system configuration.
44
45 Example:
46
47 lib.nixosSystem {
48 modules = [ ./configuration.nix ];
49 }
50
51 Inputs:
52
53 - `modules` (list of paths or inline modules): The NixOS modules to include in the system configuration.
54
55 - `specialArgs` (attribute set): Extra arguments to pass to all modules, that are available in `imports` but can not be extended or overridden by the `modules`.
56
57 - `modulesLocation` (path): A default location for modules that aren't passed by path, used for error messages.
58
59 Legacy inputs:
60
61 - `system`: Legacy alias for `nixpkgs.hostPlatform`, but this is already set in the generated `hardware-configuration.nix`, included by `configuration.nix`.
62 - `pkgs`: Legacy alias for `nixpkgs.pkgs`; use `nixpkgs.pkgs` and `nixosModules.readOnlyPkgs` instead.
63 */
64 nixosSystem =
65 args:
66 import ./nixos/lib/eval-config.nix (
67 {
68 lib = final;
69 # Allow system to be set modularly in nixpkgs.system.
70 # We set it to null, to remove the "legacy" entrypoint's
71 # non-hermetic default.
72 system = null;
73
74 modules = args.modules ++ [
75 # This module is injected here since it exposes the nixpkgs self-path in as
76 # constrained of contexts as possible to avoid more things depending on it and
77 # introducing unnecessary potential fragility to changes in flakes itself.
78 #
79 # See: failed attempt to make pkgs.path not copy when using flakes:
80 # https://github.com/NixOS/nixpkgs/pull/153594#issuecomment-1023287913
81 (
82 {
83 config,
84 pkgs,
85 lib,
86 ...
87 }:
88 {
89 config.nixpkgs.flake.source = self.outPath;
90 }
91 )
92 ];
93 }
94 // builtins.removeAttrs args [ "modules" ]
95 );
96 }
97 );
98
99 checks = forAllSystems (
100 system:
101 { }
102 //
103 lib.optionalAttrs
104 (
105 # Exclude x86_64-freebsd because "Failed to evaluate rustc-wrapper-1.85.0: «broken»: is marked as broken"
106 system != "x86_64-freebsd"
107 )
108 {
109 tarball = jobs.${system}.tarball;
110 }
111 //
112 lib.optionalAttrs
113 (
114 self.legacyPackages.${system}.stdenv.hostPlatform.isLinux
115 # Exclude power64 due to "libressl is not available on the requested hostPlatform" with hostPlatform being power64
116 && !self.legacyPackages.${system}.targetPlatform.isPower64
117 # Exclude armv6l-linux because "cannot bootstrap GHC on this platform ('armv6l-linux' with libc 'defaultLibc')"
118 && system != "armv6l-linux"
119 # Exclude riscv64-linux because "cannot bootstrap GHC on this platform ('riscv64-linux' with libc 'defaultLibc')"
120 && system != "riscv64-linux"
121 )
122 {
123 # Test that ensures that the nixosSystem function can accept a lib argument
124 # Note: prefer not to extend or modify `lib`, especially if you want to share reusable modules
125 # alternatives include: `import` a file, or put a custom library in an option or in `_module.args.<libname>`
126 nixosSystemAcceptsLib =
127 (self.lib.nixosSystem {
128 pkgs = self.legacyPackages.${system};
129 lib = self.lib.extend (
130 final: prev: {
131 ifThisFunctionIsMissingTheTestFails = final.id;
132 }
133 );
134 modules = [
135 ./nixos/modules/profiles/minimal.nix
136 (
137 { lib, ... }:
138 lib.ifThisFunctionIsMissingTheTestFails {
139 # Define a minimal config without eval warnings
140 nixpkgs.hostPlatform = "x86_64-linux";
141 boot.loader.grub.enable = false;
142 fileSystems."/".device = "nodev";
143 # See https://search.nixos.org/options?show=system.stateVersion&query=stateversion
144 system.stateVersion = lib.trivial.release; # DON'T do this in real configs!
145 }
146 )
147 ];
148 }).config.system.build.toplevel;
149 }
150 );
151
152 htmlDocs = {
153 nixpkgsManual = builtins.mapAttrs (_: jobSet: jobSet.manual) jobs;
154 nixosManual =
155 (import ./nixos/release-small.nix {
156 nixpkgs = self;
157 }).nixos.manual;
158 };
159
160 devShells = forAllSystems (
161 system:
162 { }
163 //
164 lib.optionalAttrs
165 (
166 # Exclude armv6l-linux because "Package ‘ghc-9.6.6’ in .../pkgs/development/compilers/ghc/common-hadrian.nix:579 is not available on the requested hostPlatform"
167 system != "armv6l-linux"
168 # Exclude riscv64-linux because "Package ‘ghc-9.6.6’ in .../pkgs/development/compilers/ghc/common-hadrian.nix:579 is not available on the requested hostPlatform"
169 && system != "riscv64-linux"
170 # Exclude x86_64-freebsd because "Package ‘ghc-9.6.6’ in .../pkgs/development/compilers/ghc/common-hadrian.nix:579 is not available on the requested hostPlatform"
171 && system != "x86_64-freebsd"
172 )
173 {
174 /**
175 A shell to get tooling for Nixpkgs development. See nixpkgs/shell.nix.
176 */
177 default = import ./shell.nix { inherit system; };
178 }
179 );
180
181 formatter = lib.filterAttrs (
182 system: _:
183 # Exclude armv6l-linux because "cannot bootstrap GHC on this platform ('armv6l-linux' with libc 'defaultLibc')"
184 system != "armv6l-linux"
185 # Exclude riscv64-linux because "cannot bootstrap GHC on this platform ('riscv64-linux' with libc 'defaultLibc')"
186 && system != "riscv64-linux"
187 # Exclude x86_64-freebsd because "Package ‘go-1.22.12-freebsd-amd64-bootstrap’ in /nix/store/0yw40qnrar3lvc5hax5n49abl57apjbn-source/pkgs/development/compilers/go/binary.nix:50 is not available on the requested hostPlatform"
188 && system != "x86_64-freebsd"
189 ) (forAllSystems (system: (import ./ci { inherit system; }).fmt.pkg));
190
191 /**
192 A nested structure of [packages](https://nix.dev/manual/nix/latest/glossary#package-attribute-set) and other values.
193
194 The "legacy" in `legacyPackages` doesn't imply that the packages exposed
195 through this attribute are "legacy" packages. Instead, `legacyPackages`
196 is used here as a substitute attribute name for `packages`. The problem
197 with `packages` is that it makes operations like `nix flake show
198 nixpkgs` unusably slow due to the sheer number of packages the Nix CLI
199 needs to evaluate. But when the Nix CLI sees a `legacyPackages`
200 attribute it displays `omitted` instead of evaluating all packages,
201 which keeps `nix flake show` on Nixpkgs reasonably fast, though less
202 information rich.
203
204 The reason why finding the tree structure of `legacyPackages` is slow,
205 is that for each attribute in the tree, it is necessary to check whether
206 the attribute value is a package or a package set that needs further
207 evaluation. Evaluating the attribute value tends to require a significant
208 amount of computation, even considering lazy evaluation.
209 */
210 legacyPackages = forAllSystems (
211 system:
212 (import ./. {
213 inherit system;
214 overlays = import ./pkgs/top-level/impure-overlays.nix ++ [
215 (final: prev: {
216 lib = prev.lib.extend libVersionInfoOverlay;
217 })
218 ];
219 })
220 );
221
222 /**
223 Optional modules that can be imported into a NixOS configuration.
224
225 Example:
226
227 # flake.nix
228 outputs = { nixpkgs, ... }: {
229 nixosConfigurations = {
230 foo = nixpkgs.lib.nixosSystem {
231 modules = [
232 ./foo/configuration.nix
233 nixpkgs.nixosModules.notDetected
234 ];
235 };
236 };
237 };
238 */
239 nixosModules = {
240 notDetected = ./nixos/modules/installer/scan/not-detected.nix;
241
242 /**
243 Make the `nixpkgs.*` configuration read-only. Guarantees that `pkgs`
244 is the way you initialize it.
245
246 Example:
247
248 {
249 imports = [ nixpkgs.nixosModules.readOnlyPkgs ];
250 nixpkgs.pkgs = nixpkgs.legacyPackages.x86_64-linux;
251 }
252 */
253 readOnlyPkgs = ./nixos/modules/misc/nixpkgs/read-only.nix;
254 };
255 };
256}