summary refs log tree commit diff
path: root/lib/types.nix
diff options
context:
space:
mode:
authorNaïm Favier <n@monade.li>2022-03-09 13:14:22 +0100
committerNaïm Favier <n@monade.li>2022-03-11 11:26:34 +0100
commit9c2266c03171dcf492b6accdb0cde0cb28e156b5 (patch)
tree224647a4a2e30e03b2580efcf6f5d0303b93916b /lib/types.nix
parenta782fa14c8c13e66e1642a310902566a804bb9dc (diff)
downloadnixpkgs-9c2266c03171dcf492b6accdb0cde0cb28e156b5.tar
nixpkgs-9c2266c03171dcf492b6accdb0cde0cb28e156b5.tar.gz
nixpkgs-9c2266c03171dcf492b6accdb0cde0cb28e156b5.tar.bz2
nixpkgs-9c2266c03171dcf492b6accdb0cde0cb28e156b5.tar.lz
nixpkgs-9c2266c03171dcf492b6accdb0cde0cb28e156b5.tar.xz
nixpkgs-9c2266c03171dcf492b6accdb0cde0cb28e156b5.tar.zst
nixpkgs-9c2266c03171dcf492b6accdb0cde0cb28e156b5.zip
lib.types.package: only call toDerivation when necessary
The current logic assumes that everything that isn't a derivation is a
store path, but it can also be something that's *coercible* to a store
path, like a flake input.

Unnecessary uses of `lib.toDerivation` result in errors in pure evaluation
mode when `builtins.storePath` is disabled.

Also document what a `package` is.
Diffstat (limited to 'lib/types.nix')
-rw-r--r--lib/types.nix12
1 files changed, 10 insertions, 2 deletions
diff --git a/lib/types.nix b/lib/types.nix
index 3fcac9c31b3..bf18866e55e 100644
--- a/lib/types.nix
+++ b/lib/types.nix
@@ -368,13 +368,21 @@ rec {
       emptyValue = { value = {}; };
     };
 
-    # derivation is a reserved keyword.
+    # A package is a top-level store path (/nix/store/hash-name). This includes:
+    # - derivations
+    # - more generally, attribute sets with an `outPath` or `__toString` attribute
+    #   pointing to a store path, e.g. flake inputs
+    # - strings with context, e.g. "${pkgs.foo}" or (toString pkgs.foo)
+    # - hardcoded store path literals (/nix/store/hash-foo) or strings without context
+    #   ("/nix/store/hash-foo"). These get a context added to them using builtins.storePath.
     package = mkOptionType {
       name = "package";
       check = x: isDerivation x || isStorePath x;
       merge = loc: defs:
         let res = mergeOneOption loc defs;
-        in if isDerivation res then res else toDerivation res;
+        in if builtins.isPath res || (builtins.isString res && ! builtins.hasContext res)
+          then toDerivation res
+          else res;
     };
 
     shellPackage = package // {