From 08ae837a1aa97f2b7245dc38a057b208530e6a3c Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Thu, 6 Oct 2022 21:26:15 +0300 Subject: [PATCH] fix: add wrapPureTranslator to functions.translators --- src/default.nix | 12 +++- src/lib.nix | 2 + src/modules/framework.nix | 9 +-- .../functions.translators/implementation.nix | 57 +++++++++++++++++++ src/modules/top-level.nix | 19 +++++-- 5 files changed, 86 insertions(+), 13 deletions(-) diff --git a/src/default.nix b/src/default.nix index a93e3d59..25268421 100644 --- a/src/default.nix +++ b/src/default.nix @@ -51,7 +51,17 @@ in let }; framework = import ./modules/framework.nix { - inherit apps lib dlib pkgs utils externals externalSources; + inherit + apps + lib + dlib + pkgs + utils + externals + externalSources + dream2nixWithExternals + ; + dream2nixConfigFile = configFile; dream2nixConfig = config; }; diff --git a/src/lib.nix b/src/lib.nix index 2a995710..98898c32 100644 --- a/src/lib.nix +++ b/src/lib.nix @@ -92,10 +92,12 @@ framework = import ./modules/framework.nix { inherit lib dlib externalSources; dream2nixConfig = config; + dream2nixConfigFile = l.toFile "dream2nix-config.json" (l.toJSON config); apps = throw "apps is not available before nixpkgs is imported"; pkgs = throw "pkgs is not available before nixpkgs is imported"; utils = throw "utils is not available before nixpkgs is imported"; externals = throw "externals is not available before nixpkgs is imported"; + dream2nixWithExternals = throw "not available before nixpkgs is imported"; }; initD2N = initDream2nix config; diff --git a/src/modules/framework.nix b/src/modules/framework.nix index 15cac543..5466863b 100644 --- a/src/modules/framework.nix +++ b/src/modules/framework.nix @@ -1,12 +1,7 @@ { - dream2nixConfig, - pkgs, - dlib, - externals, - externalSources, lib, - utils, - apps, + dream2nixConfig, + ... } @ args: let topLevel = import ./top-level.nix args; evaledModules = lib.evalModules { diff --git a/src/modules/functions.translators/implementation.nix b/src/modules/functions.translators/implementation.nix index 466cddf6..d4128f50 100644 --- a/src/modules/functions.translators/implementation.nix +++ b/src/modules/functions.translators/implementation.nix @@ -11,10 +11,67 @@ else def.default or null ) extraArgsDef; + + # adds a translateBin to a pure translator + wrapPureTranslator = { + subsystem, + name, + }: let + inherit + (config) + utils + pkgs + dream2nixWithExternals + configFile + ; + bin = + utils.writePureShellScript + (with pkgs; [ + coreutils + jq + nix + python3 + ]) + '' + jsonInputFile=$(realpath $1) + outputFile=$(realpath -m $(jq '.outputFile' -c -r $jsonInputFile)) + pushd $TMPDIR + nix eval \ + --option experimental-features "nix-command flakes"\ + --show-trace --impure --raw --expr " + let + dream2nix = import ${dream2nixWithExternals} { + config = ${configFile}; + }; + translatorArgs = + (builtins.fromJSON + (builtins.unsafeDiscardStringContext (builtins.readFile '''$1'''))); + dreamLock' = + dream2nix.framework.translatorsBySubsystem.${subsystem}.${name}.translate + translatorArgs; + # simpleTranslate2 puts dream-lock in result + dreamLock = dreamLock'.result or dreamLock'; + in + dream2nix.utils.dreamLock.toJSON + # don't use nix to detect cycles, this will be more efficient in python + (dreamLock // { + _generic = builtins.removeAttrs dreamLock._generic [ \"cyclicDependencies\" ]; + }) + " | python3 ${../apps/cli/format-dream-lock.py} > out + tmpOut=$(realpath out) + popd + mkdir -p $(dirname $outputFile) + cp $tmpOut $outputFile + ''; + in + bin.overrideAttrs (old: { + name = "translator-${subsystem}-pure-${name}"; + }); in { functions.translators = { inherit makeTranslatorDefaultArgs + wrapPureTranslator ; }; } diff --git a/src/modules/top-level.nix b/src/modules/top-level.nix index 1c59d137..9656334c 100644 --- a/src/modules/top-level.nix +++ b/src/modules/top-level.nix @@ -7,7 +7,9 @@ pkgs, utils, dream2nixConfig, -}: let + dream2nixConfigFile, + dream2nixWithExternals, +} @ args: let t = lib.types; in { imports = [ @@ -47,9 +49,16 @@ in { dream2nixConfig = lib.mkOption { type = t.raw; }; + dream2nixWithExternals = lib.mkOption { + type = t.path; + }; + dream2nixConfigFile = lib.mkOption { + type = t.path; + }; }; - config = { - inherit apps dlib externals externalSources pkgs utils dream2nixConfig; - lib = lib // builtins; - }; + config = + args + // { + lib = args.lib // builtins; + }; }