Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkgs/build-support/my-env-run/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ writeScript, bash, myEnvFun }: args:

let env = myEnvFun args; in writeScript "envrun-${args.name}" ''
#!${bash}/bin/bash
${env}/bin/load-env-${args.name}
''
72 changes: 72 additions & 0 deletions pkgs/misc/nix-run/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{ stdenv, bash, writeScript }:

let

nix-run = writeScript "nix-run" ''
#!${bash}/bin/bash

# Runs nix-build and executes the result
# All arguments before "--" are given to nix-build,
# and all arguments after "--" are given to the
# executed command. stdin is redirected to the executed
# command.

out=$(mktemp)
rm "$out"

# parse args into args1 and args2, separated by --
# args1 goes to nix-build, args2 goes to the built command
args1=("$@")
args2=()
for i in "''${!args1[@]}"; do
if [ "''${args1[$i]}" == "--" ]; then
args2=("''${args1[@]:$((i+1))}")
args1=("''${args1[@]:0:$((i))}")
break
fi
done

if nix-build -o "$out" "''${args1[@]}" >/dev/null; then
target=$(readlink -m "$out")
unlink "$out"
if test -f "$target" && test -x "$target"; then
exec "$target" "''${args2[@]}" <&0
else
echo "nix-run: No executable target produced by nix-build"
exit 1
fi
else
echo "nix-run: nix-build failed"
exit 1
fi
'';

in stdenv.mkDerivation {
name = "nix-run";
phases = [ "installPhase" ];
installPhase = ''
mkdir -p $out/bin
ln -s ${nix-run} $out/bin/nix-run
'';
meta = {
description = ''
Wrapper around nix-build that automatically executes the binary
produced by the provided Nix expression.
'';
longDescription = ''
nix-run invokes nix-build with any options given to it. It then
expects one executable file to be produced by nix-build. If this
is the case, that file is executed with any options that is given
to nix-run after a <literal>--</literal> option separator. If no
executable file is produced by nix-build, nix-run will exit with
an error. An example invocation of nix-run is <literal>nix-run -A
myattr mynix.nix -- -o opt</literal>. nix-run will then build the
attribute <literal>myattr</literal> from the Nix expression given
in the file <literal>mynix.nix</literal>. If a single executable
file is produced, that file is executed with the option
<literal>-o opt</literal>.
'';
maintainers = [ stdenv.lib.maintainers.rickynils ];
platforms = stdenv.lib.platforms.linux;
};
}
6 changes: 6 additions & 0 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10352,6 +10352,12 @@ let
inherit (stdenv) mkDerivation;
};

myEnvRun = import ../build-support/my-env-run {
inherit writeScript bash myEnvFun;
};

nix-run = callPackage ../misc/nix-run { };

# patoline requires a rather large ocaml compilation environment.
# this is why it is build as an environment and not just a normal package.
# remark : the emacs mode is also installed, but you have to adjust your load-path.
Expand Down