1
0
Fork 0
nixredo/builder.nix

59 lines
2.2 KiB
Nix

buildArgs@{ lib ? import ./lib.nix {}, nixpkgs ? <nixpkgs>, pkgs ? import nixpkgs {}, root, filter ? [] }:
let
inherit (lib) searchPath whichdo d1 d2;
resolve = path: let
builder = (whichdo path);
in if builder == "" then throw "No rule to build ${path}"
else runBuilder (d1 path builder) (d2 path builder) builder;
runBuilder = d1: d2: builder:
with pkgs.lib;
with builtins;
let
# Funktionalise pkgs; our main entry point to resolve default*nix build scripts.
funcPkgs = { __functor = self: arg: resolve arg; } // pkgs;
_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 {
src = filterSource
(path: type: type != "directory" || (baseNameOf path != ".git" && (all (x: trace x path != x) filter)))
(/. + root);
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;
args = { stdenv = pkgs.stdenvNoCC; } // (if isString __builder
then { builder = __builder; }
else __builder);
drv = (args.deriver or args.stdenv.mkDerivation) ({
name = pkgs.lib.strings.sanitizeDerivationName d1;
buildCommand = args.builder;
passAsFile = [ "buildCommand" ];
preferLocalBuild = true;
allowSubstitutes = false;
outputs = [ "out" ];
inherit d1 d2;
} // (removeAttrs args [ "builder" "system" "deriver" "stdenv" "outputs" ]));
in drv; # myself
in resolve