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