Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at fix-function-merge 165 lines 4.7 kB view raw
1{ 2 devShellTools, 3 emptyFile, 4 lib, 5 stdenv, 6 hello, 7 writeText, 8 zlib, 9 nixosTests, 10}: 11let 12 inherit (lib) 13 concatLines 14 escapeShellArg 15 isString 16 mapAttrsToList 17 ; 18in 19lib.recurseIntoAttrs { 20 21 # nix-build -A tests.devShellTools.nixos 22 nixos = nixosTests.docker-tools-nix-shell; 23 24 # nix-build -A tests.devShellTools.valueToString 25 valueToString = 26 let inherit (devShellTools) valueToString; in 27 28 stdenv.mkDerivation { 29 name = "devShellTools-valueToString-built-tests"; 30 31 # Test inputs 32 inherit emptyFile hello; 33 one = 1; 34 boolTrue = true; 35 boolFalse = false; 36 foo = "foo"; 37 list = [ 1 2 3 ]; 38 pathDefaultNix = ./default.nix; 39 packages = [ hello emptyFile ]; 40 # TODO: nested lists 41 42 buildCommand = '' 43 touch $out 44 ( set -x 45 [[ "$one" = ${escapeShellArg (valueToString 1)} ]] 46 [[ "$boolTrue" = ${escapeShellArg (valueToString true)} ]] 47 [[ "$boolFalse" = ${escapeShellArg (valueToString false)} ]] 48 [[ "$foo" = ${escapeShellArg (valueToString "foo")} ]] 49 [[ "$hello" = ${escapeShellArg (valueToString hello)} ]] 50 [[ "$list" = ${escapeShellArg (valueToString [ 1 2 3 ])} ]] 51 [[ "$packages" = ${escapeShellArg (valueToString [ hello emptyFile ])} ]] 52 [[ "$pathDefaultNix" = ${escapeShellArg (valueToString ./default.nix)} ]] 53 [[ "$emptyFile" = ${escapeShellArg (valueToString emptyFile)} ]] 54 ) >log 2>&1 || { cat log; exit 1; } 55 ''; 56 }; 57 58 # nix-build -A tests.devShellTools.valueToString 59 unstructuredDerivationInputEnv = 60 let 61 inherit (devShellTools) unstructuredDerivationInputEnv; 62 63 drvAttrs = { 64 one = 1; 65 boolTrue = true; 66 boolFalse = false; 67 foo = "foo"; 68 list = [ 1 2 3 ]; 69 pathDefaultNix = ./default.nix; 70 stringWithDep = "Exe: ${hello}/bin/hello"; 71 aPackageAttrSet = hello; 72 anOutPath = hello.outPath; 73 anAnAlternateOutput = zlib.dev; 74 args = [ "args must not be added to the environment" "Nix doesn't do it." ]; 75 76 passAsFile = [ "bar" ]; 77 bar = '' 78 bar 79 ${writeText "qux" "yea"} 80 ''; 81 }; 82 result = unstructuredDerivationInputEnv { inherit drvAttrs; }; 83 in 84 assert result // { barPath = "<check later>"; } == { 85 one = "1"; 86 boolTrue = "1"; 87 boolFalse = ""; 88 foo = "foo"; 89 list = "1 2 3"; 90 pathDefaultNix = "${./default.nix}"; 91 stringWithDep = "Exe: ${hello}/bin/hello"; 92 aPackageAttrSet = "${hello}"; 93 anOutPath = "${hello.outPath}"; 94 anAnAlternateOutput = "${zlib.dev}"; 95 96 passAsFile = "bar"; 97 barPath = "<check later>"; 98 }; 99 100 # Not runCommand, because it alters `passAsFile` 101 stdenv.mkDerivation ({ 102 name = "devShellTools-unstructuredDerivationInputEnv-built-tests"; 103 104 exampleBarPathString = 105 assert isString result.barPath; 106 result.barPath; 107 108 dontUnpack = true; 109 dontBuild = true; 110 dontFixup = true; 111 doCheck = true; 112 113 installPhase = "touch $out"; 114 115 checkPhase = '' 116 fail() { 117 echo "$@" >&2 118 exit 1 119 } 120 checkAttr() { 121 echo checking attribute $1... 122 if [[ "$2" != "$3" ]]; then 123 echo "expected: $3" 124 echo "actual: $2" 125 exit 1 126 fi 127 } 128 ${ 129 concatLines 130 (mapAttrsToList 131 (name: value: 132 "checkAttr ${name} \"\$${name}\" ${escapeShellArg value}" 133 ) 134 (removeAttrs 135 result 136 [ 137 "args" 138 139 # Nix puts it in workdir, which is not a concept for 140 # unstructuredDerivationInputEnv, so we have to put it in the 141 # store instead. This means the full path won't match. 142 "barPath" 143 ]) 144 ) 145 } 146 ( 147 set -x 148 149 diff $exampleBarPathString $barPath 150 151 ${lib.optionalString (builtins?convertHash) '' 152 [[ "$(basename $exampleBarPathString)" = "$(basename $barPath)" ]] 153 ''} 154 ) 155 156 ''${args:+fail "args should not be set by Nix. We don't expect it to and unstructuredDerivationInputEnv removes it."} 157 if [[ "''${builder:-x}" == x ]]; then 158 fail "builder should be set by Nix. We don't remove it in unstructuredDerivationInputEnv." 159 fi 160 ''; 161 } // removeAttrs drvAttrs [ 162 # This would break the derivation. Instead, we have a check in the derivation to make sure Nix doesn't set it. 163 "args" 164 ]); 165}