Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{
2 lib,
3 stdenv,
4 buildEnv,
5 devShellTools,
6}:
7
8# A special kind of derivation that is only meant to be consumed by the
9# nix-shell.
10{
11 name ? "nix-shell",
12 # a list of packages to add to the shell environment
13 packages ? [ ],
14 # propagate all the inputs from the given derivations
15 inputsFrom ? [ ],
16 buildInputs ? [ ],
17 nativeBuildInputs ? [ ],
18 propagatedBuildInputs ? [ ],
19 propagatedNativeBuildInputs ? [ ],
20 passthru ? { },
21 ...
22}@attrs:
23let
24 mergeInputs =
25 name:
26 (attrs.${name} or [ ])
27 ++
28 # 1. get all `{build,nativeBuild,...}Inputs` from the elements of `inputsFrom`
29 # 2. since that is a list of lists, `flatten` that into a regular list
30 # 3. filter out of the result everything that's in `inputsFrom` itself
31 # this leaves actual dependencies of the derivations in `inputsFrom`, but never the derivations themselves
32 (lib.subtractLists inputsFrom (lib.flatten (lib.catAttrs name inputsFrom)));
33
34 rest = builtins.removeAttrs attrs [
35 "name"
36 "packages"
37 "inputsFrom"
38 "buildInputs"
39 "nativeBuildInputs"
40 "propagatedBuildInputs"
41 "propagatedNativeBuildInputs"
42 "shellHook"
43 ];
44in
45
46stdenv.mkDerivation (
47 finalAttrs:
48 {
49 inherit name;
50
51 buildInputs = mergeInputs "buildInputs";
52 nativeBuildInputs = packages ++ (mergeInputs "nativeBuildInputs");
53 propagatedBuildInputs = mergeInputs "propagatedBuildInputs";
54 propagatedNativeBuildInputs = mergeInputs "propagatedNativeBuildInputs";
55
56 shellHook = lib.concatStringsSep "\n" (
57 lib.catAttrs "shellHook" (lib.reverseList inputsFrom ++ [ attrs ])
58 );
59
60 phases = [ "buildPhase" ];
61
62 buildPhase = ''
63 { echo "------------------------------------------------------------";
64 echo " WARNING: the existence of this path is not guaranteed.";
65 echo " It is an internal implementation detail for pkgs.mkShell.";
66 echo "------------------------------------------------------------";
67 echo;
68 # Record all build inputs as runtime dependencies
69 export;
70 } >> "$out"
71 '';
72
73 preferLocalBuild = true;
74
75 passthru = {
76 devShell =
77 let
78 inherit (finalAttrs.finalPackage) drvAttrs;
79 in
80 devShellTools.buildShellEnv {
81 inherit drvAttrs;
82
83 # The default prefix is "build shell", but this shell is not derived
84 # directly from a derivation, so we set a more generic title.
85 promptPrefix = "nix";
86
87 # Change the default name
88 promptName =
89 if
90 # name was passed originally
91 attrs ? name
92 # or with overrideAttrs
93 || drvAttrs.name != "nix-shell"
94 then
95 null
96 else
97 "mkShell";
98 };
99 }
100 // passthru;
101 }
102 // rest
103)