|
| 1 | +lib: |
| 2 | + |
| 3 | +originalArgs: |
| 4 | + |
| 5 | +# A special kind of derivation that is only meant to be consumed by the |
| 6 | +# nix-shell. |
| 7 | +{ name ? if originalArgs ? name then "${originalArgs.name}-dev-shell" else "nix-shell" |
| 8 | +, # a list of packages to add to the shell environment |
| 9 | + packages ? [ ] |
| 10 | +, # propagate all the inputs from the given derivations |
| 11 | + inputsFrom ? [ ] |
| 12 | +, buildInputs ? [ ] |
| 13 | +, nativeBuildInputs ? [ ] |
| 14 | +, propagatedBuildInputs ? [ ] |
| 15 | +, propagatedNativeBuildInputs ? [ ] |
| 16 | +, ... |
| 17 | +}@attrs: |
| 18 | +let |
| 19 | + mergeInputs = name: |
| 20 | + (originalArgs.${name} or [ ]) ++ |
| 21 | + (attrs.${name} or [ ]) ++ |
| 22 | + # 1. get all `{build,nativeBuild,...}Inputs` from the elements of `inputsFrom` |
| 23 | + # 2. since that is a list of lists, `flatten` that into a regular list |
| 24 | + # 3. filter out of the result everything that's in `inputsFrom` itself |
| 25 | + # this leaves actual dependencies of the derivations in `inputsFrom`, but never the derivations themselves |
| 26 | + (lib.subtractLists inputsFrom (lib.flatten (lib.catAttrs name inputsFrom))); |
| 27 | + |
| 28 | + rest = builtins.removeAttrs attrs [ |
| 29 | + "name" |
| 30 | + "packages" |
| 31 | + "inputsFrom" |
| 32 | + "buildInputs" |
| 33 | + "nativeBuildInputs" |
| 34 | + "propagatedBuildInputs" |
| 35 | + "propagatedNativeBuildInputs" |
| 36 | + "shellHook" |
| 37 | + ]; |
| 38 | +in { |
| 39 | + inherit name; |
| 40 | + |
| 41 | + buildInputs = mergeInputs "buildInputs"; |
| 42 | + nativeBuildInputs = packages ++ (mergeInputs "nativeBuildInputs"); |
| 43 | + propagatedBuildInputs = mergeInputs "propagatedBuildInputs"; |
| 44 | + propagatedNativeBuildInputs = mergeInputs "propagatedNativeBuildInputs"; |
| 45 | + |
| 46 | + shellHook = lib.concatStringsSep "\n" (lib.catAttrs "shellHook" |
| 47 | + (lib.reverseList inputsFrom ++ [ attrs ])); |
| 48 | + |
| 49 | + phases = [ "buildPhase" ]; |
| 50 | + |
| 51 | + buildPhase = '' |
| 52 | + { echo "------------------------------------------------------------"; |
| 53 | + echo " WARNING: the existence of this path is not guaranteed."; |
| 54 | + echo " It is an internal implementation detail for pkgs.mkShell."; |
| 55 | + echo "------------------------------------------------------------"; |
| 56 | + echo; |
| 57 | + # Record all build inputs as runtime dependencies |
| 58 | + export; |
| 59 | + } >> "$out" |
| 60 | + ''; |
| 61 | + |
| 62 | + preferLocalBuild = true; |
| 63 | +} // rest |
| 64 | + |
0 commit comments