Commit 96c6dc8c authored by Profpatsch's avatar Profpatsch

containers/helpers/nix-json-trans: allow nix arguments

It is now possible to pass arguments to the json transformation function.
This means e.g. passing build paths from outside works without having to go
through shell variables (and all the ills that brings).
Every transformation function takes one extra argument, an attrset specifying
all nix inputs (and possible default values).
parent c8b233f4
{ lib, pkgs, stdenv }:
{ libPath ? <nixpkgs/lib> }:
{ lib, pkgs, stdenv, libPath ? <nixpkgs/lib> }:
# TODO: Do we need all of lib?
# TODO: Is there a better way to pass stuff to nix?!
# TODO: better argument handling
let
transGeneric = nixCommand: ''
#!${stdenv.shell}
ARGS="$(realpath "$1")"
DIR=$(mktemp -d)
cat >$DIR/script-input.nix <<EOF
with import ${libPath};
$(cat "$1")
$(cat "$2")
EOF
JSON="$(realpath "$2")"
JSON="$(realpath "$3")"
cat >$DIR/script.nix <<EOF
let
args = import $ARGS;
f = import $DIR/script-input.nix;
json = builtins.fromJSON (builtins.readFile $JSON);
in
assert builtins.typeOf $JSON == "path";
assert builtins.typeOf args == "set";
assert builtins.typeOf f == "lambda";
f json
assert builtins.typeOf $JSON == "path";
f args json
EOF
source ${pkgs.setupLocalNixStore}
......@@ -53,8 +58,9 @@ let
jsonTests =
let
eq = nixFile: jsonFile: resultFile: ''
echo "{}" > ./args.nix
${pkgs.diffutils}/bin/diff \
<(${json2json} ${nixFile} ${jsonFile}) \
<(${json2json} ./args.nix ${nixFile} ${jsonFile}) \
${resultFile}
'';
in {
......@@ -64,28 +70,44 @@ let
json = mkjson {
foo = [ 1 2 3 null "bar" ];
};
idScr = pkgs.writeText "id-converter" "id";
idScr = pkgs.writeText "id-converter" "{}: id";
in eq idScr json json;
replaceAttrsJson =
eq (pkgs.writeText "maybe-its-neuer"
''mapAttrs (_: _: "manuel neuer")'')
''{}: mapAttrs (_: _: "manuel neuer")'')
(mkjson { foo = "richard stallman"; bar = "linus torvalds"; })
(mkjson { foo = "manuel neuer"; bar = "manuel neuer"; });
};
outTests = {
buildEchoScriptFromJsonString =
let
nix = pkgs.writeText "echo" ''
str: str
'';
in ''
${json2string} ${nix} ${mkjson "hello!\nworld!"} \
buildEchoScriptFromJsonString = ''
echo "{}" > ./args.nix
echo "{}: str: str" > ./echo.nix
${json2string} ./args.nix ./echo.nix ${mkjson "hello!\nworld!"} \
> out
grep "hello!" <out
grep "world!" <out
'';
passingArguments =
let
args = pkgs.writeText "args.nix" ''
{ shell = "${stdenv.shell}"; }
'';
echoshell = pkgs.writeText "echoshell.nix" ''
{ shell }: _: '''
#!''${shell}
echo echoshell
'''
'';
in ''
touch empty.json
${json2string} ${args} ${echoshell} empty.json \
| ${stdenv.shell} -s \
| grep echoshell
'';
};
in {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment