git.iko.soy/typeable/nixpkgs
on master help sign in
lib/strings: Add sanitizeDerivationName function
commit4b206ac83ba23e44231088b1d5f23d96adbced05 authorSilvan Mosberger <contact@infinisil.com> dateMon 27 Jan 2020, 23:03 (7 years ago) parent5e46d5a
3 files changed+76−2
lib/attrsets.nix+2−2 lib/strings.nix+32 lib/tests/misc.nix+42
lib/attrsets.nix+2−2
@@ -4,7 +4,7 @@
44let
55 inherit (builtins) head tail length;
66 inherit (lib.trivial) and;
7− inherit (lib.strings) concatStringsSep;
7+ inherit (lib.strings) concatStringsSep sanitizeDerivationName;
88 inherit (lib.lists) fold concatMap concatLists;
99in
1010
@@ -310,7 +310,7 @@ rec {
310310 path' = builtins.storePath path;
311311 res =
312312 { type = "derivation";
313− name = builtins.unsafeDiscardStringContext (builtins.substring 33 (-1) (baseNameOf path'));
313+ name = sanitizeDerivationName (builtins.substring 33 (-1) (baseNameOf path'));
314314 outPath = path';
315315 outputs = [ "out" ];
316316 out = res;
lib/strings.nix+32
@@ -678,4 +678,36 @@ rec {
678678 => "1.0"
679679 */
680680 fileContents = file: removeSuffix "\n" (builtins.readFile file);
681+
682+
683+ /* Creates a valid derivation name from a potentially invalid one.
684+
685+ Type: sanitizeDerivationName :: String -> String
686+
687+ Example:
688+ sanitizeDerivationName "../hello.bar # foo"
689+ => "-hello.bar-foo"
690+ sanitizeDerivationName ""
691+ => "unknown"
692+ sanitizeDerivationName pkgs.hello
693+ => "-nix-store-2g75chlbpxlrqn15zlby2dfh8hr9qwbk-hello-2.10"
694+ */
695+ sanitizeDerivationName = string: lib.pipe string [
696+ # Get rid of string context. This is safe under the assumption that the
697+ # resulting string is only used as a derivation name
698+ builtins.unsafeDiscardStringContext
699+ # Strip all leading "."
700+ (x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0)
701+ # Split out all invalid characters
702+ # https://github.com/NixOS/nix/blob/2.3.2/src/libstore/store-api.cc#L85-L112
703+ # https://github.com/NixOS/nix/blob/2242be83c61788b9c0736a92bb0b5c7bbfc40803/nix-rust/src/store/path.rs#L100-L125
704+ (builtins.split "[^[:alnum:]+._?=-]+")
705+ # Replace invalid character ranges with a "-"
706+ (concatMapStrings (s: if lib.isList s then "-" else s))
707+ # Limit to 211 characters (minus 4 chars for ".drv")
708+ (x: substring (lib.max (stringLength x - 207) 0) (-1) x)
709+ # If the result is empty, replace it with "unknown"
710+ (x: if stringLength x == 0 then "unknown" else x)
711+ ];
712+
681713}
lib/tests/misc.nix+42
@@ -3,6 +3,23 @@
33# if the resulting list is empty, all tests passed
44with import ../default.nix;
55
6+let
7+
8+ testSanitizeDerivationName = { name, expected }:
9+ let
10+ drv = derivation {
11+ name = strings.sanitizeDerivationName name;
12+ builder = "x";
13+ system = "x";
14+ };
15+ in {
16+ # Evaluate the derivation so an invalid name would be caught
17+ expr = builtins.seq drv.drvPath drv.name;
18+ inherit expected;
19+ };
20+
21+in
22+
623runTests {
724
825
@@ -478,4 +495,29 @@ runTests {
478495
479496 expected = "'-X' 'PUT' '--data' '{\"id\":0}' '--retry' '3' '--url' 'https://example.com/foo' '--url' 'https://example.com/bar' '--verbose'";
480497 };
498+
499+ testSanitizeDerivationNameLeadingDots = testSanitizeDerivationName {
500+ name = "..foo";
501+ expected = "foo";
502+ };
503+
504+ testSanitizeDerivationNameAscii = testSanitizeDerivationName {
505+ name = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
506+ expected = "-+--.-0123456789-=-?-ABCDEFGHIJKLMNOPQRSTUVWXYZ-_-abcdefghijklmnopqrstuvwxyz-";
507+ };
508+
509+ testSanitizeDerivationNameTooLong = testSanitizeDerivationName {
510+ name = "This string is loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong";
511+ expected = "loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong";
512+ };
513+
514+ testSanitizeDerivationNameTooLongWithInvalid = testSanitizeDerivationName {
515+ name = "Hello there aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&&&&&&&";
516+ expected = "there-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-";
517+ };
518+
519+ testSanitizeDerivationNameEmpty = testSanitizeDerivationName {
520+ name = "";
521+ expected = "unknown";
522+ };
481523}