aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-24 01:04:21 +0000
committertv <tv@krebsco.de>2026-11-24 01:12:10 +0000
commita021d211906d23c9e3454da974806454e9ec74c2 (patch)
tree8a9186c4bc6b0050b83a89536c458d5c2f497355
parentba2dc2e941fe3170ccd85d319982d48d78081177 (diff)
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.
-rw-r--r--CHANGELOG.md3
-rw-r--r--README.md4
-rw-r--r--lib/with.nix3
-rw-r--r--pkgs/populate/README.md28
-rw-r--r--pkgs/populate/lib/default.nix12
-rw-r--r--pkgs/populate/lib/types/populate.nix16
-rw-r--r--pkgs/populate/tests/default.nix24
7 files changed, 83 insertions, 7 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 69b53c8..9f5f4a2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -37,6 +37,9 @@
- A `passage` source's `identities_file` defaults to
`~/.passage/identities` when populate runs, instead of during
evaluation.
+- `evalSource` leaves out a source whose type is set to null, e.g. with
+ the new `optionalSource`, which returns a type's settings if a condition
+ holds, and else null.
## 1.31.1
diff --git a/README.md b/README.md
index f197dc9..4ed56d2 100644
--- a/README.md
+++ b/README.md
@@ -345,7 +345,9 @@ that means.
passes to populate. [populate's
README](pkgs/populate/README.md#source-types) describes the source types:
`derivation`, `file`, `flake`, `flake-input`, `git`, `pass`, `passage`,
-`pipe`, and `symlink`.
+`pipe`, and `symlink`, and [how to write
+them](pkgs/populate/README.md#sources-in-nix), e.g. with
+`klib.optionalSource`, which leaves a source out unless a condition holds.
## Flake inputs from sources
diff --git a/lib/with.nix b/lib/with.nix
index b3ff7fa..bf164e0 100644
--- a/lib/with.nix
+++ b/lib/with.nix
@@ -14,7 +14,8 @@ in
rec {
# What populate's Nix library defines.
- inherit (populate.lib) evalSource flakeFollows hasGeneratedFlake derivesFlake;
+ inherit (populate.lib)
+ evalSource flakeFollows hasGeneratedFlake derivesFlake optionalSource;
maybeHostName = default: let
# We're parsing /etc/hostname here because reading
diff --git a/pkgs/populate/README.md b/pkgs/populate/README.md
index 74181e9..f28598a 100644
--- a/pkgs/populate/README.md
+++ b/pkgs/populate/README.md
@@ -170,6 +170,34 @@ nix-instantiate --eval --strict --json -E '
' | populate --force - root@server
```
+`evalSource` evaluates the sources with the module system, so a list's
+attribute sets get merged, and sources and settings take nixpkgs'
+`lib.mkIf`, `lib.mkMerge`, and the like. A source needs exactly one type
+that isn't null. One whose types are all null, at least one of them set to
+null, gets left out, so a source's type can depend on a condition with
+`optionalSource`, which returns its settings if the condition holds, and
+else null:
+
+```nix
+let
+ inherit (import ./lib {}) evalSource optionalSource;
+ local = false;
+in
+evalSource {
+ nixpkgs.file = optionalSource local { path = "/home/me/nixpkgs"; };
+ nixpkgs.git = optionalSource (!local) {
+ url = "https://github.com/NixOS/nixpkgs";
+ ref = "nixos-26.05";
+ };
+ secrets.passage = optionalSource local "/home/me/secrets";
+}
+```
+
+A type that `lib.mkIf false` leaves unset doesn't count as set to null: a
+source without a type is more likely a mistake, e.g. a forgotten type,
+than meant to be left out. So a source whose types are all unset is an
+error, as is one with more than one type.
+
Store paths in the sources, e.g. of a `file` source that is a derivation,
must exist when populate runs. `nix-instantiate --eval` doesn't build
them; building a file that contains the sources, e.g. with `pkgs.writeText
diff --git a/pkgs/populate/lib/default.nix b/pkgs/populate/lib/default.nix
index e9155c5..4ccc609 100644
--- a/pkgs/populate/lib/default.nix
+++ b/pkgs/populate/lib/default.nix
@@ -20,7 +20,8 @@ in rec {
# The sources, a list of attribute sets of sources by name, or one, with
# shorthands expanded and defaults filled in, and without settings that
- # are null, as populate reads them, e.g. with builtins.toJSON.
+ # are null, as populate reads them, e.g. with builtins.toJSON. Sources
+ # whose type is set to null are left out.
evalSource = let
eval = source: lib.evalModules {
modules = lib.singleton {
@@ -42,7 +43,14 @@ in rec {
string = x;
};
in
- source: sanitize (eval source).config.source;
+ source: sanitize
+ (lib.filterAttrs (_: source: source.type != null)
+ (eval source).config.source);
+
+ # The settings of a source type if cond holds, else null, which leaves
+ # the source out unless another type is set, e.g.
+ # nixpkgs.file = optionalSource local { path = ...; };
+ optionalSource = cond: settings: if cond then settings else null;
# Inputs a source draws from other sources, as list of attribute sets like
# { path = [ "stockholm" "nixpkgs" ]; source = "nixpkgs"; }.
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:
diff --git a/pkgs/populate/tests/default.nix b/pkgs/populate/tests/default.nix
index e0ce469..9693034 100644
--- a/pkgs/populate/tests/default.nix
+++ b/pkgs/populate/tests/default.nix
@@ -114,7 +114,31 @@ let
throw "tests: no scenario's name matches ${builtins.toJSON only}"
else
selected;
+
+ # evalSource leaves out sources whose types are set to null, and refuses
+ # ones without exactly one type.
+ checkSourceTypes = let
+ inherit (populateLib) evalSource optionalSource;
+ names = source: lib.attrNames (evalSource source);
+ fails = source:
+ !(builtins.tryEval (builtins.deepSeq (evalSource source) null)).success;
+ in
+ names {
+ a.file = optionalSource false "/a";
+ b.file = "/b";
+ } == [ "b" ] &&
+ names {
+ a.file = optionalSource false "/a";
+ a.git = optionalSource true { url = "https://example.org/a"; ref = "main"; };
+ } == [ "a" ] &&
+ fails { a.file = lib.mkIf false "/a"; } &&
+ fails { a = {}; } &&
+ fails { a.file = "/a"; a.symlink = "b"; } ||
+ throw "tests: evalSource doesn't leave out or refuse sources by their types as it should";
in
+
+assert checkSourceTypes;
+
testsOf false ""
# Not built by nix-build either, which runs these scenarios anyway.
// {