Commit c8b233f4 authored by Profpatsch's avatar Profpatsch

containers/helpers: add json2string to nix-json-trans

Instead of converting to json, this can use nix’ string interpolation
capabilities to generate a string from a json input.
parent 44d97967
...@@ -19,13 +19,15 @@ let ...@@ -19,13 +19,15 @@ let
nixpkgs.overlays = [(self: _: { nixpkgs.overlays = [(self: _: {
sangha = self.callPackage ../packages/sangha {}; sangha = self.callPackage ../packages/sangha {};
helpers = lib.mapAttrs (_: pkg: self.callPackage pkg {}) { helpers = (lib.mapAttrs (_: pkg: self.callPackage pkg {}) {
genUtf8Localedb = ./helpers/gen-utf8-localedb.nix; genUtf8Localedb = ./helpers/gen-utf8-localedb.nix;
postgresTemplate = ./helpers/postgres-template.nix; postgresTemplate = ./helpers/postgres-template.nix;
prependPrefix = ./helpers/prepend-prefix.nix; prependPrefix = ./helpers/prepend-prefix.nix;
debugDockerImage = ./helpers/debug-docker-image.nix; debugDockerImage = ./helpers/debug-docker-image.nix;
createStandaloneDockerImage = ./helpers/create-standalone-docker-image.nix; createStandaloneDockerImage = ./helpers/create-standalone-docker-image.nix;
nixJsonTrans = ./helpers/nix-json-trans.nix; }) // {
inherit (self.callPackage ./helpers/nix-json-trans.nix {})
json2json json2string;
}; };
})]; })];
}; };
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# TODO: Do we need all of lib? # TODO: Do we need all of lib?
let let
trans = pkgs.writeScript "nix-json-trans" '' transGeneric = nixCommand: ''
#!${stdenv.shell} #!${stdenv.shell}
DIR=$(mktemp -d) DIR=$(mktemp -d)
cat >$DIR/script-input.nix <<EOF cat >$DIR/script-input.nix <<EOF
...@@ -24,19 +24,39 @@ let ...@@ -24,19 +24,39 @@ let
EOF EOF
source ${pkgs.setupLocalNixStore} source ${pkgs.setupLocalNixStore}
${pkgs.nix}/bin/nix-instantiate \ ${nixCommand}
--eval --json --strict \
--show-trace \
$DIR/script.nix
''; '';
tests = json2json = pkgs.writeScript "nix-json-to-json"
(transGeneric ''
${pkgs.nix}/bin/nix eval \
--json \
--show-trace \
-f $DIR/script.nix \
""
'');
# TODO: Not sure if the produced output path is always
# reachable from the result? Maybe enable passing a valid
# path from outside?
json2string = pkgs.writeScript "nix-json-to-out"
(transGeneric ''
${pkgs.nix}/bin/nix eval \
--raw \
--show-trace \
-f $DIR/script.nix \
""
'');
mkjson = json: pkgs.writeText "test.json" (builtins.toJSON json);
jsonTests =
let let
call = nixFile: jsonFile: "${trans} ${nixFile} ${jsonFile}";
eq = nixFile: jsonFile: resultFile: '' eq = nixFile: jsonFile: resultFile: ''
${pkgs.diffutils}/bin/diff <(${call nixFile jsonFile}) ${resultFile} ${pkgs.diffutils}/bin/diff \
<(${json2json} ${nixFile} ${jsonFile}) \
${resultFile}
''; '';
mkjson = json: pkgs.writeText "test.json" (builtins.toJSON json);
in { in {
idJson = idJson =
...@@ -54,4 +74,21 @@ let ...@@ -54,4 +74,21 @@ let
(mkjson { foo = "manuel neuer"; bar = "manuel neuer"; }); (mkjson { foo = "manuel neuer"; bar = "manuel neuer"; });
}; };
in pkgs.withTests tests trans outTests = {
buildEchoScriptFromJsonString =
let
nix = pkgs.writeText "echo" ''
str: str
'';
in ''
${json2string} ${nix} ${mkjson "hello!\nworld!"} \
> out
grep "hello!" <out
grep "world!" <out
'';
};
in {
json2json = pkgs.withTests jsonTests json2json;
json2string = pkgs.withTests outTests json2string;
}
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