I’ve been working on a python package using nix-shell
and a shell.nix
, installing all dependencies with nix (no pip or virtualenv).
I now converted that shell.nix
to a default.nix
in the vein of the ones in nixpkgs (a function with dependencies as parameters, and a buildPythonPackage
) because I’m thinking of using it for deployment.
But I’d like to be able to drop to a shell like before, with a python with all the dependencies set up.
How do I write a shell.nix
that uses my default.nix
to get to that point?
Something like this:
{ pkgs ? import ./pin.nix {} }:
with pkgs;
with pkgs.python.pkgs;
let thePackage = callPackage ./default.nix {};
in mkShell {
propagateBuildInputs = [
thePackage
];
}
builds the package OK and gives me the right python version, but with no dependencies import
able.
(Oh, I’d need to have also the test dependencies, to run tests manually.)
This works, sort of:
{ pkgs ? import ./pin.nix {} }:
with pkgs;
with pkgs.python.pkgs;
let crawlers = callPackage ./default.nix {};
in (pkgs.python.buildEnv.override {
extraLibs = crawlers.propagatedBuildInputs;
}).env
but no test dependencies.
You should be able to do nix-shell default.nix
.
probably the easiest solution would be to import your default.nix from your shell.nix. nix-shell should do the right thing
Once I deleted the setup.py
that worked, but I still don’t have the test dependencies.
nix-shell default.nix -A ""
may also work
Is there any way to access the checkInputs
of a Python derivation?
checkInputs aren’t included in the shellHook logic:
buildInputs = mergeInputs "buildInputs";
nativeBuildInputs = mergeInputs "nativeBuildInputs";
propagatedBuildInputs = mergeInputs "propagatedBuildInputs";
propagatedNativeBuildInputs = mergeInputs "propagatedNativeBuildInputs";
shellHook = lib.concatStringsSep "\n" (lib.catAttrs "shellHook"
(lib.reverseList inputsFrom ++ [attrs]));
sounds like you want a python development environment. I would recommend looking at https://github.com/NixOS/nixpkgs/blob/c69153580b37c37964f2a01f113813fc0b7975a1/doc/languages-frameworks/python.section.md#how-to-consume-python-modules-using-pip-in-a-virtual-environment-like-i-am-used-to-on-other-operating-systems
But I don’t want to use pip…
shell.nix:
let
pkgs = import <nixpkgs> {};
in pkgs.mkShell (import ./default.nix).overrideAttrs(oldAttrs: {
buildInputs = oldAttrs.buildInputs ++ oldAttrs.checkInputs;
})
Hmmm, no luck. It says checkInputs
is not an attribute. I’ll try some more tomorrow, but for now, thanks 
This is a shell.nix
that works for me:
{ pkgs ? import ./pinned.nix {} }:
with pkgs;
with pkgs.python.pkgs;
let
crawlers = callPackage ./default.nix {};
pydrv = (pkgs.python.buildEnv.override {
extraLibs = crawlers.buildInputs ++ crawlers.nativeBuildInputs ++ [
# Development stuff
mypy pylama pylint black isort ipython
# Deployment stuff
shub
];
});
in mkShell {
buildInputs=[
pydrv crawlers.buildInputs jre
# Development stuff
nodejs yarn
];
}
Only problem is that jre
is in the checkInputs
of default.nix, but needs to be mentioned again here. Can live with that