From a021d211906d23c9e3454da974806454e9ec74c2 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 24 Nov 2026 01:04:21 +0000 Subject: populate lib: leave out sources whose types are set to null A source's type can depend on a condition, e.g. nixpkgs.file = optionalSource local { ... }, which returns the settings if the condition holds, and else null. A source whose types are all null, at least one of them set to null rather than left unset, gets the type null, and evalSource leaves it out. A type left unset, e.g. by lib.mkIf false, still counts as missing, so an empty source stays an error. krops' lib provides optionalSource, too. --- pkgs/populate/lib/types/populate.nix | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'pkgs/populate/lib/types') diff --git a/pkgs/populate/lib/types/populate.nix b/pkgs/populate/lib/types/populate.nix index e5de33f..6605f61 100644 --- a/pkgs/populate/lib/types/populate.nix +++ b/pkgs/populate/lib/types/populate.nix @@ -1,19 +1,29 @@ { lib }: rec { - source = lib.types.submodule ({ config, name, ... }: { + # A source, of the one type that isn't null. A source whose types are + # all null, at least one of them set to null rather than left unset, has + # the type null: evalSource leaves it out. + source = lib.types.submodule ({ config, name, options, ... }: { options = { type = let known-types = lib.attrNames source-types; type-candidates = lib.filter (k: config.${k} != null) known-types; + # Set to null, e.g. by optionalSource, rather than left unset, which + # leaves them at their default's priority. + nulled = lib.filter + (k: options.${k}.highestPrio < (lib.mkOptionDefault null).priority) + known-types; in lib.mkOption { default = if lib.length type-candidates == 1 then lib.head type-candidates else if type-candidates != [] then throw "populate: source ${name} has more than one type: ${lib.concatStringsSep ", " type-candidates}" + else if nulled != [] then + null else - throw "populate: source ${name} has no type; set one of ${lib.concatStringsSep ", " known-types}"; - type = lib.types.enum known-types; + throw "populate: source ${name} has no type; set one of ${lib.concatStringsSep ", " known-types}, or set one to null to leave the source out"; + type = lib.types.nullOr (lib.types.enum known-types); }; derivation = lib.mkOption { apply = x: -- cgit v1.3.1