From 696271700a1308c4dff4a9c30008b4303b417d76 Mon Sep 17 00:00:00 2001 From: Michael Raitza Date: Wed, 17 Feb 2021 09:12:23 +0100 Subject: [PATCH] handle simultaneous writes to stdout and $3 --- builder.nix | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/builder.nix b/builder.nix index 1a2dd90..826271a 100644 --- a/builder.nix +++ b/builder.nix @@ -14,15 +14,27 @@ let # Funktionalise pkgs; our main entry point to resolve default*nix build scripts. funcPkgs = { __functor = self: arg: resolve arg; } // pkgs; - _builder = if (tryEval (let - # Needs to be done as nix chokes on empty files rn. - content = readFile builder; - in assert content != "" && (! (hasPrefix "#!" content)); import builder)).success - then import builder - else '' - #!/bin/sh - ${/. + builder} $d1 $d2 $out >$out - ''; + _builder = let + imported = (tryEval (let + # Needs to be done as nix chokes on empty files rn. + content = readFile builder; + in assert content != "" && (! (hasPrefix "#!" content)); import builder)); + in if imported.success + then imported.value + else { + builder = '' + #!/bin/sh + set -e + pout=$(mktemp -u -p $PWD) + ${/. + builder} $d1 $d2 $out >$pout + if [ -e $out -a -s $pout ]; then + printf "Error: %s wrote to stdout and created \$3\n" "$d1" >&2 + exit 207 + elif [ -s $pout ]; then + mv $pout $out + fi + ''; + }; __builder = if isFunction _builder then _builder funcPkgs else _builder;