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