2020-04-22 09:28:58 +00:00
|
|
|
let
|
2020-05-19 09:46:48 +00:00
|
|
|
pkgs = import (import ./mach_nix/nix/nixpkgs-src.nix) { config = {}; overlays = []; };
|
2020-04-22 09:28:58 +00:00
|
|
|
python = import ./mach_nix/nix/python.nix { inherit pkgs; };
|
2020-05-19 09:46:48 +00:00
|
|
|
python_deps = (builtins.attrValues (import ./mach_nix/nix/python-deps.nix { inherit python; fetchurl = pkgs.fetchurl; }));
|
2020-08-09 13:24:12 +00:00
|
|
|
mergeOverrides = with pkgs.lib; foldr composeExtensions (self: super: { });
|
2020-05-19 09:46:48 +00:00
|
|
|
autoPatchelfHook = import ./mach_nix/nix/auto_patchelf_hook.nix {inherit (pkgs) fetchurl makeSetupHook writeText;};
|
2020-08-25 08:21:49 +00:00
|
|
|
concat_reqs = reqs_list:
|
|
|
|
let
|
|
|
|
concat = s1: s2: s1 + "\n" + s2;
|
|
|
|
in
|
|
|
|
builtins.foldl' concat "" reqs_list;
|
2020-08-25 03:11:08 +00:00
|
|
|
extractMeta = python: src: extras:
|
|
|
|
with builtins;
|
|
|
|
let
|
|
|
|
file = (builtins.readFile "${(import ./lib/extractor).extract_from_src {
|
|
|
|
py = python;
|
|
|
|
src = src;
|
|
|
|
}}/python.json");
|
|
|
|
data = fromJSON file;
|
|
|
|
setup_requires = if hasAttr "setup_requires" data then data.setup_requires else [];
|
|
|
|
install_requires = if hasAttr "install_requires" data then data.install_requires else [];
|
|
|
|
name = if hasAttr "name" data then data.name else null;
|
|
|
|
version = if hasAttr "version" data then data.version else null;
|
|
|
|
extras_require =
|
|
|
|
if hasAttr "install_requires" data then
|
|
|
|
pkgs.lib.flatten (map (extra: data.extras_require."${extra}") extras)
|
|
|
|
else [];
|
2020-08-25 08:21:49 +00:00
|
|
|
all_reqs = concat_reqs (setup_requires ++ install_requires ++ extras_require);
|
2020-08-25 03:11:08 +00:00
|
|
|
in
|
|
|
|
{
|
2020-08-25 08:21:49 +00:00
|
|
|
requirements = trace "\n automatically detected requirements of ${name} ${version}:${all_reqs}\n\n" all_reqs;
|
2020-08-25 03:11:08 +00:00
|
|
|
inherit name version;
|
|
|
|
};
|
|
|
|
is_http_url = url:
|
|
|
|
with builtins;
|
|
|
|
if (substring 0 8 url) == "https://" || (substring 0 7 url) == "http://" then true else false;
|
|
|
|
get_src = src:
|
|
|
|
with builtins;
|
|
|
|
if isString src && is_http_url src then (fetchTarball src) else src;
|
2020-04-22 09:28:58 +00:00
|
|
|
in
|
2020-04-24 10:35:42 +00:00
|
|
|
rec {
|
2020-05-19 09:46:48 +00:00
|
|
|
# the mach-nix cmdline tool derivation
|
2020-04-24 10:35:42 +00:00
|
|
|
mach-nix = python.pkgs.buildPythonPackage rec {
|
|
|
|
pname = "mach-nix";
|
|
|
|
version = builtins.readFile ./mach_nix/VERSION;
|
|
|
|
name = "${pname}-${version}";
|
|
|
|
src = ./.;
|
|
|
|
propagatedBuildInputs = python_deps;
|
|
|
|
doCheck = false;
|
|
|
|
};
|
|
|
|
|
2020-05-19 09:46:48 +00:00
|
|
|
inherit mergeOverrides;
|
2020-04-24 10:35:42 +00:00
|
|
|
|
2020-05-19 09:46:48 +00:00
|
|
|
# User might want to access it to choose python version
|
|
|
|
nixpkgs = pkgs;
|
|
|
|
|
|
|
|
# call this to generate a nix expression which contains the python overrides
|
|
|
|
machNixFile = args: import ./mach_nix/nix/mach.nix args;
|
|
|
|
|
|
|
|
# Returns `overrides` and `select_pkgs` which satisfy your requirements
|
2020-07-03 11:03:10 +00:00
|
|
|
machNix = args:
|
2020-08-09 13:24:12 +00:00
|
|
|
let
|
|
|
|
result = import "${machNixFile args}/share/mach_nix_file.nix";
|
|
|
|
manylinux =
|
|
|
|
if pkgs.stdenv.hostPlatform.system == "x86_64-darwin" then
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
pkgs.pythonManylinuxPackages.manylinux1;
|
2020-07-03 11:03:10 +00:00
|
|
|
in {
|
2020-08-09 13:24:12 +00:00
|
|
|
overrides = result.overrides manylinux autoPatchelfHook;
|
2020-07-03 11:03:10 +00:00
|
|
|
select_pkgs = result.select_pkgs;
|
|
|
|
};
|
2020-04-24 10:35:42 +00:00
|
|
|
|
|
|
|
# call this to use the python environment with nix-shell
|
|
|
|
mkPythonShell = args: (mkPython args).env;
|
2020-05-19 09:46:48 +00:00
|
|
|
|
2020-07-03 11:03:10 +00:00
|
|
|
# equivalent to buildPythonPackage of nixpkgs
|
2020-08-25 03:11:08 +00:00
|
|
|
buildPythonPackage = __buildPython "buildPythonPackage";
|
2020-07-03 11:03:10 +00:00
|
|
|
|
|
|
|
# equivalent to buildPythonApplication of nixpkgs
|
2020-08-25 03:11:08 +00:00
|
|
|
buildPythonApplication = __buildPython "buildPythonApplication";
|
|
|
|
|
2020-08-25 08:21:49 +00:00
|
|
|
__buildPython = with builtins; func: args:
|
|
|
|
if isString args || isPath args then _buildPython func { src = args; } else _buildPython func args;
|
2020-07-03 11:03:10 +00:00
|
|
|
|
|
|
|
_buildPython = func: args@{
|
2020-08-25 03:38:30 +00:00
|
|
|
add_requirements ? "", # add additional requirements to the packge
|
2020-08-24 11:20:29 +00:00
|
|
|
requirements ? null, # content from a requirements.txt file
|
2020-07-03 11:03:10 +00:00
|
|
|
disable_checks ? true, # Disable tests wherever possible to decrease build time.
|
2020-08-24 11:20:29 +00:00
|
|
|
extras ? [],
|
|
|
|
doCheck ? ! disable_checks,
|
2020-07-03 11:03:10 +00:00
|
|
|
overrides_pre ? [], # list of pythonOverrides to apply before the machnix overrides
|
|
|
|
overrides_post ? [], # list of pythonOverrides to apply after the machnix overrides
|
2020-08-09 13:24:12 +00:00
|
|
|
pkgs ? nixpkgs, # pass custom nixpkgs.
|
2020-07-03 11:03:10 +00:00
|
|
|
providers ? {}, # define provider preferences
|
|
|
|
pypi_deps_db_commit ? builtins.readFile ./mach_nix/nix/PYPI_DEPS_DB_COMMIT, # python dependency DB version
|
|
|
|
pypi_deps_db_sha256 ? builtins.readFile ./mach_nix/nix/PYPI_DEPS_DB_SHA256,
|
2020-08-09 13:24:12 +00:00
|
|
|
python ? pkgs.python3, # select custom python to base overrides onto. Should be from nixpkgs >= 20.03
|
2020-07-03 11:03:10 +00:00
|
|
|
_provider_defaults ? with builtins; fromTOML (readFile ./mach_nix/provider_defaults.toml),
|
|
|
|
...
|
|
|
|
}:
|
2020-08-25 03:11:08 +00:00
|
|
|
with builtins;
|
2020-07-03 11:03:10 +00:00
|
|
|
let
|
2020-08-25 03:11:08 +00:00
|
|
|
src = get_src pass_args.src;
|
|
|
|
# Extract dependencies automatically if 'requirements' is unset
|
|
|
|
meta = extractMeta python src extras;
|
2020-08-24 11:20:29 +00:00
|
|
|
reqs =
|
|
|
|
with builtins;
|
2020-08-25 08:21:49 +00:00
|
|
|
(if requirements == null then
|
2020-08-24 11:20:29 +00:00
|
|
|
if builtins.hasAttr "format" args && args.format != "setuptools" then
|
|
|
|
throw "Automatic dependency extraction is only available for 'setuptools' format."
|
2020-08-25 03:11:08 +00:00
|
|
|
" Please specify 'requirements' if setuptools is not used."
|
2020-08-24 11:20:29 +00:00
|
|
|
else
|
2020-08-25 08:21:49 +00:00
|
|
|
meta.requirements
|
2020-08-24 11:20:29 +00:00
|
|
|
else
|
2020-08-25 08:21:49 +00:00
|
|
|
requirements) + "\n" + add_requirements;
|
2020-08-25 03:11:08 +00:00
|
|
|
pname =
|
|
|
|
if hasAttr "name" args then null
|
|
|
|
else if hasAttr "pname" args then args.pname
|
|
|
|
else meta.name;
|
|
|
|
version =
|
|
|
|
if hasAttr "name" args then null
|
|
|
|
else if hasAttr "version" args then args.version
|
|
|
|
else meta.version;
|
2020-07-03 11:03:10 +00:00
|
|
|
py = python.override { packageOverrides = mergeOverrides overrides_pre; };
|
|
|
|
result = machNix {
|
2020-08-24 11:20:29 +00:00
|
|
|
inherit disable_checks providers pypi_deps_db_commit pypi_deps_db_sha256 _provider_defaults;
|
2020-08-09 13:24:12 +00:00
|
|
|
overrides = overrides_pre;
|
2020-07-03 11:03:10 +00:00
|
|
|
python = py;
|
2020-08-25 08:21:49 +00:00
|
|
|
requirements = reqs;
|
2020-07-03 11:03:10 +00:00
|
|
|
};
|
|
|
|
py_final = python.override { packageOverrides = mergeOverrides (
|
2020-08-17 11:16:56 +00:00
|
|
|
overrides_pre ++ [ result.overrides ] ++ overrides_post
|
|
|
|
);};
|
|
|
|
pass_args = removeAttrs args (builtins.attrNames ({
|
2020-08-25 03:38:30 +00:00
|
|
|
inherit add_requirements disable_checks overrides_pre overrides_post pkgs providers
|
2020-08-24 11:20:29 +00:00
|
|
|
requirements pypi_deps_db_commit pypi_deps_db_sha256 python _provider_defaults;
|
2020-08-17 11:16:56 +00:00
|
|
|
}));
|
2020-07-03 11:03:10 +00:00
|
|
|
in
|
|
|
|
py_final.pkgs."${func}" ( pass_args // {
|
|
|
|
propagatedBuildInputs = result.select_pkgs py_final.pkgs;
|
2020-08-25 03:11:08 +00:00
|
|
|
src = src;
|
|
|
|
inherit doCheck pname version;
|
2020-08-25 08:21:49 +00:00
|
|
|
passthru = {
|
|
|
|
requirements = reqs;
|
|
|
|
};
|
2020-07-03 11:03:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2020-05-19 09:46:48 +00:00
|
|
|
# (High level API) generates a python environment with minimal user effort
|
2020-08-25 08:21:49 +00:00
|
|
|
mkPython = args: if builtins.isList args then _mkPython { extra_pkgs = args; } else _mkPython args;
|
|
|
|
|
|
|
|
_mkPython =
|
2020-05-19 09:46:48 +00:00
|
|
|
{
|
2020-08-25 08:21:49 +00:00
|
|
|
requirements ? "", # content from a requirements.txt file
|
2020-05-19 09:46:48 +00:00
|
|
|
disable_checks ? true, # Disable tests wherever possible to decrease build time.
|
2020-08-25 08:21:49 +00:00
|
|
|
extra_pkgs ? [],
|
2020-05-19 09:46:48 +00:00
|
|
|
overrides_pre ? [], # list of pythonOverrides to apply before the machnix overrides
|
|
|
|
overrides_post ? [], # list of pythonOverrides to apply after the machnix overrides
|
2020-08-09 13:24:12 +00:00
|
|
|
pkgs ? nixpkgs, # pass custom nixpkgs.
|
2020-05-19 09:46:48 +00:00
|
|
|
providers ? {}, # define provider preferences
|
|
|
|
pypi_deps_db_commit ? builtins.readFile ./mach_nix/nix/PYPI_DEPS_DB_COMMIT, # python dependency DB version
|
|
|
|
pypi_deps_db_sha256 ? builtins.readFile ./mach_nix/nix/PYPI_DEPS_DB_SHA256,
|
2020-08-09 13:24:12 +00:00
|
|
|
python ? pkgs.python3, # select custom python to base overrides onto. Should be from nixpkgs >= 20.03
|
2020-05-19 09:46:48 +00:00
|
|
|
_provider_defaults ? with builtins; fromTOML (readFile ./mach_nix/provider_defaults.toml)
|
|
|
|
}:
|
2020-08-25 08:21:49 +00:00
|
|
|
with builtins;
|
2020-05-19 09:46:48 +00:00
|
|
|
let
|
2020-08-25 08:21:49 +00:00
|
|
|
_extra_pkgs = map (p: if isString p || isPath p then buildPythonPackage p else p) extra_pkgs;
|
|
|
|
extra_pkgs_reqs =
|
|
|
|
map (p:
|
|
|
|
if builtins.hasAttr "requirements" p then p.requirements
|
|
|
|
else throw "Packages passed via 'extra_pkgs' must be built via mach-nix.buildPythonPackage"
|
|
|
|
) _extra_pkgs;
|
2020-05-19 09:46:48 +00:00
|
|
|
py = python.override { packageOverrides = mergeOverrides overrides_pre; };
|
|
|
|
result = machNix {
|
2020-08-25 08:21:49 +00:00
|
|
|
inherit disable_checks providers pypi_deps_db_commit pypi_deps_db_sha256 _provider_defaults;
|
2020-08-09 13:24:12 +00:00
|
|
|
overrides = overrides_pre;
|
2020-05-19 09:46:48 +00:00
|
|
|
python = py;
|
2020-08-25 08:21:49 +00:00
|
|
|
requirements = concat_reqs ([requirements] ++ extra_pkgs_reqs);
|
2020-05-19 09:46:48 +00:00
|
|
|
};
|
|
|
|
py_final = python.override { packageOverrides = mergeOverrides (
|
2020-07-03 11:03:10 +00:00
|
|
|
overrides_pre ++ [ result.overrides ] ++ overrides_post
|
2020-05-19 09:46:48 +00:00
|
|
|
);};
|
|
|
|
in
|
2020-08-25 08:21:49 +00:00
|
|
|
py_final.withPackages (ps: (result.select_pkgs ps) ++ _extra_pkgs)
|
2020-05-19 09:46:48 +00:00
|
|
|
;
|
2020-04-24 10:35:42 +00:00
|
|
|
}
|