From 66fe92be8a0ad7eac6b10a58c7ba3a0ab78769d8 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Sat, 7 Jun 2025 05:53:36 +0200 Subject: [PATCH 1/2] aliases: move 'with self;' below let-in block, prefix 'super' with '_' This makes use of 'self' explicit in the let block, and communicates that 'super' should not generally be used in this file. --- pkgs/top-level/aliases.nix | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkgs/top-level/aliases.nix b/pkgs/top-level/aliases.nix index e34f83d9ce728..ad3a3f833c860 100644 --- a/pkgs/top-level/aliases.nix +++ b/pkgs/top-level/aliases.nix @@ -1,4 +1,4 @@ -lib: self: super: +lib: self: _super: ### Deprecated aliases - for backward compatibility ### Please maintain this list in ASCIIbetical ordering. @@ -17,8 +17,6 @@ lib: self: super: # distro aliases such as: # debian-package-name -> nixos-package-name -with self; - let # Removing recurseForDerivation prevents derivations of aliased attribute set # to appear while listing all the packages available. @@ -46,7 +44,7 @@ let "${p} has been renamed to ${p3} since ${p4} is also available. Note that upgrade caused data loss for some users so backup is recommended (see NixOS 24.11 release notes for details)"; deprecatedPlasma5Packages = { - inherit (plasma5Packages) + inherit (self.plasma5Packages) akonadi akregator arianna @@ -178,7 +176,7 @@ let zanshin ; - inherit (plasma5Packages.thirdParty) + inherit (self.plasma5Packages.thirdParty) krohnkite krunner-ssh krunner-symbols @@ -188,7 +186,7 @@ let plasma-applet-virtual-desktop-bar ; - inherit (libsForQt5) + inherit (self.libsForQt5) sddm ; }; @@ -209,7 +207,7 @@ let # Make sure that we are not shadowing something from all-packages.nix. checkInPkgs = n: alias: - if builtins.hasAttr n super then throw "Alias ${n} is still in all-packages.nix" else alias; + if builtins.hasAttr n _super then throw "Alias ${n} is still in all-packages.nix" else alias; mapAliases = aliases: @@ -218,6 +216,8 @@ let ) aliases; in +with self; + mapAliases { # Added 2018-07-16 preserve, reason: forceSystem should not be used directly in Nixpkgs. forceSystem = system: _: (import self.path { localSystem = { inherit system; }; }); From 3b5db57fc30a4d96cde8bc12358ced21c07f68ac Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Sat, 7 Jun 2025 06:14:28 +0200 Subject: [PATCH 2/2] aliases: warnOnInstantiate all aliases if `config.warnAliases=true` --- doc/release-notes/rl-2511.section.md | 3 ++ pkgs/top-level/aliases.nix | 51 ++++++++++++++++++++++++++-- pkgs/top-level/config.nix | 17 ++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/doc/release-notes/rl-2511.section.md b/doc/release-notes/rl-2511.section.md index 02b08039812b6..26a322b2e9611 100644 --- a/doc/release-notes/rl-2511.section.md +++ b/doc/release-notes/rl-2511.section.md @@ -9,6 +9,9 @@ and newer series. However, embedded chips without LSX (Loongson SIMD eXtension), such as 2K0300 SoC, are not supported. `pkgsCross.loongarch64-linux-embedded` can be used to build software and systems for these platforms. +- Package aliases now print a warning when used. This behaviour is controlled with the new `config.warnAliases` + which by default is `true`. + ## Backward Incompatibilities {#sec-nixpkgs-release-25.11-incompatibilities} diff --git a/pkgs/top-level/aliases.nix b/pkgs/top-level/aliases.nix index ad3a3f833c860..8e7d4eec9e3ac 100644 --- a/pkgs/top-level/aliases.nix +++ b/pkgs/top-level/aliases.nix @@ -1,4 +1,4 @@ -lib: self: _super: +lib': self: _super: ### Deprecated aliases - for backward compatibility ### Please maintain this list in ASCIIbetical ordering. @@ -209,14 +209,59 @@ let n: alias: if builtins.hasAttr n _super then throw "Alias ${n} is still in all-packages.nix" else alias; + # `config.warnAliases` machinery + # These are no-ops when warnAliases=false + + selfWithAliasWarningNames = + if _super.config.warnAliases then + lib.mapAttrs ( + name: package: + if lib.isDerivation package then + # smuggle the attribute name of each package + package // { __aliasWarningAttrName = name; } + else + package + ) self + else + self; + + addAliasWarnOnInstantiate = + if _super.config.warnAliases then + name: alias: + if !alias ? __aliasWarningAttrName then + alias + else + # our lib.warnOnInstantiate override also removes __aliasWarningAttrName + lib.warnOnInstantiate "'${name}' has been renamed to/replaced by '${alias.__aliasWarningAttrName}'" + alias + else + name: alias: alias; + + lib = lib'.extend ( + final: prev: + lib'.optionalAttrs _super.config.warnAliases { + # make warnOnInstantiate also remove __aliasWarningAttrName + # to enable tailoring alias warnings without producing double warnings + warnOnInstantiate = + message: package: + prev.warnOnInstantiate message (prev.removeAttrs package [ "__aliasWarningAttrName" ]); + } + ); + mapAliases = aliases: lib.mapAttrs ( - n: alias: removeDistribute (removeRecurseForDerivations (checkInPkgs n alias)) + name: alias: + addAliasWarnOnInstantiate name ( + removeDistribute (removeRecurseForDerivations (checkInPkgs name alias)) + ) ) aliases; in -with self; +with selfWithAliasWarningNames; +let + pkgs = selfWithAliasWarningNames; +in mapAliases { # Added 2018-07-16 preserve, reason: forceSystem should not be used directly in Nixpkgs. diff --git a/pkgs/top-level/config.nix b/pkgs/top-level/config.nix index 840c4e03e92d6..0e35ce604e7ad 100644 --- a/pkgs/top-level/config.nix +++ b/pkgs/top-level/config.nix @@ -101,6 +101,23 @@ let ''; }; + warnAliases = mkOption { + type = types.bool; + default = true; + description = '' + Whether to show warnings when accessing aliases. + + While it is true by default it is perfectly okay to disable + this. For example it make sense to disable alias warnings when + running `nix search`. + + This is a tool to gently let you know which attributes in your code + are aliases. Projects that aren't Nixpkgs should however be cautious + of instantly removing all usages of aliases, as migrating too soon + can break compatibility with the stable Nixpkgs releases. + ''; + }; + allowUnfree = mkOption { type = types.bool; default = false;