add translator: npmlock2nix

This commit is contained in:
DavHau 2021-09-14 03:00:29 +01:00
parent d83d76d5ef
commit a1a3e57e05
8 changed files with 149 additions and 26 deletions

View File

@ -15,9 +15,26 @@
"type": "indirect" "type": "indirect"
} }
}, },
"npmlock2nix": {
"flake": false,
"locked": {
"lastModified": 1631558099,
"narHash": "sha256-xguvgtrIQHPxpc4J6EhfBnYtQDGJpt6hK1dN7KEi8R4=",
"owner": "nix-community",
"repo": "npmlock2nix",
"rev": "33eb3300561d724da64a46ab8e9a05a9cfa9264b",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "npmlock2nix",
"type": "github"
}
},
"root": { "root": {
"inputs": { "inputs": {
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs",
"npmlock2nix": "npmlock2nix"
} }
} }
}, },

View File

@ -3,12 +3,13 @@
inputs = { inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable"; nixpkgs.url = "nixpkgs/nixos-unstable";
npmlock2nix = { url = "github:nix-community/npmlock2nix"; flake = false; };
}; };
outputs = { self, nixpkgs }: outputs = { self, nixpkgs, npmlock2nix }:
let let
lib = nixpkgs.lib; lib = nixpkgs.lib;
supportedSystems = [ "x86_64-linux" ]; supportedSystems = [ "x86_64-linux" ];
@ -19,7 +20,13 @@
overlays = [ self.overlay ]; overlays = [ self.overlay ];
}); });
dream2nixFor = forAllSystems (system: import ./src { pkgs = nixpkgsFor."${system}"; } ); dream2nixFor = forAllSystems (system: import ./src rec {
pkgs = nixpkgsFor."${system}";
externalSources = pkgs.runCommand "dream2nix-imported" {} ''
mkdir -p $out/npmlock2nix
cp ${npmlock2nix}/{internal.nix,LICENSE} $out/npmlock2nix/
'';
});
in in
{ {

View File

@ -49,6 +49,10 @@ def translate(args):
outputFile=output, outputFile=output,
) )
# remove output files if exists
if os.path.exists(output):
os.remove(output)
# dump translator arguments to json file and execute translator # dump translator arguments to json file and execute translator
with tempfile.NamedTemporaryFile("w") as inputJsonFile: with tempfile.NamedTemporaryFile("w") as inputJsonFile:
json.dump(translatorInput, inputJsonFile, indent=2) json.dump(translatorInput, inputJsonFile, indent=2)

View File

@ -1,5 +1,7 @@
{ {
pkgs, pkgs,
externalSources,
location, location,
translators, translators,
}: }:
@ -11,6 +13,8 @@ in
# the unified translator cli # the unified translator cli
cli = callPackage ({ python3, writeScript, ... }: cli = callPackage ({ python3, writeScript, ... }:
writeScript "cli" '' writeScript "cli" ''
export d2nExternalSources=${externalSources}
translatorsJsonFile=${translators.translatorsJsonFile} \ translatorsJsonFile=${translators.translatorsJsonFile} \
dream2nixSrc=${../.} \ dream2nixSrc=${../.} \
${python3}/bin/python ${./cli.py} "$@" ${python3}/bin/python ${./cli.py} "$@"
@ -35,6 +39,8 @@ in
fi fi
cp -r ${location}/* $target/ cp -r ${location}/* $target/
mkdir $target/external
cp -r ${externalSources}/* $target/external/
chmod -R +w $target chmod -R +w $target
'' ''
) {}; ) {};

View File

@ -1,20 +1,29 @@
{ {
pkgs ? import <nixpkgs> {}, pkgs ? import <nixpkgs> {},
externalSources ?
if builtins.getEnv "d2nExternalSources" != "" then
builtins.getEnv "d2nExternalSources"
else
./external,
}: }:
let let
callPackage = pkgs.callPackage; callPackage = pkgs.callPackage;
externals = {
npmlock2nix = callPackage "${externalSources}/npmlock2nix/internal.nix" {};
};
in in
rec { rec {
apps = callPackage ./apps { inherit location translators; }; apps = callPackage ./apps { inherit externalSources location translators; };
builders = callPackage ./builders {}; builders = callPackage ./builders {};
fetchers = callPackage ./fetchers {}; fetchers = callPackage ./fetchers {};
translators = callPackage ./translators {}; translators = callPackage ./translators { inherit externalSources externals location; };
# the location of the dream2nix framework for self references (update scripts, etc.) # the location of the dream2nix framework for self references (update scripts, etc.)

View File

@ -1,38 +1,79 @@
{ pkgs }: {
pkgs,
externalSources,
externals,
location,
}:
let let
lib = pkgs.lib; lib = pkgs.lib;
callPackage = pkgs.callPackage; callPackage = pkgs.callPackage;
callTranslator = file: name: args: pkgs.callPackage file (args // {
inherit externals;
translatorName = name;
});
# every translator must provide 'bin/translate' # every translator must provide 'bin/translate'
translatorExec = translatorPkg: "${translatorPkg}/bin/translate"; translatorExec = translatorPkg: "${translatorPkg}/bin/translate";
# directory names of a given directory
dirNames = dir: lib.attrNames (lib.filterAttrs (name: type: type == "directory") (builtins.readDir dir)); dirNames = dir: lib.attrNames (lib.filterAttrs (name: type: type == "directory") (builtins.readDir dir));
translators = # wrapPureTranslator
wrapPureTranslator = translatorAttrPath: pkgs.writeScriptBin "translate" ''
#!${pkgs.bash}/bin/bash
echo wrapPureTranslator
jsonInputFile=$1
outputFile=$(${pkgs.jq}/bin/jq '.outputFile' -c -r $jsonInputFile)
export d2nExternalSources=${externalSources}
nix eval --impure --raw --expr "
builtins.toJSON (
(import ${location} {}).translators.translatorsInternal.${
lib.concatStringsSep "." translatorAttrPath
}.translate
(builtins.fromJSON (builtins.readFile '''$1'''))
)
" | ${pkgs.jq}/bin/jq > $outputFile
'';
mkTranslatorsSet = function:
lib.genAttrs (dirNames ./.) (subsystem: lib.genAttrs (dirNames ./.) (subsystem:
lib.genAttrs lib.genAttrs
(lib.filter (dir: builtins.pathExists (./. + "/${subsystem}/${dir}")) [ "impure" "ifd" "pure-nix" ]) (lib.filter (dir: builtins.pathExists (./. + "/${subsystem}/${dir}")) [ "impure" "ifd" "pure" ])
(transType: (transType: function subsystem transType)
lib.genAttrs (dirNames (./. + "/${subsystem}/${transType}")) (translatorName:
callPackage (./. + "/${subsystem}/${transType}/${translatorName}") {}
)
)
); );
# dump the list of available translators to a json file so they can be listed in the CLI
translatorsJsonFile = pkgs.writeText "translators.json" (builtins.toJSON ( translators = mkTranslatorsSet (subsystem: type:
lib.genAttrs (dirNames ./.) (subsystem: lib.genAttrs (dirNames (./. + "/${subsystem}/${type}")) (translatorName:
lib.genAttrs if type == "impure" then
(lib.filter (dir: builtins.pathExists (./. + "/${subsystem}/${dir}")) [ "impure" "ifd" "pure-nix" ]) callTranslator (./. + "/${subsystem}/${type}/${translatorName}") translatorName {}
(transType: else
dirNames (./. + "/${subsystem}/${transType}") wrapPureTranslator [ subsystem type translatorName ]
)
) )
)); );
translatorsInternal = mkTranslatorsSet (subsystem: type:
lib.genAttrs (dirNames (./. + "/${subsystem}/${type}")) (translatorName:
callTranslator (./. + "/${subsystem}/${type}/${translatorName}") translatorName {}
)
);
translatorsJsonFile =
pkgs.writeText
"translators.json"
(builtins.toJSON
(mkTranslatorsSet (subsystem: type:
dirNames (./. + "/${subsystem}/${type}")
)
));
in in
{ {
inherit translators translatorsJsonFile; inherit translators translatorsInternal translatorsJsonFile;
} }

View File

@ -0,0 +1,39 @@
{
externals,
translatorName,
}:
let
translate =
{
inputFiles,
...
}:
let
parsed = externals.npmlock2nix.readLockfile (builtins.elemAt inputFiles 0);
in
{
sources = builtins.mapAttrs (pname: pdata:{
url = pdata.resolved;
type = "fetchurl";
hash = pdata.integrity;
}) parsed.dependencies;
generic = {
buildSystem = "nodejs";
buildSystemFormatVersion = 1;
producedBy = translatorName;
dependencyGraph = null;
sourcesCombinedHash = null;
};
buildSystem = {
nodejsVersion = 14;
};
};
in
{
inherit translate;
}

View File

@ -6,7 +6,7 @@ import sys
def main(): def main():
direcotry = sys.argv[1] directory = sys.argv[1]
output_file = sys.argv[2] output_file = sys.argv[2]
packages = {} packages = {}
@ -15,7 +15,7 @@ def main():
# - url # - url
# - sha256 # - sha256
# - format (sdist/wheel) # - format (sdist/wheel)
for path in list(glob(direcotry + '/*')): for path in list(glob(directory + '/*')):
_, _, file = path.rpartition('/') _, _, file = path.rpartition('/')
print(f"processing file: {file}") print(f"processing file: {file}")