1 { config, options, lib, pkgs, ... }:
3 cfg = config.nixpkgs.flake;
6 options.nixpkgs.flake = {
7 source = lib.mkOption {
8 # In newer Nix versions, particularly with lazy trees, outPath of
9 # flakes becomes a Nix-language path object. We deliberately allow this
10 # to gracefully come through the interface in discussion with @roberth.
12 # See: https://github.com/NixOS/nixpkgs/pull/278522#discussion_r1460292639
13 type = lib.types.nullOr (lib.types.either lib.types.str lib.types.path);
16 defaultText = "if (using nixpkgsFlake.lib.nixosSystem) then self.outPath else null";
18 example = ''builtins.fetchTarball { name = "source"; sha256 = "${lib.fakeHash}"; url = "https://github.com/nixos/nixpkgs/archive/somecommit.tar.gz"; }'';
21 The path to the nixpkgs sources used to build the system. This is automatically set up to be
22 the store path of the nixpkgs flake used to build the system if using
23 `nixpkgs.lib.nixosSystem`, and is otherwise null by default.
25 This can also be optionally set if the NixOS system is not built with a flake but still uses
26 pinned sources: set this to the store path for the nixpkgs sources used to build the system,
27 as may be obtained by `builtins.fetchTarball`, for example.
29 Note: the name of the store path must be "source" due to
30 <https://github.com/NixOS/nix/issues/7075>.
34 setNixPath = lib.mkOption {
35 type = lib.types.bool;
37 default = cfg.source != null;
38 defaultText = "config.nixpkgs.flake.source != null";
41 Whether to set {env}`NIX_PATH` to include `nixpkgs=flake:nixpkgs` such that `<nixpkgs>`
42 lookups receive the version of nixpkgs that the system was built with, in concert with
43 {option}`nixpkgs.flake.setFlakeRegistry`.
45 This is on by default for NixOS configurations built with flakes.
47 This makes {command}`nix-build '<nixpkgs>' -A hello` work out of the box on flake systems.
49 Note that this option makes the NixOS closure depend on the nixpkgs sources, which may add
50 undesired closure size if the system will not have any nix commands run on it.
54 setFlakeRegistry = lib.mkOption {
55 type = lib.types.bool;
57 default = cfg.source != null;
58 defaultText = "config.nixpkgs.flake.source != null";
61 Whether to pin nixpkgs in the system-wide flake registry (`/etc/nix/registry.json`) to the
62 store path of the sources of nixpkgs used to build the NixOS system.
64 This is on by default for NixOS configurations built with flakes.
66 This option makes {command}`nix run nixpkgs#hello` reuse dependencies from the system, avoid
67 refetching nixpkgs, and have a consistent result every time.
69 Note that this option makes the NixOS closure depend on the nixpkgs sources, which may add
70 undesired closure size if the system will not have any nix commands run on it.
75 config = lib.mkIf (cfg.source != null) (lib.mkMerge [
79 assertion = cfg.setNixPath -> cfg.setFlakeRegistry;
81 Setting `nixpkgs.flake.setNixPath` requires that `nixpkgs.flake.setFlakeRegistry` also
82 be set, since it is implemented in terms of indirection through the flake registry.
87 (lib.mkIf cfg.setFlakeRegistry {
88 nix.registry.nixpkgs.to = lib.mkDefault {
93 (lib.mkIf cfg.setNixPath {
94 # N.B. This does not include nixos-config in NIX_PATH unlike modules/config/nix-channel.nix
95 # because we would need some kind of evil shim taking the *calling* flake's self path,
96 # perhaps, to ever make that work (in order to know where the Nix expr for the system came
97 # from and how to call it).
98 nix.nixPath = lib.mkDefault ([ "nixpkgs=flake:nixpkgs" ]
99 ++ lib.optional config.nix.channel.enable "/nix/var/nix/profiles/per-user/root/channels");