summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Hensing <roberth@users.noreply.github.com>2022-04-05 20:05:33 +0200
committerGitHub <noreply@github.com>2022-04-05 20:05:33 +0200
commit559ac3c9e792ae905497528d22e31f9fa9df5982 (patch)
treed3acba16e06763557797a8666198e96425fb515c
parentf1aa084baa724e97e8aa24b28f6d000952a31340 (diff)
parentfffabe7500e972eb5275199c82a35132a07fa29f (diff)
downloadnixpkgs-559ac3c9e792ae905497528d22e31f9fa9df5982.tar
nixpkgs-559ac3c9e792ae905497528d22e31f9fa9df5982.tar.gz
nixpkgs-559ac3c9e792ae905497528d22e31f9fa9df5982.tar.bz2
nixpkgs-559ac3c9e792ae905497528d22e31f9fa9df5982.tar.lz
nixpkgs-559ac3c9e792ae905497528d22e31f9fa9df5982.tar.xz
nixpkgs-559ac3c9e792ae905497528d22e31f9fa9df5982.tar.zst
nixpkgs-559ac3c9e792ae905497528d22e31f9fa9df5982.zip
Merge pull request #166383 from hercules-ci/always-sanitize-derivation-name
Always sanitize derivation name
-rw-r--r--lib/strings.nix9
-rw-r--r--lib/tests/misc.nix5
-rw-r--r--nixos/tests/nixops/default.nix2
-rw-r--r--pkgs/build-support/test-equal-derivation.nix2
-rw-r--r--pkgs/build-support/trivial-builders.nix3
-rw-r--r--pkgs/stdenv/generic/make-derivation.nix9
6 files changed, 22 insertions, 8 deletions
diff --git a/lib/strings.nix b/lib/strings.nix
index d34263c9949..820d1901f94 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -756,7 +756,14 @@ rec {
        sanitizeDerivationName pkgs.hello
        => "-nix-store-2g75chlbpxlrqn15zlby2dfh8hr9qwbk-hello-2.10"
   */
-  sanitizeDerivationName = string: lib.pipe string [
+  sanitizeDerivationName =
+  let okRegex = match "[[:alnum:]+_?=-][[:alnum:]+._?=-]*";
+  in
+  string:
+  # First detect the common case of already valid strings, to speed those up
+  if stringLength string <= 207 && okRegex string != null
+  then unsafeDiscardStringContext string
+  else lib.pipe string [
     # Get rid of string context. This is safe under the assumption that the
     # resulting string is only used as a derivation name
     unsafeDiscardStringContext
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index 1eb2d953ebb..c7cef2a9059 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -649,6 +649,11 @@ runTests {
     expected = "foo";
   };
 
+  testSanitizeDerivationNameUnicode = testSanitizeDerivationName {
+    name = "fö";
+    expected = "f-";
+  };
+
   testSanitizeDerivationNameAscii = testSanitizeDerivationName {
     name = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
     expected = "-+--.-0123456789-=-?-ABCDEFGHIJKLMNOPQRSTUVWXYZ-_-abcdefghijklmnopqrstuvwxyz-";
diff --git a/nixos/tests/nixops/default.nix b/nixos/tests/nixops/default.nix
index f0834c51f0b..227b3881507 100644
--- a/nixos/tests/nixops/default.nix
+++ b/nixos/tests/nixops/default.nix
@@ -97,7 +97,7 @@ let
     derivations and all build dependency outputs, all the way down.
   */
   allDrvOutputs = pkg:
-    let name = lib.strings.sanitizeDerivationName "allDrvOutputs-${pkg.pname or pkg.name or "unknown"}";
+    let name = "allDrvOutputs-${pkg.pname or pkg.name or "unknown"}";
     in
     pkgs.runCommand name { refs = pkgs.writeReferencesToFile pkg.drvPath; } ''
       touch $out
diff --git a/pkgs/build-support/test-equal-derivation.nix b/pkgs/build-support/test-equal-derivation.nix
index 5d2185ce165..652f3716b2a 100644
--- a/pkgs/build-support/test-equal-derivation.nix
+++ b/pkgs/build-support/test-equal-derivation.nix
@@ -23,7 +23,7 @@ let
   drvB = builtins.unsafeDiscardOutputDependency b.drvPath or (throw "testEqualDerivation third argument must be a package");
   name =
     if a?name
-    then lib.strings.sanitizeDerivationName "testEqualDerivation-${a.name}"
+    then "testEqualDerivation-${a.name}"
     else "testEqualDerivation";
 in
 if drvA == drvB then
diff --git a/pkgs/build-support/trivial-builders.nix b/pkgs/build-support/trivial-builders.nix
index 68f0f1bc4dd..c3072357a57 100644
--- a/pkgs/build-support/trivial-builders.nix
+++ b/pkgs/build-support/trivial-builders.nix
@@ -70,8 +70,7 @@ rec {
     # name of the resulting derivation
     }: buildCommand:
     stdenv.mkDerivation ({
-      name = lib.strings.sanitizeDerivationName name;
-      inherit buildCommand;
+      inherit buildCommand name;
       passAsFile = [ "buildCommand" ]
         ++ (derivationArgs.passAsFile or []);
     }
diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix
index 2465449867c..8749e8b7555 100644
--- a/pkgs/stdenv/generic/make-derivation.nix
+++ b/pkgs/stdenv/generic/make-derivation.nix
@@ -219,9 +219,11 @@ else let
           # it again.
           staticMarker = lib.optionalString stdenv.hostPlatform.isStatic "-static";
         in
+        lib.strings.sanitizeDerivationName (
           if attrs ? name
           then attrs.name + hostSuffix
-          else "${attrs.pname}${staticMarker}${hostSuffix}-${attrs.version}";
+          else "${attrs.pname}${staticMarker}${hostSuffix}-${attrs.version}"
+        );
     }) // {
       builder = attrs.realBuilder or stdenv.shell;
       args = attrs.args or ["-e" (attrs.builder or ./default-builder.sh)];
@@ -340,8 +342,9 @@ else let
   # passed to the builder and is not a dependency.  But since we
   # include it in the result, it *is* available to nix-env for queries.
   meta = {
-      # `name` above includes cross-compilation cruft (and is under assert),
-      # lets have a clean always accessible version here.
+      # `name` above includes cross-compilation cruft,
+      # is under assert, and is sanitized.
+      # Let's have a clean always accessible version here.
       name = attrs.name or "${attrs.pname}-${attrs.version}";
 
       # If the packager hasn't specified `outputsToInstall`, choose a default,