Hi,
i managed to create a package for my project with nix-build. However, I still have a doubt, how do I use it in my nix-shell file?
When putting its name in the list of packages that my nix-shell executes, it returns an undefined variable error.
Another question, after being able to run the package in nix-shell, does anything change to import it in my application.py?
Without your code it’s hard to say more, but if you have a file myprogram.nix
{stdenv, ... }:
stdenv.mkDerivation {
…
}
you can load it using something like that in your shell.nix
:
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
nativeBuildInputs = with pkgs; [
git
(callPackage ./myprogram.nix {})
];
}
If your package is a python library, like:
{stdenv, python3, ... }:
python3.buildPythonPackage {
…
}
then you might want to do something like
{ pkgs ? import <nixpkgs> {} }:
let
mypython = pkgs.python3.withPackages (ps: with ps; [ numpy (ps.callPackage ./myPythonPackage.nix {})]);
in
pkgs.mkShell {
nativeBuildInputs = with pkgs; [
git
(callPackage ./myprogram.nix {})
mypython
];
}
Thank you, it worked perfectly.
Another question came to me, how to call this package that I created as a constructor of another package that I want to create?
For example, this package I successfully built is in the requirements.txt of another package I want to build.
It’s possible?
Yes sure, in nix programs (or even OS ^^) can be manipulated as normal variables, so you just need to pass it to your new program, either via callPackage
or using an overlay (that basically globally adds your first package as new package).
In any case, modify your new program’s derivation into:
{stdenv, firstProgram, ... }:
…
Then, if you go for the first approach your shell is like:
{ pkgs ? import <nixpkgs> {} }:
let
mypython = pkgs.python3.withPackages (ps: with ps; [ numpy (ps.callPackage ./myPythonPackage.nix {})]);
firstProgram = pkgs.callPackage ./myprogram.nix {};
secondProgram = pkgs.callPackage ./myprogram.nix {inherit firstProgram};
in
pkgs.mkShell {
nativeBuildInputs = with pkgs; [
git
secondProgram
mypython
];
}