From 3e1a40df7598cd24d091ef7fb0933b104aa05157 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Sat, 15 Aug 2020 23:11:20 +0200 Subject: [PATCH] mkDerivation: Introduce .inputDerivation for shell.nix build convenience This introduces the .inputDerivation attribute on all derivations created with mkDerivation. This is another derivation that can always build successfully and whose runtime dependencies are the build time dependencies of the original derivation. This allows easy building and distributing of all derivations needed to enter a nix-shell with nix-build shell.nix -A inputDerivation --- pkgs/stdenv/generic/make-derivation.nix | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index 993ae68e9e81..06d6cc2384a6 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -337,6 +337,32 @@ in rec { validity.handled ({ overrideAttrs = f: mkDerivation (attrs // (f attrs)); + + # A derivation that always builds successfully and whose runtime + # dependencies are the original derivations build time dependencies + # This allows easy building and distributing of all derivations + # needed to enter a nix-shell with + # nix-build shell.nix -A inputDerivation + inputDerivation = derivation (derivationArg // { + # Add a name in case the original drv didn't have one + name = derivationArg.name or "inputDerivation"; + # This always only has one output + outputs = [ "out" ]; + + # Propagate the original builder and arguments, since we override + # them and they might contain references to build inputs + _derivation_original_builder = derivationArg.builder; + _derivation_original_args = derivationArg.args; + + builder = stdenv.shell; + # The bash builtin `export` dumps all current environment variables, + # which is where all build input references end up (e.g. $PATH for + # binaries). By writing this to $out, Nix can find and register + # them as runtime dependencies (since Nix greps for store paths + # through $out to find them) + args = [ "-c" "export > $out" ]; + }); + inherit meta passthru; } // # Pass through extra attributes that are not inputs, but