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