stdenv: Conservatively move `mkDerivation` into it's own file

+168 -152
+13 -152
pkgs/stdenv/generic/default.nix
··· 27 27 let 28 28 inherit (targetPlatform) system; 29 29 30 - ifDarwin = attrs: if system == "x86_64-darwin" then attrs else {}; 31 - 32 30 defaultNativeBuildInputs = extraBuildInputs ++ 33 31 [ ../../build-support/setup-hooks/move-docs.sh 34 32 ../../build-support/setup-hooks/compress-man-pages.sh ··· 37 35 ] 38 36 # FIXME this on Darwin; see 39 37 # https://github.com/NixOS/nixpkgs/commit/94d164dd7#commitcomment-22030369 40 - ++ lib.optional result.isLinux ../../build-support/setup-hooks/audit-tmpdir.sh 38 + ++ lib.optional hostPlatform.isLinux ../../build-support/setup-hooks/audit-tmpdir.sh 41 39 ++ [ 42 40 ../../build-support/setup-hooks/multiple-outputs.sh 43 41 ../../build-support/setup-hooks/move-sbin.sh ··· 46 44 cc 47 45 ]; 48 46 49 - # `mkDerivation` wraps the builtin `derivation` function to 50 - # produce derivations that use this stdenv and its shell. 51 - # 52 - # See also: 53 - # 54 - # * https://nixos.org/nixpkgs/manual/#sec-using-stdenv 55 - # Details on how to use this mkDerivation function 56 - # 57 - # * https://nixos.org/nix/manual/#ssec-derivation 58 - # Explanation about derivations in general 59 - mkDerivation = 60 - { nativeBuildInputs ? [] 61 - , buildInputs ? [] 62 - 63 - , propagatedNativeBuildInputs ? [] 64 - , propagatedBuildInputs ? [] 65 - 66 - , crossConfig ? null 67 - , meta ? {} 68 - , passthru ? {} 69 - , pos ? # position used in error messages and for meta.position 70 - (if attrs.meta.description or null != null 71 - then builtins.unsafeGetAttrPos "description" attrs.meta 72 - else builtins.unsafeGetAttrPos "name" attrs) 73 - , separateDebugInfo ? false 74 - , outputs ? [ "out" ] 75 - , __impureHostDeps ? [] 76 - , __propagatedImpureHostDeps ? [] 77 - , sandboxProfile ? "" 78 - , propagatedSandboxProfile ? "" 79 - , ... } @ attrs: 80 - let 81 - dependencies = [ 82 - (map (drv: drv.nativeDrv or drv) nativeBuildInputs) 83 - (map (drv: drv.crossDrv or drv) buildInputs) 84 - ]; 85 - propagatedDependencies = [ 86 - (map (drv: drv.nativeDrv or drv) propagatedNativeBuildInputs) 87 - (map (drv: drv.crossDrv or drv) propagatedBuildInputs) 88 - ]; 89 - in let 90 - 91 - outputs' = 92 - outputs ++ 93 - (if separateDebugInfo then assert targetPlatform.isLinux; [ "debug" ] else []); 94 - 95 - dependencies' = let 96 - justMap = map lib.chooseDevOutputs dependencies; 97 - nativeBuildInputs = lib.elemAt justMap 0 98 - ++ lib.optional targetPlatform.isWindows ../../build-support/setup-hooks/win-dll-link.sh; 99 - buildInputs = lib.elemAt justMap 1 100 - # TODO(@Ericson2314): Should instead also be appended to `nativeBuildInputs`. 101 - ++ lib.optional separateDebugInfo ../../build-support/setup-hooks/separate-debug-info.sh; 102 - in [ nativeBuildInputs buildInputs ]; 103 - 104 - propagatedDependencies' = map lib.chooseDevOutputs propagatedDependencies; 105 - 106 - derivationArg = 107 - (removeAttrs attrs 108 - ["meta" "passthru" "crossAttrs" "pos" 109 - "__impureHostDeps" "__propagatedImpureHostDeps" 110 - "sandboxProfile" "propagatedSandboxProfile"]) 111 - // (let 112 - # TODO(@Ericson2314): Reversing of dep lists is just temporary to avoid Darwin mass rebuild. 113 - computedSandboxProfile = 114 - lib.concatMap (input: input.__propagatedSandboxProfile or []) (extraBuildInputs ++ lib.concatLists (lib.reverseList dependencies')); 115 - computedPropagatedSandboxProfile = 116 - lib.concatMap (input: input.__propagatedSandboxProfile or []) (lib.concatLists (lib.reverseList propagatedDependencies')); 117 - computedImpureHostDeps = 118 - lib.unique (lib.concatMap (input: input.__propagatedImpureHostDeps or []) (extraBuildInputs ++ lib.concatLists (lib.reverseList dependencies'))); 119 - computedPropagatedImpureHostDeps = 120 - lib.unique (lib.concatMap (input: input.__propagatedImpureHostDeps or []) (lib.concatLists (lib.reverseList propagatedDependencies'))); 121 - in 122 - { 123 - builder = attrs.realBuilder or shell; 124 - args = attrs.args or ["-e" (attrs.builder or ./default-builder.sh)]; 125 - stdenv = result; 126 - system = result.system; 127 - userHook = config.stdenv.userHook or null; 128 - __ignoreNulls = true; 129 - 130 - nativeBuildInputs = lib.elemAt dependencies' 0; 131 - buildInputs = lib.elemAt dependencies' 1; 132 - 133 - propagatedNativeBuildInputs = lib.elemAt propagatedDependencies' 0; 134 - propagatedBuildInputs = lib.elemAt propagatedDependencies' 1; 135 - } // ifDarwin { 136 - # TODO: remove lib.unique once nix has a list canonicalization primitive 137 - __sandboxProfile = 138 - let profiles = [ extraSandboxProfile ] ++ computedSandboxProfile ++ computedPropagatedSandboxProfile ++ [ propagatedSandboxProfile sandboxProfile ]; 139 - final = lib.concatStringsSep "\n" (lib.filter (x: x != "") (lib.unique profiles)); 140 - in final; 141 - __propagatedSandboxProfile = lib.unique (computedPropagatedSandboxProfile ++ [ propagatedSandboxProfile ]); 142 - __impureHostDeps = computedImpureHostDeps ++ computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps ++ __impureHostDeps ++ __extraImpureHostDeps ++ [ 143 - "/dev/zero" 144 - "/dev/random" 145 - "/dev/urandom" 146 - "/bin/sh" 147 - ]; 148 - __propagatedImpureHostDeps = computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps; 149 - } // (if outputs' != [ "out" ] then { 150 - outputs = outputs'; 151 - } else { })); 152 - 153 - # The meta attribute is passed in the resulting attribute set, 154 - # but it's not part of the actual derivation, i.e., it's not 155 - # passed to the builder and is not a dependency. But since we 156 - # include it in the result, it *is* available to nix-env for queries. 157 - meta = { } 158 - # If the packager hasn't specified `outputsToInstall`, choose a default, 159 - # which is the name of `p.bin or p.out or p`; 160 - # if he has specified it, it will be overridden below in `// meta`. 161 - # Note: This default probably shouldn't be globally configurable. 162 - # Services and users should specify outputs explicitly, 163 - # unless they are comfortable with this default. 164 - // { outputsToInstall = 165 - let 166 - outs = outputs'; # the value passed to derivation primitive 167 - hasOutput = out: builtins.elem out outs; 168 - in [( lib.findFirst hasOutput null (["bin" "out"] ++ outs) )]; 169 - } 170 - // attrs.meta or {} 171 - # Fill `meta.position` to identify the source location of the package. 172 - // lib.optionalAttrs (pos != null) 173 - { position = pos.file + ":" + toString pos.line; } 174 - ; 175 - 176 - in 177 - 178 - lib.addPassthru 179 - (derivation (import ./check-meta.nix 180 - { 181 - inherit lib config meta derivationArg; 182 - mkDerivationArg = attrs; 183 - inherit system; # TODO: cross-compilation? 184 - })) 185 - ( { 186 - overrideAttrs = f: mkDerivation (attrs // (f attrs)); 187 - inherit meta passthru; 188 - } // 189 - # Pass through extra attributes that are not inputs, but 190 - # should be made available to Nix expressions using the 191 - # derivation (e.g., in assertions). 192 - passthru); 193 - 194 47 # The stdenv that we are producing. 195 - result = 48 + stdenv = 196 49 derivation ( 197 50 (if isNull allowedRequisites then {} else { allowedRequisites = allowedRequisites ++ defaultNativeBuildInputs; }) // 198 51 { ··· 206 59 207 60 inherit preHook initialPath shell defaultNativeBuildInputs; 208 61 } 209 - // ifDarwin { 62 + // lib.optionalAttrs hostPlatform.isDarwin { 210 63 __sandboxProfile = stdenvSandboxProfile; 211 64 __impureHostDeps = __stdenvImpureHostDeps; 212 65 }) ··· 228 81 # Whether we should run paxctl to pax-mark binaries. 229 82 needsPax = isLinux; 230 83 231 - inherit mkDerivation; 84 + inherit (import ./make-derivation.nix { 85 + inherit lib config stdenv; 86 + # TODO(@Ericson2314): Remove 87 + inherit 88 + extraBuildInputs 89 + __extraImpureHostDeps 90 + extraSandboxProfile 91 + hostPlatform targetPlatform; 92 + }) mkDerivation; 232 93 233 94 # For convenience, bring in the library functions in lib/ so 234 95 # packages don't have to do that themselves. ··· 247 108 # like curl = if stdenv ? curl then stdenv.curl else ...). 248 109 // extraAttrs; 249 110 250 - in result) 111 + in stdenv)
+155
pkgs/stdenv/generic/make-derivation.nix
··· 1 + { lib, config, stdenv 2 + 3 + # TODO(@Ericson2314): get off stdenv 4 + , extraBuildInputs 5 + , __extraImpureHostDeps 6 + , extraSandboxProfile 7 + , hostPlatform, targetPlatform 8 + }: 9 + 10 + rec { 11 + # `mkDerivation` wraps the builtin `derivation` function to 12 + # produce derivations that use this stdenv and its shell. 13 + # 14 + # See also: 15 + # 16 + # * https://nixos.org/nixpkgs/manual/#sec-using-stdenv 17 + # Details on how to use this mkDerivation function 18 + # 19 + # * https://nixos.org/nix/manual/#ssec-derivation 20 + # Explanation about derivations in general 21 + mkDerivation = 22 + { nativeBuildInputs ? [] 23 + , buildInputs ? [] 24 + 25 + , propagatedNativeBuildInputs ? [] 26 + , propagatedBuildInputs ? [] 27 + 28 + , crossConfig ? null 29 + , meta ? {} 30 + , passthru ? {} 31 + , pos ? # position used in error messages and for meta.position 32 + (if attrs.meta.description or null != null 33 + then builtins.unsafeGetAttrPos "description" attrs.meta 34 + else builtins.unsafeGetAttrPos "name" attrs) 35 + , separateDebugInfo ? false 36 + , outputs ? [ "out" ] 37 + , __impureHostDeps ? [] 38 + , __propagatedImpureHostDeps ? [] 39 + , sandboxProfile ? "" 40 + , propagatedSandboxProfile ? "" 41 + , ... } @ attrs: 42 + let 43 + dependencies = [ 44 + (map (drv: drv.nativeDrv or drv) nativeBuildInputs) 45 + (map (drv: drv.crossDrv or drv) buildInputs) 46 + ]; 47 + propagatedDependencies = [ 48 + (map (drv: drv.nativeDrv or drv) propagatedNativeBuildInputs) 49 + (map (drv: drv.crossDrv or drv) propagatedBuildInputs) 50 + ]; 51 + in let 52 + 53 + outputs' = 54 + outputs ++ 55 + (if separateDebugInfo then assert targetPlatform.isLinux; [ "debug" ] else []); 56 + 57 + dependencies' = let 58 + justMap = map lib.chooseDevOutputs dependencies; 59 + nativeBuildInputs = lib.elemAt justMap 0 60 + ++ lib.optional targetPlatform.isWindows ../../build-support/setup-hooks/win-dll-link.sh; 61 + buildInputs = lib.elemAt justMap 1 62 + # TODO(@Ericson2314): Should instead also be appended to `nativeBuildInputs`. 63 + ++ lib.optional separateDebugInfo ../../build-support/setup-hooks/separate-debug-info.sh; 64 + in [ nativeBuildInputs buildInputs ]; 65 + 66 + propagatedDependencies' = map lib.chooseDevOutputs propagatedDependencies; 67 + 68 + derivationArg = 69 + (removeAttrs attrs 70 + ["meta" "passthru" "crossAttrs" "pos" 71 + "__impureHostDeps" "__propagatedImpureHostDeps" 72 + "sandboxProfile" "propagatedSandboxProfile"]) 73 + // (let 74 + # TODO(@Ericson2314): Reversing of dep lists is just temporary to avoid Darwin mass rebuild. 75 + computedSandboxProfile = 76 + lib.concatMap (input: input.__propagatedSandboxProfile or []) (extraBuildInputs ++ lib.concatLists (lib.reverseList dependencies')); 77 + computedPropagatedSandboxProfile = 78 + lib.concatMap (input: input.__propagatedSandboxProfile or []) (lib.concatLists (lib.reverseList propagatedDependencies')); 79 + computedImpureHostDeps = 80 + lib.unique (lib.concatMap (input: input.__propagatedImpureHostDeps or []) (extraBuildInputs ++ lib.concatLists (lib.reverseList dependencies'))); 81 + computedPropagatedImpureHostDeps = 82 + lib.unique (lib.concatMap (input: input.__propagatedImpureHostDeps or []) (lib.concatLists (lib.reverseList propagatedDependencies'))); 83 + in 84 + { 85 + builder = attrs.realBuilder or stdenv.shell; 86 + args = attrs.args or ["-e" (attrs.builder or ./default-builder.sh)]; 87 + inherit stdenv; 88 + system = stdenv.system; # TODO(@Ericson2314): be correct about cross compilation 89 + userHook = config.stdenv.userHook or null; 90 + __ignoreNulls = true; 91 + 92 + nativeBuildInputs = lib.elemAt dependencies' 0; 93 + buildInputs = lib.elemAt dependencies' 1; 94 + 95 + propagatedNativeBuildInputs = lib.elemAt propagatedDependencies' 0; 96 + propagatedBuildInputs = lib.elemAt propagatedDependencies' 1; 97 + } // lib.optionalAttrs (hostPlatform.isDarwin) { 98 + # TODO: remove lib.unique once nix has a list canonicalization primitive 99 + __sandboxProfile = 100 + let profiles = [ extraSandboxProfile ] ++ computedSandboxProfile ++ computedPropagatedSandboxProfile ++ [ propagatedSandboxProfile sandboxProfile ]; 101 + final = lib.concatStringsSep "\n" (lib.filter (x: x != "") (lib.unique profiles)); 102 + in final; 103 + __propagatedSandboxProfile = lib.unique (computedPropagatedSandboxProfile ++ [ propagatedSandboxProfile ]); 104 + __impureHostDeps = computedImpureHostDeps ++ computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps ++ __impureHostDeps ++ __extraImpureHostDeps ++ [ 105 + "/dev/zero" 106 + "/dev/random" 107 + "/dev/urandom" 108 + "/bin/sh" 109 + ]; 110 + __propagatedImpureHostDeps = computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps; 111 + } // (if outputs' != [ "out" ] then { 112 + outputs = outputs'; 113 + } else { })); 114 + 115 + # The meta attribute is passed in the resulting attribute set, 116 + # but it's not part of the actual derivation, i.e., it's not 117 + # passed to the builder and is not a dependency. But since we 118 + # include it in the result, it *is* available to nix-env for queries. 119 + meta = { } 120 + # If the packager hasn't specified `outputsToInstall`, choose a default, 121 + # which is the name of `p.bin or p.out or p`; 122 + # if he has specified it, it will be overridden below in `// meta`. 123 + # Note: This default probably shouldn't be globally configurable. 124 + # Services and users should specify outputs explicitly, 125 + # unless they are comfortable with this default. 126 + // { outputsToInstall = 127 + let 128 + outs = outputs'; # the value passed to derivation primitive 129 + hasOutput = out: builtins.elem out outs; 130 + in [( lib.findFirst hasOutput null (["bin" "out"] ++ outs) )]; 131 + } 132 + // attrs.meta or {} 133 + # Fill `meta.position` to identify the source location of the package. 134 + // lib.optionalAttrs (pos != null) 135 + { position = pos.file + ":" + toString pos.line; } 136 + ; 137 + 138 + in 139 + 140 + lib.addPassthru 141 + (derivation (import ./check-meta.nix 142 + { 143 + inherit lib config meta derivationArg; 144 + mkDerivationArg = attrs; 145 + inherit (stdenv) system; # TODO: cross-compilation? 146 + })) 147 + ( { 148 + overrideAttrs = f: mkDerivation (attrs // (f attrs)); 149 + inherit meta passthru; 150 + } // 151 + # Pass through extra attributes that are not inputs, but 152 + # should be made available to Nix expressions using the 153 + # derivation (e.g., in assertions). 154 + passthru); 155 + }