Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,68 @@
# flake-utils.lib.system.x86_64-darwin
];

poetryBuildSystems = final: prev:
lib.mapAttrs
(attr: systems:
builtins.foldl'
(drv: attr:
addBuildSystem {
inherit drv final attr;
})
prev.${attr}
systems)
(lib.importJSON "${poetry2nix}/overrides/build-systems.json");

addBuildSystem = {
final,
drv,
attr,
extraAttrs ? [],
}: let
buildSystem =
if builtins.isAttrs attr
then let
fromIsValid =
if builtins.hasAttr "from" attr
then lib.versionAtLeast drv.version attr.from
else true;
untilIsValid =
if builtins.hasAttr "until" attr
then lib.versionOlder drv.version attr.until
else true;
intendedBuildSystem =
if attr.buildSystem == "cython"
then final.python.pythonForBuild.cython
else final.${attr.buildSystem};
in
if fromIsValid && untilIsValid
then intendedBuildSystem
else null
else if attr == "cython"
then final.python.pythonForBuild.pkgs.cython
else final.${attr};
in (
# Flit only works on Python3
if (attr == "flit-core" || attr == "flit" || attr == "hatchling") && !final.isPy3k
then drv
else if drv == null
then null
else if drv ? overridePythonAttrs == false
then drv
else
drv.overridePythonAttrs (
old:
# We do not need the build system for wheels.
if old ? format && old.format == "wheel"
then {}
else {
nativeBuildInputs =
(old.nativeBuildInputs or [])
++ lib.optionals (!(builtins.isNull buildSystem)) [buildSystem]
++ map (a: final.${a}) extraAttrs;
}
)
);
/*
Takes a path to the kernels directory, `kernelsPath`,
a kernel name, `fileName`,
Expand Down Expand Up @@ -166,8 +228,9 @@
overlays = [
poetry2nix.overlay
rust-overlay.overlays.default
(self: super: {
(final: prev: {
npmlock2nix = pkgs.callPackage npmlock2nix {};
inherit poetryBuildSystems;
})
];

Expand Down
2 changes: 1 addition & 1 deletion kernels/available/python/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
projectDir ? self + "/kernels/available/python",
pyproject ? projectDir + "/pyproject.toml",
poetrylock ? projectDir + "/poetry.lock",
overrides ? pkgs.poetry2nix.overrides.withDefaults (import ./overrides.nix),
overrides ? import ./overrides.nix pkgs,
python ? pkgs.python3,
editablePackageSources ? {},
extraPackages ? ps: [],
Expand Down
17 changes: 16 additions & 1 deletion kernels/available/python/overrides.nix
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
final: prev: {}
pkgs: final: prev:
(pkgs.poetryBuildSystems final prev)
// {
cffi = prev.cffi.overridePythonAttrs (old: {
nativeBuildInputs = old.nativeBuildInputs or [] ++ [pkgs.pkg-config final.setuptools];
buildInputs = old.buildInputs or [] ++ [pkgs.libffi];
prePatch =
(old.prePatch or "")
+ pkgs.lib.optionalString pkgs.stdenv.isDarwin ''
# Remove setup.py impurities
substituteInPlace setup.py --replace "'-iwithsysroot/usr/include/ffi'" ""
substituteInPlace setup.py --replace "'/usr/include/ffi'," ""
substituteInPlace setup.py --replace '/usr/include/libffi' '${pkgs.lib.getDev pkgs.libffi}/include'
'';
});
}
1 change: 1 addition & 0 deletions kernels/example_python.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ availableKernels.python {
inherit name;
inherit (extraArgs) pkgs;
displayName = "Example Python Kernel";
extraPackages = ps: with ps; [requests numpy matplotlib];
}
1 change: 1 addition & 0 deletions kernels/example_stable_python.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ availableKernels.python {
inherit name;
pkgs = extraArgs.pkgs_stable;
displayName = "Example (nixpkgs stable) Python Kernel";
extraPackages = ps: with ps; [requests numpy matplotlib];
}
81 changes: 47 additions & 34 deletions overrides.nix
Original file line number Diff line number Diff line change
@@ -1,36 +1,49 @@
pkgs: let
addNativeBuildInputs = prev: drvName: inputs: {
"${drvName}" = prev.${drvName}.overridePythonAttrs (old: {
nativeBuildInputs = (old.nativeBuildInputs or []) ++ inputs;
});
};
pkgs: final: prev:
(pkgs.poetryBuildSystems final prev)
// {
cffi = prev.cffi.overridePythonAttrs (old: {
nativeBuildInputs = old.nativeBuildInputs or [] ++ [pkgs.pkg-config final.setuptools];
buildInputs = old.buildInputs or [] ++ [pkgs.libffi];
prePatch =
(old.prePatch or "")
+ pkgs.lib.optionalString pkgs.stdenv.isDarwin ''
# Remove setup.py impurities
substituteInPlace setup.py --replace "'-iwithsysroot/usr/include/ffi'" ""
substituteInPlace setup.py --replace "'/usr/include/ffi'," ""
substituteInPlace setup.py --replace '/usr/include/libffi' '${pkgs.lib.getDev pkgs.libffi}/include'
'';
});

# A fix is on the way soon, https://github.com/nix-community/poetry2nix/pull/787
preOverlay = final: prev: {
babel = null;
Babel = null;
babel_ = prev.babel.overridePythonAttrs (old: {
nativeBuildInputs = (old.nativeBuildInputs or []) ++ [final.setuptools];
});
};
nbconvert = prev.nbconvert.overridePythonAttrs (old: {
nativeBuildInputs = old.nativeBuildInputs or [] ++ [final.hatchling];
postPatch =
pkgs.lib.optionalString (pkgs.lib.versionAtLeast final.nbconvert.version "6.5.0") ''
substituteInPlace \
./nbconvert/exporters/templateexporter.py \
--replace \
'root_dirs.extend(jupyter_path())' \
'root_dirs.extend(jupyter_path() + [os.path.join("@out@", "share", "jupyter")])' \
--subst-var out
''
+ pkgs.lib.optionalString (pkgs.lib.versionAtLeast final.nbconvert.version "7.0") ''
substituteInPlace \
./hatch_build.py \
--replace \
'if self.target_name not in ["wheel", "sdist"]:' \
'if True:'
'';
});

postOverlay = final: prev:
{}
// {
babel = prev.babel_;
Babel = prev.babel_;
testbook = prev.testbook.overridePythonAttrs (old: {
postPatch = ''
mkdir ./tmp
${pkgs.unzip}/bin/unzip dist/testbook-${old.version}-py3-none-any.whl -d ./tmp
sed -i -e "s|if not any(arg.startswith('--Kernel|if False and not any(arg.startswith('--Kernel|" tmp/testbook/client.py
rm dist/testbook-${old.version}-py3-none-any.whl
pushd tmp
${pkgs.zip}/bin/zip -r ../dist/testbook-${old.version}-py3-none-any.whl ./*
popd
rm -rf tmp
'';
});
};
in
(pkgs.poetry2nix.defaultPoetryOverrides.overrideOverlay preOverlay).extend postOverlay
testbook = prev.testbook.overridePythonAttrs (old: {
postPatch = ''
mkdir ./tmp
${pkgs.unzip}/bin/unzip dist/testbook-${old.version}-py3-none-any.whl -d ./tmp
sed -i -e "s|if not any(arg.startswith('--Kernel|if False and not any(arg.startswith('--Kernel|" tmp/testbook/client.py
rm dist/testbook-${old.version}-py3-none-any.whl
pushd tmp
${pkgs.zip}/bin/zip -r ../dist/testbook-${old.version}-py3-none-any.whl ./*
popd
rm -rf tmp
'';
});
}