fix: Correctly resolve packages from preferences.packages.version (#33)

Before this, a value that does not correspond to an attribute under a
package alias' `versions` would make it default to the latest as defined
by `lib.packages.getLatest`, which isn't a silver bullt: it choose
"bash5.2.15-bootstrap" for instance instead of stage1-passthrough (and I
don;t think we have an easy fix for that).

Given values like `"1.2.3"` makes little sense in
`config.preferences.packages.version` (because it's a package-set-wide
preference), this means it was hardly ever useful.

before:

```console
❯ nix eval -f tidepool --apply 't:
  t.extend {modules = [{config.preferences.packages.version = "latest";}];}
  |> (t: t.config.lib.packages.resolve t.config.packages.foundation.bash)
  |> (p: p.version)'
"5.2.15-bootstrap"
❯ nix eval -f tidepool --apply 't:
  t.extend {modules = [{config.preferences.packages.version = "stable";}];}
  |> (t: t.config.lib.packages.resolve t.config.packages.foundation.bash)
  |> (p: p.version)'
"5.2.15-bootstrap"
```

after:

```console
❯ nix eval -f tidepool --apply 't:
  t.extend {modules = [{config.preferences.packages.version = "latest";}];}
  |> (t: t.config.lib.packages.resolve t.config.packages.foundation.bash)
  |> (p: p.version)'
"5.2.15-stage1-passthrough"
❯ nix eval -f tidepool --apply 't:
  t.extend {modules = [{config.preferences.packages.version = "stable";}];}
  |> (t: t.config.lib.packages.resolve t.config.packages.foundation.bash)
  |> (p: p.version)'
"5.2.15-bootstrap"
```

Co-authored-by: austreelis <git@swhaele.net>
Reviewed-on: auxolotl/labs#33
Reviewed-by: Ruby Iris Juric <ruby+auxolotl@srxl.me>
Co-authored-by: Austreelis <austreelis@noreply.git.auxolotl.org>
Co-committed-by: Austreelis <austreelis@noreply.git.auxolotl.org>
This commit is contained in:
Austreelis 2025-10-04 00:40:05 +00:00 committed by vlinkz
parent 45ac7bc494
commit b67847e7eb
3 changed files with 18 additions and 7 deletions

View file

@ -243,11 +243,22 @@ in
in
outputs;
WELL_KNOWN_ALIASES = [
# NOTE: When adding a version alias here, update lib.types.alias and lib.types.platforms' alias sub-option
"stable"
"latest"
];
resolve =
alias:
if alias ? versions then
alias.versions.${config.preferences.packages.version}
or (alias.versions.${lib.packages.getLatest alias})
let
prefered = config.preferences.packages.version;
isWellKnown = builtins.elem prefered lib.packages.WELL_KNOWN_ALIASES;
in
if isWellKnown && alias ? ${prefered} then
alias.${prefered}
else if alias ? versions then
alias.versions.${prefered} or (alias.versions.${lib.packages.getLatest alias})
else
alias;

View file

@ -307,6 +307,7 @@ in
type = lib.types.artifact;
default.value = null;
};
# NOTE: When adding a version alias here, update lib.packages.WELL_KNOWN_ALIASES
versions = lib.options.create {
description = "Available versions of the artifact.";
type = lib.types.attrs.of lib.types.artifact;
@ -401,6 +402,8 @@ in
if config.versions == { } then null else config.versions.${lib.packages.getLatest config};
};
# NOTE: When adding a version alias here, update lib.packages.WELL_KNOWN_ALIASES
versions = lib.options.create {
description = "Available versions of the package.";
type = lib.types.attrs.of lib.types.package;

View file

@ -37,10 +37,7 @@ in
preferences.packages = {
version = lib.options.create {
description = "The preferred package version when using aliases.";
type = lib.types.enum [
"latest"
"stable"
];
type = lib.types.enum lib.packages.WELL_KNOWN_ALIASES;
default.value = "latest";
};