mach-nix/flake.nix

117 lines
3.8 KiB
Nix
Raw 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";
inputs.nixpkgs.url = "github:NixOS/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;
usageGen = "usage: nix (build|develop) mach-nix#gen.(python|shell|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;
};
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;
2021-04-04 07:50:17 +00:00
shell = mach-nix-default.shellWith;
docker = mach-nix-default.dockerImageWith;
inherit (mach-nix-default)
pythonWith
shellWith
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; };
2021-05-14 10:04:54 +00:00
apps.extract-reqs =
let
extractor = import ./lib/extractor {
inherit pkgs;
lib = inp.nixpkgs.lib;
};
in
{
type = "app";
program = toString (pkgs.writeScript "extract.sh" ''
export SRC=$1
nix-build -o reqs -E 'let
pkgs = import <nixpkgs> {};
srcEnv = builtins.getEnv "SRC";
src = pkgs.copyPathToStore srcEnv;
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
'');
};
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}";
};
}