From adc5c9b83df203c9e425efe00f9a788ed3554c2d Mon Sep 17 00:00:00 2001 From: zimbatm Date: Wed, 20 Dec 2017 23:42:07 +0000 Subject: [PATCH] mkShell: add builder (#30975) --- doc/default.nix | 4 +++ doc/shell.md | 20 +++++++++++ pkgs/build-support/mkshell/default.nix | 46 ++++++++++++++++++++++++++ pkgs/top-level/all-packages.nix | 2 ++ 4 files changed, 72 insertions(+) create mode 100644 doc/shell.md create mode 100644 pkgs/build-support/mkshell/default.nix diff --git a/doc/default.nix b/doc/default.nix index e3d7ef2ef9d6..60c613878c72 100644 --- a/doc/default.nix +++ b/doc/default.nix @@ -49,6 +49,10 @@ pkgs.stdenv.mkDerivation { outputFile = "introduction.xml"; useChapters = true; } + + toDocbook { + inputFile = ./shell.md; + outputFile = "shell.xml"; + } + toDocbook { inputFile = ./languages-frameworks/python.md; outputFile = "./languages-frameworks/python.xml"; diff --git a/doc/shell.md b/doc/shell.md new file mode 100644 index 000000000000..a4f999695cbc --- /dev/null +++ b/doc/shell.md @@ -0,0 +1,20 @@ +--- +title: stdenv.mkShell +author: zimbatm +date: 2017-10-30 +--- + +stdenv.mkShell is a special kind of derivation that is only useful when using +it combined with nix-shell. It will in fact fail to instantiate when invoked +with nix-build. + +## Usage + +```nix +{ pkgs ? import {} }: +pkgs.mkShell { + # this will make all the build inputs from hello and gnutar available to the shell environment + inputsFrom = with pkgs; [ hello gnutar ]; + buildInputs = [ pkgs.gnumake ]; +} +``` diff --git a/pkgs/build-support/mkshell/default.nix b/pkgs/build-support/mkshell/default.nix new file mode 100644 index 000000000000..a98b4affacba --- /dev/null +++ b/pkgs/build-support/mkshell/default.nix @@ -0,0 +1,46 @@ +{ lib, stdenv }: + +# A special kind of derivation that is only meant to be consumed by the +# nix-shell. +{ + inputsFrom ? [], # a list of derivations whose inputs will be made available to the environment + buildInputs ? [], + nativeBuildInputs ? [], + propagatedBuildInputs ? [], + propagatedNativeBuildInputs ? [], + ... +}@attrs: +let + mergeInputs = name: + let + op = item: sum: sum ++ item."${name}" or []; + nul = []; + list = [attrs] ++ inputsFrom; + in + lib.foldr op nul list; + + rest = builtins.removeAttrs attrs [ + "inputsFrom" + "buildInputs" + "nativeBuildInputs" + "propagatedBuildInputs" + "propagatedNativeBuildInputs" + ]; +in + +stdenv.mkDerivation ({ + name = "nix-shell"; + phases = ["nobuildPhase"]; + + buildInputs = mergeInputs "buildInputs"; + nativeBuildInputs = mergeInputs "nativeBuildInputs"; + propagatedBuildInputs = mergeInputs "propagatedBuildInputs"; + propagatedNativeBuildInputs = mergeInputs "propagatedNativeBuildInputs"; + + nobuildPhase = '' + echo + echo "This derivation is not meant to be built, aborting"; + echo + exit 1 + ''; +} // rest) diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index bf003b959a2b..974acdfa047d 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -311,6 +311,8 @@ with pkgs; inherit kernel rootModules allowMissing; }; + mkShell = callPackage ../build-supports/mkshell { }; + nixBufferBuilders = import ../build-support/emacs/buffer.nix { inherit (pkgs) lib writeText; inherit (emacsPackagesNg) inherit-local; }; pathsFromGraph = ../build-support/kernel/paths-from-graph.pl;