|
1 | 1 | systemOrPkgs: |
2 | 2 | let |
3 | | - inherit (builtins) replaceStrings toJSON isAttrs foldl' unsafeDiscardStringContext elemAt match split concatStringsSep isList substring stringLength length attrNames; |
| 3 | + inherit (builtins) |
| 4 | + replaceStrings |
| 5 | + toJSON |
| 6 | + isAttrs |
| 7 | + foldl' |
| 8 | + unsafeDiscardStringContext |
| 9 | + elemAt |
| 10 | + match |
| 11 | + split |
| 12 | + concatStringsSep |
| 13 | + isList |
| 14 | + substring |
| 15 | + stringLength |
| 16 | + length |
| 17 | + attrNames |
| 18 | + ; |
4 | 19 | system = systemOrPkgs.system or systemOrPkgs; |
5 | 20 | pipe = val: functions: foldl' (x: f: f x) val functions; |
6 | 21 | max = x: y: if x > y then x else y; |
7 | 22 |
|
8 | 23 | # Minimized copy-paste https://github.com/NixOS/nixpkgs/blob/master/lib/strings.nix#L746-L762 |
9 | | - sanitizeDerivationName = string: pipe (toString string) [ |
10 | | - # Get rid of string context. This is safe under the assumption that the |
11 | | - # resulting string is only used as a derivation name |
12 | | - unsafeDiscardStringContext |
13 | | - # Strip all leading "." |
14 | | - (x: elemAt (match "\\.*(.*)" x) 0) |
15 | | - # Split out all invalid characters |
16 | | - # https://github.com/NixOS/nix/blob/2.3.2/src/libstore/store-api.cc#L85-L112 |
17 | | - # https://github.com/NixOS/nix/blob/2242be83c61788b9c0736a92bb0b5c7bbfc40803/nix-rust/src/store/path.rs#L100-L125 |
18 | | - (split "[^[:alnum:]+._?=-]+") |
19 | | - # Replace invalid character ranges with a "-" |
20 | | - (map (s: if isList s then "-" else s)) |
21 | | - (concatStringsSep "") |
22 | | - # Limit to 211 characters (minus 4 chars for ".drv") |
23 | | - (x: substring (max (stringLength x - 207) 0) (-1) x) |
24 | | - # If the result is empty, replace it with "?EMPTY?" |
25 | | - (x: if stringLength x == 0 then "?EMPTY?" else x) |
26 | | - ]; |
| 24 | + sanitizeDerivationName = |
| 25 | + string: |
| 26 | + pipe (toString string) [ |
| 27 | + # Get rid of string context. This is safe under the assumption that the |
| 28 | + # resulting string is only used as a derivation name |
| 29 | + unsafeDiscardStringContext |
| 30 | + # Strip all leading "." |
| 31 | + (x: elemAt (match "\.*(.*)" x) 0) |
| 32 | + # Split out all invalid characters |
| 33 | + # https://github.com/NixOS/nix/blob/2.3.2/src/libstore/store-api.cc#L85-L112 |
| 34 | + # https://github.com/NixOS/nix/blob/2242be83c61788b9c0736a92bb0b5c7bbfc40803/nix-rust/src/store/path.rs#L100-L125 |
| 35 | + (split "[^[:alnum:]+._?=-]+") |
| 36 | + # Replace invalid character ranges with a "-" |
| 37 | + (map (s: if isList s then "-" else s)) |
| 38 | + (concatStringsSep "") |
| 39 | + # Limit to 211 characters (minus 4 chars for ".drv") |
| 40 | + (x: substring (max (stringLength x - 207) 0) (-1) x) |
| 41 | + # If the result is empty, replace it with "?EMPTY?" |
| 42 | + (x: if stringLength x == 0 then "?EMPTY?" else x) |
| 43 | + ]; |
27 | 44 |
|
28 | 45 | # Minimized version of 'sanitizeDerivationName' function |
29 | | - str = it: |
| 46 | + str = |
| 47 | + it: |
30 | 48 | if it == null then |
31 | 49 | "null" |
32 | 50 | # TODO: Better solution is needed |
33 | 51 | else if isAttrs it then |
34 | 52 | (replaceStrings [ "-" ] [ "" ] (sanitizeDerivationName (toJSON it))) |
35 | | - else sanitizeDerivationName it; |
| 53 | + else |
| 54 | + sanitizeDerivationName it; |
36 | 55 |
|
37 | | - test = name: command: derivation { |
38 | | - inherit system; |
39 | | - name = substring 0 (211 - 33) name; |
40 | | - builder = "/bin/sh"; |
41 | | - args = [ "-c" command ]; |
42 | | - }; |
| 56 | + test = |
| 57 | + name: command: |
| 58 | + derivation { |
| 59 | + inherit system; |
| 60 | + name = substring 0 (211 - 33) name; |
| 61 | + builder = "/bin/sh"; |
| 62 | + args = [ |
| 63 | + "-c" |
| 64 | + command |
| 65 | + ]; |
| 66 | + }; |
43 | 67 | in |
44 | 68 | { |
45 | 69 |
|
46 | | - isEqual = a: b: |
47 | | - if a == b |
48 | | - then test "SUCCESS__${str a}__IS_EQUAL__${str b}" "echo success > $out" |
49 | | - else test "FAILURE__${str a}__NOT_EQUAL__${str b}" "exit 1"; |
| 70 | + isEqual = |
| 71 | + a: b: |
| 72 | + if a == b then |
| 73 | + test "SUCCESS__${str a}__IS_EQUAL__${str b}" "echo success > $out" |
| 74 | + else |
| 75 | + test "FAILURE__${str a}__NOT_EQUAL__${str b}" "exit 1"; |
50 | 76 |
|
51 | | - hasKey = attrset: key: |
52 | | - if attrset ? ${str key} |
53 | | - then test "SUCCESS__${str key}__EXISTS_IN_ATTRSET" "echo success > $out" |
54 | | - else test "FAILURE__${str key}__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_${str(length (attrNames attrset))}" "exit 1"; |
| 77 | + hasKey = |
| 78 | + attrset: key: |
| 79 | + if attrset ? ${str key} then |
| 80 | + test "SUCCESS__${str key}__EXISTS_IN_ATTRSET" "echo success > $out" |
| 81 | + else |
| 82 | + test "FAILURE__${str key}__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_${str (length (attrNames attrset))}" "exit 1"; |
55 | 83 | } |
0 commit comments