Skip to content

Commit 9c98692

Browse files
committed
mkDerivation: add toDevShell
1 parent 7463086 commit 9c98692

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

generic/make-derivation.nix

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,12 @@ extendDerivation
635635
disallowedRequisites = [ ];
636636
});
637637

638+
toDevShell = let
639+
originalArgs = deleteFixedOutputRelatedAttrs derivationArg;
640+
toShell = import ./to-dev-shell.nix lib originalArgs;
641+
shellFunc = f: if builtins.isFunction f then f stdenv else f;
642+
in f: derivation (toShell (shellFunc f));
643+
638644
inherit passthru overrideAttrs;
639645
inherit meta;
640646
} //

generic/to-dev-shell.nix

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)