Make variable resolution more uniform

This commit is contained in:
Alexander Bantyev 2020-03-26 20:24:04 +03:00
parent 4d585096ba
commit 1dabc4442d
Signed by: balsoft
GPG Key ID: E081FF12ADCB4AD5
3 changed files with 26 additions and 33 deletions

View File

@ -21,8 +21,9 @@ in rec {
}; };
# Generate a nix file from an opam file # Generate a nix file from an opam file
opam2nix = { src, opamFile ? findOpamFile src, ... }@args: opam2nix =
pkgs.runCommandNoCC (args.pname or opamFile + ".nix") args { src, opamFile ? findOpamFile src, name ? opamFile + ".nix", ... }:
pkgs.runCommandNoCC name { }
"cat ${src}/${opamFile} | ${pkgs.glibc.bin}/bin/iconv -c -f utf-8 -t ascii | ${opam-parser} > $out"; "cat ${src}/${opamFile} | ${pkgs.glibc.bin}/bin/iconv -c -f utf-8 -t ascii | ${opam-parser} > $out";
# Traverse OPAM repository, producing an extension to # Traverse OPAM repository, producing an extension to
@ -42,10 +43,15 @@ in rec {
opam2nix { opam2nix {
src = "${repo}/packages/${name}/${name}.${version}"; src = "${repo}/packages/${name}/${name}.${version}";
opamFile = "opam"; opamFile = "opam";
name = "${name}.nix";
};
package = version:
self.callPackage (file version) {
extraArgs = {
pname = name; pname = name;
inherit version; inherit version;
}; };
package = version: self.callPackage (file version) { }; };
latest = package (latestVersion (versions name)); latest = package (latestVersion (versions name));
others = map package (versions name); others = map package (versions name);
in latest // { in latest // {
@ -66,14 +72,13 @@ in rec {
# Extension that adds callOPAMPackage to the package set # Extension that adds callOPAMPackage to the package set
callOPAMPackage = self: super: { callOPAMPackage = self: super: {
callOPAMPackage = src: newAttrs: overrides: callOPAMPackage = src: extraArgs: overrides:
(self.callPackage (opam2nix (newAttrs // { inherit src; })) (self.callPackage (opam2nix (extraArgs // { inherit src; }))
overrides).overrideAttrs ({ buildInputs, ... }@args: (overrides // { inherit extraArgs; })).overrideAttrs ({ buildInputs, ... }@args: {
{
inherit src; inherit src;
buildInputs = buildInputs ++ newAttrs.extraBuildInputs or [ ]; buildInputs = buildInputs ++ extraArgs.extraBuildInputs or [ ];
propagatedBuildInputs = buildInputs propagatedBuildInputs = buildInputs
++ newAttrs.extraBuildInputs or [ ]; ++ extraArgs.extraBuildInputs or [ ];
} // newAttrs); });
}; };
} }

View File

@ -38,9 +38,9 @@ opam2nix OPAM {..} =
sepspace = mconcat . intersperse " " . normalize sepspace = mconcat . intersperse " " . normalize
preparephase = mconcat . intersperse " " . mconcat . intersperse ["\n"] preparephase = mconcat . intersperse " " . mconcat . intersperse ["\n"]
in in
"{ stdenv, fetchzip, " <>deps<> " }:\n" "{ stdenv, fetchzip, " <>deps<> ", extraArgs ? { } }:\n"
<>"with {};\n" -- Awful hack to allow this to evaluate even if some of the variables are undefined <>"with extraArgs;\n" -- Awful hack to allow this to evaluate even if some of the variables are undefined
<>"stdenv.mkDerivation rec {\n" <>"stdenv.mkDerivation (rec {\n"
<>foldMap (\name' -> " pname = \""<>name'<>"\";\n") name <>foldMap (\name' -> " pname = \""<>name'<>"\";\n") name
<>foldMap (\version' -> " version = \""<>version'<>"\";\n") version <>foldMap (\version' -> " version = \""<>version'<>"\";\n") version
<>foldMap (\url -> " src = builtins.fetchTarball { url = \""<>url<>"\"; };\n") source <>foldMap (\url -> " src = builtins.fetchTarball { url = \""<>url<>"\"; };\n") source
@ -58,8 +58,7 @@ opam2nix OPAM {..} =
<>preparephase checkPhase' <>preparephase checkPhase'
<>"\nrunHook postCheck\n'';\n") checkPhase <>"\nrunHook postCheck\n'';\n") checkPhase
<>" installPhase = ''\nrunHook preInstall\nopaline -prefix $out -libdir $OCAMLFIND_DESTDIR\nrunHook postInstall\n'';\n" <>" installPhase = ''\nrunHook preInstall\nopaline -prefix $out -libdir $OCAMLFIND_DESTDIR\nrunHook postInstall\n'';\n"
<>"} // extraArgs)\n"
<>"}\n"
update :: Maybe a -> a -> Maybe a update :: Maybe a -> a -> Maybe a
update old new = if isNothing old then Just new else old update old new = if isNothing old then Just new else old
@ -196,18 +195,7 @@ listParser valueParser =
whiteSpace = optional $ many $ oneOf " \n" whiteSpace = optional $ many $ oneOf " \n"
main :: IO () main :: IO ()
main = do main = getContents >>= \s -> case parse opamFile "(unknown)" s of
initialOPAM <- OPAM
<$> A.optional (getEnv "pname")
<*> A.optional (getEnv "version")
<*> pure Nothing
<*> pure Nothing
<*> pure Nothing
<*> pure Nothing
<*> pure Nothing
<*> pure Nothing
getContents >>= \s -> case parse opamFile "(unknown)" s of
Left e -> print e Left e -> print e
Right fs -> putStrLn $ opam2nix $ evaluateFields initialOPAM fs Right fs -> putStrLn $ opam2nix $ evaluateFields (OPAM Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing) fs
-- Right fs -> mapM_ print fs -- Right fs -> mapM_ print fs

View File

@ -1,5 +1,5 @@
opam-version: "2.0" opam-version: "2.0"
name: "memory-proto-alpha" name: "tezos-memory-proto-alpha"
version: "1.0" version: "1.0"
synopsis: "Tezos Protocol Alpha in memory" synopsis: "Tezos Protocol Alpha in memory"
maintainer: "Galfour <ligolang@gmail.com>" maintainer: "Galfour <ligolang@gmail.com>"