mach-nix/flake.nix

179 lines
6.1 KiB
Nix
Raw Permalink Normal View History

2020-07-18 06:41:26 +00:00
{
2020-10-03 19:11:50 +00:00
description = "Create highly reproducible python environments";
2020-07-18 06:41:26 +00:00
2020-10-03 19:11:50 +00:00
inputs.flake-utils.url = "github:numtide/flake-utils";
2022-02-03 16:02:45 +00:00
inputs.nixpkgs.url = "nixpkgs/nixos-unstable";
inputs.pypi-deps-db = {
url = "github:DavHau/pypi-deps-db";
flake = false;
};
2020-07-18 06:41:26 +00:00
outputs = { self, nixpkgs, flake-utils, ... }@inp:
with nixpkgs.lib;
let
dataLastModified = toInt (readFile "${inp.pypi-deps-db}/UNIX_TIMESTAMP");
dataOutdated =
if inp.nixpkgs.sourceInfo ? lastModified
&& dataLastModified < inp.nixpkgs.sourceInfo.lastModified then
true
else
false;
2021-05-22 14:49:39 +00:00
usageGen = "usage: nix (build|shell) mach-nix#gen.(python|docker).package1.package2...";
in
(flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
mach-nix-default = import ./default.nix {
inherit pkgs dataOutdated;
pypiData = inp.pypi-deps-db;
};
in rec
{
devShell = import ./shell.nix {
inherit pkgs;
2021-06-06 07:57:04 +00:00
pypiData = "${inp.pypi-deps-db}";
};
packages = rec {
inherit (mach-nix-default) mach-nix;
sdist = pkgs.runCommand "mach-nix-sdist"
{ buildInputs = mach-nix-default.pythonDeps; }
''
mkdir src
cp -r ${./.}/* src
cd src
python setup.py sdist -d $out
'';
# fake package which contains functions inside passthru
gen = pkgs.stdenv.mkDerivation {
name = usageGen;
src = throw usageGen;
passthru = {
python = mach-nix-default.pythonWith;
docker = mach-nix-default.dockerImageWith;
inherit (mach-nix-default)
pythonWith
dockerImageWith;
};
};
};
2020-07-18 06:41:26 +00:00
defaultPackage = packages.mach-nix;
2020-07-18 06:41:26 +00:00
apps.mach-nix = flake-utils.lib.mkApp { drv = packages.mach-nix.mach-nix; };
2022-01-29 05:31:13 +00:00
apps.extract-reqs =
let
2021-05-14 10:04:54 +00:00
extractor = import ./lib/extractor {
inherit pkgs;
lib = inp.nixpkgs.lib;
};
in
{
type = "app";
program = toString (pkgs.writeScript "extract.sh" ''
2022-01-29 05:31:13 +00:00
export SRC=$1
2021-05-14 10:04:54 +00:00
nix-build -o reqs -E 'let
pkgs = import <nixpkgs> {};
srcEnv = builtins.getEnv "SRC";
2022-01-29 05:31:13 +00:00
src = pkgs.copyPathToStore srcEnv;
2021-05-14 10:04:54 +00:00
srcTar = pkgs.runCommand "src.tar.gz" {} "mkdir src && cp -r ''${src}/* src/ && pwd && ls -la && tar -c src | gzip -1 > $out";
in (import ./lib/extractor {}).extract_from_src {
py="python3";
src = srcTar;
}'
cat reqs/*
rm reqs
'');
};
2021-06-06 07:57:04 +00:00
2021-06-09 06:11:17 +00:00
apps.tests-unit = {
type = "app";
program = toString (pkgs.writeScript "tests-unit" ''
export PATH="${pkgs.lib.makeBinPath (with pkgs; [
(import ./mach_nix/nix/python.nix {
inherit pkgs;
dev = true;
})
2021-10-20 20:21:11 +00:00
]
++ pkgs.lib.optional (stdenv.isLinux) busybox
# This is not equivalent to "busybox", but is close enough for
# everything to work. The only quirk here is borrowing a trick
# from nixpkgs to provide a "/bin/sh" that's identical to the
# one nixpkgs exists (coreutils doesn't bundle /bin/sh but
# busybox does).
++ pkgs.lib.optionals (stdenv.isDarwin) [
coreutils
2021-06-09 06:11:17 +00:00
])}"
export PYPI_DATA=${inp.pypi-deps-db}
export CONDA_DATA=${(import ./mach_nix/nix/conda-channels.nix {
inherit pkgs;
providers = { _default = [ "conda/main" "conda/r" "conda/conda-forge"]; };
}).condaChannelsJson}
# Use "python -m pytest" to add current directory to python path.
# This ensures that mach_nix is importable.
2021-06-09 06:11:17 +00:00
echo "executing unit tests"
python -m pytest -n ''${WORKERS:-$(nproc)} -x ${./.}
2021-06-09 06:11:17 +00:00
'');
};
apps.tests-eval = {
type = "app";
program = toString (pkgs.writeScript "tests-eval" ''
export PATH="${pkgs.lib.makeBinPath (with pkgs; [
git
nixFlakes
parallel
2022-02-10 00:57:50 +00:00
bash
2021-10-20 20:21:11 +00:00
]
++ pkgs.lib.optional (stdenv.isLinux) busybox
++ pkgs.lib.optionals (stdenv.isDarwin) [
coreutils
2021-06-09 06:11:17 +00:00
])}"
cd tests
2022-02-10 00:59:02 +00:00
echo "executing evaluation tests"
2021-06-09 06:11:17 +00:00
./execute.sh
'');
};
apps.tests-all = {
type = "app";
program = toString (pkgs.writeScript "tests-eval" ''
2022-02-10 00:59:02 +00:00
set -e
2021-06-09 13:27:46 +00:00
${apps.tests-unit.program}
${apps.tests-eval.program}
2022-02-10 00:59:02 +00:00
CONDA_TESTS=y ${apps.tests-eval.program}
2021-06-09 06:11:17 +00:00
'');
};
defaultApp = { type = "app"; program = "${defaultPackage}/bin/mach-nix"; };
lib = {
inherit (mach-nix-default)
mkPython
mkPythonShell
mkDockerImage
mkOverlay
mkNixpkgs
mkPythonOverrides
2020-11-25 07:09:40 +00:00
buildPythonPackage
buildPythonApplication
fetchPypiSdist
fetchPypiWheel
;
};
}
))
// # deprecated usage
{
pythonWith = {} // throw "\n'pythonWith' is deprecated.\n${usageGen}";
"with" = {} // throw "\n'with' is deprecated.\n${usageGen}";
shellWith = {} // throw "\n'shellWith' is deprecated.\n${usageGen}";
dockerImageWith = {} // throw "\n'dockerImageWith' is deprecated.\n${usageGen}";
};
}