summary refs log tree commit diff
path: root/pkgs/misc
diff options
context:
space:
mode:
authorRickard Nilsson <rickynils@gmail.com>2013-09-13 16:48:54 +0200
committerRickard Nilsson <rickynils@gmail.com>2013-12-01 09:36:55 +0100
commit8685278f11f809af52710f165257fb5808018182 (patch)
treefeb1f1d1ebe5b0df2db5ca2f6062a7c244a4014d /pkgs/misc
parent214e92081f8748c508fbcd13d9b437368e17d839 (diff)
downloadnixlib-8685278f11f809af52710f165257fb5808018182.tar
nixlib-8685278f11f809af52710f165257fb5808018182.tar.gz
nixlib-8685278f11f809af52710f165257fb5808018182.tar.bz2
nixlib-8685278f11f809af52710f165257fb5808018182.tar.lz
nixlib-8685278f11f809af52710f165257fb5808018182.tar.xz
nixlib-8685278f11f809af52710f165257fb5808018182.tar.zst
nixlib-8685278f11f809af52710f165257fb5808018182.zip
Add nix-run and myEnvRun
This adds nix-run, which is a thin wrapper around nix-build.
nix-run calls nix-build, and then executes the resulting build.
If no executable artifact is built, nix-runs outputs an error
message.

myEnvRun calls myEnvFun and builds a script that directly runs
the load-env-* script.

Together, nix-run and myEnvRun allows you to set up an environment
that can be loaded in this way:

  envs.nix:
    {
      gcc = myEnvRun {
        name = "gcc";
        buildInputs = [ gcc ];
      };
    }

  $ nix-run -A gcc envs.nix

You end up directly in your environment without having to do
nix-env -i. You will always have a fresh environment and you
don't have to pollute you profile with a lot of env packages.
Diffstat (limited to 'pkgs/misc')
-rw-r--r--pkgs/misc/nix-run/default.nix72
1 files changed, 72 insertions, 0 deletions
diff --git a/pkgs/misc/nix-run/default.nix b/pkgs/misc/nix-run/default.nix
new file mode 100644
index 000000000000..ef83ec2a1f3d
--- /dev/null
+++ b/pkgs/misc/nix-run/default.nix
@@ -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;
+  };
+}